Search code examples
genericslanguage-agnosticsemantics

Why are generics called generics?


At the risk of becoming the village idiot, can someone explain to me why generics are called generics? I understand their usage and benefits, but if the definition of generic is "general" and generic collections are type safe, then why isn't this a misnomer?

For example, an ArrayList can hold anything that's an object:

ArrayList myObjects = new ArrayList();
myObjects.Add("one");
myObjects.Add(1);

while a generic collection of type string can only hold strings:

var myStrings = new List<string>();
myStrings.Add("one");
myStrings.Add("1");

I'm just not clear on why it's called "generic". If the answer is "...which make it possible to design classes and methods that defer the specification of one or more types until the class or method is declared and instantiated by client code." from here, then I suppose that makes sense. Perhaps I'm having this mental lapse because I only began programming after Java introduced generics, so I don't recall a time before them. But still...

Any help is appreciated.


Solution

  • "Generic" is talking about the implementation. You write a single "Generic" list implementation that works with any type, instead of having to write specific implementations for each type you want to use.