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 ?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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:

In .Net 2.0, they have made it even easier by introducing the List() class and the concept of Generics:



Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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.
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!
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

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:


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 :)


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)...
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)
    else if(obj is Animal)
    else if(obj is Plant)

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
scarface7Author Commented:
Thank you all for the nice responses !
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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.