I think as you "get to know" Generics, and have a "feeling" for how to use them, you will find them immensely useful. I also predict that your sense of what Generics can be used to do ... and to greatly simplify the coding of ... will come in "stages."
So, let me outline a few of what I consider the main "goodnesses" that Generics add to .NET, in order of what I believe is their easiness to understand, and the likelihood that their usefulness will be immediately visible to you.
0. for all use-cases: generics eliminate the need for type-checking in your code, and casting from Object to a specific Type: that's a pain-in-the-ass removed !
1. an immediate replacement for .NET's older data-structures
a. use of
List<SomeType>
, and other generic data structures like Dictionaries, Queues, Stacks, etc. are more efficient, offer more internal functionality, and make the code you write inherently more strongly-typed, easier to understand, and maintain.
2. Generics enable you to more easily express, and use, complex, "compound" data-structures
a. consider this generic code data-structure declaration:
var listOfListOfInt = new List<List<List<int>>>();
It expresses simply the concept of ... working from the inside => out, a collection of Lists of integers, each member of which contains a collection of other Lists of integers, with each member of that (second-level) List of integers containing other Lists of integers. Yes, that's quite a mouthful to write :)
To bring that down-to-earth let's try: I have an auto-repair shop; each week I write a list containing all the money paid for each repair (rounded off to whole numbers, i.e., integers); each month I store each of the four weekly lists in a month list, and each year, I store the twelve monthly lists in a year-list: I think you get the idea.
Now consider what you would have to do to build such a data-structure before Generics
ArrayList al1 = new ArrayList();
ArrayList al2;
ArrayList al3;
for (int i = 0; i < 10; i++)
{
al2 = new ArrayList();
for (int j = 100; j > 90; j--)
{
al3 = new ArrayList();
for (int k = 200; k < 210; k++)
{
al3.Add(k);
}
al2.Add(al3);
}
al1.Add(al2);
}
Yes, you might say I'm cheating here in this comparison, because you can use multi-dimensional arrays in .NET., or "jagged" arrays (arrays-of-arrays), and those can be strongly-typed, where ArrayLists cannot be strongly-typed, and are only one-dimensional: internally an ArrayList uses a linked-list, and there are memory costs when you reach the limit of the number of entries in an ArrayList which triggers allocation of more memory for the ArrayList. And, with Arrays, you have to declare their size (each dimension's size) at the point you are going to start using one. With Arrays you cannot remove items, or dynamically re-allocate the number of items the Array can hold !
To sum it up:
List<int>
offers you a strongly-typed, flexibly variable length (you can add, and delete items, without a high overhead) storage structure that also offers many additional features, like range accessors and manipulators, etc. .NET's generic data structures usually offer better performance than their older counter-parts.
3. Generics enable you to use late-binding at run-time. So, if, based on the user's actions, at one point in time a List of integers is required, and, at another point in time a List of strings is required, you can write code using generics with "placeholder" Type declaration that can be used for both cases.
If I write a simple class using Generics:
public class GenericList<T> : List<T>
{
}
I can then do this:
List<int> intList = new GenericList<int>();
List<string> stringList = new GenericList<string>();
for (int i = 0; i < 10; i++)
{
intList.Add(i);
stringList.Add(i.ToString());
}
Admittedly, a trivial example, but I hope it gives you some ideas.
4. finally, Generics go, imho, hand-in-hand with Linq, and the synergy between those two powerful facilities, when you begin to grasp it, and become able to use it, is going to open-up a whole new dimension for you in programming "elegantly," and efficiently.
You might enjoy reading Eric Lippert's (he is one of Microsoft's most articulate guru-level .NET experts) blog entry on "Arrays:" [
^].