Take the 2-minute tour ×

I am trying to understand generics in a semantic way. For instance, abstract classes seemed to snap into place for me when I read people refer to them as structures that can set policy. Interfaces snapped when I read people refer to them as collaboration contracts.

What are some good ways to think about generics that might help me to differentiate them from other OO structures and write more intelligent APIs?

share|improve this question
Think of them as Type-Templates. –  RBarryYoung Apr 6 '13 at 1:00
add comment

3 Answers

up vote 1 down vote accepted

Think of generic classes as stencils to make other classes (similarly, generic functions are stencils for making other functions). Type parameters serve as openings in your stencils: by plugging in a concrete type into them, you make the generic class or the generic function into a real class or function. The type parameters "stick through" the designated holes in the stencil, producing a complete definition.

share|improve this answer
This is interesting. If I understand this correctly, it is almost as if you are marking parts of your method/class signature abstract... to be defined by the caller. warmer? or colder? –  cocogorilla Apr 6 '13 at 0:49
@user1902664 Correct, you keep parts of the class unfinished, until the user supplies the missing type information. –  dasblinkenlight Apr 6 '13 at 0:58
Accepting this answer as the most helpful to me in getting my brain thinking about them differently. Thank you. –  cocogorilla Apr 6 '13 at 1:36
add comment

It seems you want to approach your understanding from a top-down perspective. "What is it" in a qualitative sense and then derive the real meaning from there. Isn't it easier to simply learn what these different constructs do rather than trying to come up with labels? i.e. approach it from a bottom-up perspective and infer your own qualitative descriptions from what you've now already understood firsthand.

Abstract classes require you to implement a property or method and can't be instantiated. What distinguishes it from an interface? It requires subclasses to choose yours as its only base class. Interfaces face no such restriction but require you to define its entire behavior in the implementation, rather than relying on some of the behavior to be defined in the base class.

Similarly, generics allow you to introduce types as variables that can be specified by the caller. The utility of this is analogous to method parameters in general, just taken to a higher level. In other words, method parameters allow you to vary the implementation based on some input specified by the caller. Generic parameters allow you to vary the implementation based on some (other) input (i.e. types) specified by the caller.

Surely it's clear why List<T> is more useful than ArrayList. I'm not really sure why metaphors are really helpful for understanding why.

share|improve this answer
I do appreciate your effort, truly. But I had to lol... I got half way through and all the details turned to gobbledygook in my brain. I personally have difficulty deriving the hole from the parts (my wife does not). If I can get a gestalt... then suddenly the parts start falling into place. I've seen many List<T> vs ArrayList examples, but I end up thinking about generics as containers, which is limiting. Your comparison to parameters helps though. Thank you. –  cocogorilla Apr 6 '13 at 1:15
@user1902664, but do you understand why List<T> is superior to ArrayList as a consumer of those classes? If so, then it's a simple step for you to try to implement one of those classes (or a class like it) yourself. Once you do, I guarantee it will come with understanding. –  Kirk Woll Apr 6 '13 at 1:16
Yes. As a consumer of List<T> and ArrayList, I completely understand the superiority. Actually, trying to implement one is why I asked this question... because I was uncertain that I was grasping the why, and so uncertain I was using it correctly. i.e. maybe an interface would serve my purpose just fine... or maybe a generic really would be useful. I cannot say until I grasp it. –  cocogorilla Apr 6 '13 at 1:29
add comment

You could view them as wrappers around object types. You are creating functions that will do something for whatever type of object it is instantiated for, so it's like a template that will perform the same work for multiple types of objects.

Microsoft's introduction to generics might have some good descriptions as well


share|improve this answer
add comment

Your Answer


