What is "strongly typed" ??

Posted on 2006-04-18
Last Modified: 2010-04-16
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 ?
Question by:scarface7
    LVL 85

    Accepted Solution

    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:


    LVL 13

    Expert Comment

    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.
    LVL 85

    Expert Comment

    by:Mike Tomlinson
    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!

    Author Comment

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


    LVL 85

    Expert Comment

    by:Mike Tomlinson 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)...
    LVL 35

    Expert Comment

    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

    Author Comment

    Thank you all for the nice responses !
    LVL 13

    Expert Comment

    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.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
    Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now