• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 515
  • Last Modified:

What is "strongly typed" ??

I hear the term "strongly typed" used a lot with in the context of C# programming for example "strongly typed" datasets, collections etc. What does this mean ?
0
scarface7
Asked:
scarface7
  • 3
  • 2
  • 2
  • +1
1 Solution
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Something that is strongly typed will only accept a specific kind of data type.  For instance, the generic ArrayList collection will accept any data type you add to it.  You could even mix data types in it!

A strongly typed collection however would only accept ONE kind of data type.  This would ensure that all items within it are of that type and therefore you can safely make calls to and methods of that type.  One way to make a strongly typed collection would be to create a class that encapsulates a generic collection such as the ArrayList but all of you public methods only accept the desired data type.

The preferred method though is to inherit from the CollectionBase() class:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemcollectionscollectionbaseclasstopic.asp

In .Net 2.0, they have made it even easier by introducing the List() class and the concept of Generics:
http://msdn2.microsoft.com/en-us/library/6sh2ey19(VS.80).aspx

Generics:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnvs05/html/csharp_generics.asp
http://msdn2.microsoft.com/en-us/library/512aeb7t.aspx

0
 
devsolnsCommented:
idle i see where your going with your first statement but i think its a little misleading.  An arraylist is indeed strongly typed as is EVERYTHING is .NET.  The statement "a strongly typed collection however would only accept ONE kind of data type."  Ok then what about this,

class A {}
class B : A {}

List<A> list = new List<A>();
list.Add(new B());

The same thing is happening with an ArrayList.  So are you saying these are not strongly typed?

the fact that it accepts all types is because they derive from type object.  if they did not then the arraylist would not accept them.

it might be easier to give a weakly typed example.  variants in vb and javascript are good examples of this because they can hold the value of everything and its NOT because those types dervice from a common base type.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
There seems to be a wide range of what is considered strong/weak typing out there on the net...

Indeed, Inheritance appears to allow more than one type to be added to a strongly typed collection...but in your example, Class B "is a" Class A so we aren't really adding a different type.

As you pointed out, we can draw the same parallel with an ArrayList as it accepts any type derived from Object (which IS everything), but I don't think many people would consider an ArrayList strongly typed.  I mean, why then introduce the concept of Generics if the ArrayList was already strongly typed?...

So I guess a better definition of a strongy typed collection is any that accepts only one data type (or any of its derivations), where the data type accepted is not the base data type for all other data types in the language.

Glad to have a discussion with someone...wish more people would pipe in!
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
scarface7Author Commented:
Thank you for responses.

Idle_Mind, I looked at the links you provided me. I would like to learn a bit more from the first comment you made and the example in one of the links:

http://msdn2.microsoft.com/en-us/library/6sh2ey19.aspx

The above link gives a "Dinosaur" example for the List Generic Class.

It seems the example is trying to demonstrate that the specific list generic class will only accept objects of type "dinosaur". Is this what makes it "strongly typed" ? Are you saying that in a simple ArrayList collection, you could have different types of objects being stored in the same collection, such as "Plant" for example ? So if an ArrayList were to be used instead it could have stored "Plant" along with "Dinosaur" ?

devsolns, feel free to argue this out :)

Thanks.



0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Yes...an ArrayList could have more than one Type in it.  Of course, they are all Objects but in the context you've given, you could have both "Plant" and "Dinosaur" in the ArrayList at the same time.  The objects don't even need to be related at all...

More discussion later...have to get out of the house as a showing is coming (I'm selling my house)...
0
 
mrichmonCommented:
One thing, shich Idle_Mind, is referring to is that you can have levels of strong typing.

An ArrayList is considered weakly typed because EVERY class inherits from the object class implicitly.  So although any class you create "IS A" object, in general it is not considered strongly typed because it is the default base type.  In fact it is SO base that if you ask an object for it's Base Type (which there are functions to do) you do not get "object" ever returned.  Using the below example getting the base type of a Dinosaur would return Animal - not object even though both Animal and Dinosaur "IS A" object.  --I realize that is bad grammer, but that is what it is :o)

When you bring inheritance into the picture then you have levels of strong typing.  It all is based on the IS A relationship as Idle_Mind pointed out above.

So if we look at

class Dinosaur: Animal
{
...
}

List<Dinosaur> d = new List<Dinosaur>();
List<Animal> a = new List<Animal> ();

Then you could have this:

a.Add(new Human("Bob")); // works
a.Add(new Dinosaur("T-Rex")); // works

d.Add(new Human("Bob")); // error
d.Add(new Dinosaur("T-Rex")); // works

so a could contain anything that IS A Animal - whether directly or indirectly.
but d can only contain anything that IS A Dinosaur - or a child type

So the list a is considered more strongly typed than the list d


And if we wanted to thow in the base ArrayList from .NET 1 you have:

ArrayList o = new ArrayList();

o.Add(new Human("Bob")); // works
o.Add(new Dinosaur("T-Rex")); // works
o.Add(new Plant("Tree")); // works even though there is no relationship between Animal and Plant NOR Dinosaur and Plant


Now what does all this mean when accessing the lists?

Well in the ArrayList you would have to test each object coming out and cast like so:

foreach(object obj in o)
{
    if(obj is Dinosaur)
         ((Dinosaur)obj).SomeDinoFunctionOrProperty;
    else if(obj is Animal)
         ((Animal)obj).SomeAnimalFunctionOrProperty;
    else if(obj is Plant)
         ((Plant)obj).SomePlantFunctionOrProperty;
}

Note that you would have to ensure that the type detection was in order from most specific to least specific to get teh best cast, although the animal cast would work on a dinosaur, you could not access Dinosaur specific properties...  Without doing the casts by default since everything IS A object you would only have access to very base level properties such as ToString(), even if every class had a property called "Name"

However, with the strongly typed lists it comes out as the type the list is defined as.  So if all you were doing was printing out a name you could do that for BOTH the dinosaur entry and the Animal entry in list a without any cast.  You have by default access to all the properties that are defined by the base type of the list.  You would still have to cast if you wanted Dinosaur specific properties for a dinosaur, but any common fucntionality due to inheritance would be already present.

Hence there is a scale of strong typing, but in general - including in most books on the subject - you will see object considered as weak type - and anything with inheritance considered degrees of strong typing.

I hope that helps
0
 
scarface7Author Commented:
Thank you all for the nice responses !
0
 
devsolnsCommented:
Truly strongly typed can mean different things depending on the context of the question.  Both points are correct, great responses idle & mrichmon.

Typically the terminology I use is such,

I'd say that an ArrayList is indeed strongly typed but is not type safe which I think is a pretty accurate and clarifying description.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now