As I mentioned previously I’m educating my team on the new 2005/2.0 stuff, and extending that here. This is not new information, but as they say on TV during the summer, “if you haven’t seen it, it’s new to you”. (and if it’s not new to you, and I get it totally wrong, please tell me. I’m still learning this stuff too.)
One of the new features of C# in 2.0 is what’s called “Generics”. What is a generic? Essentially it boils down to generalization in design.
Think about a collection of some object. In a structure such as a Linked List. Very simply you have a data structure with n number of nodes containing data and a reference to the next node. (this isn’t about linked lists, click through above to learn more about one of the most basic data structures in computing)
Okay, sounds pretty much like a collection with a Next() function yeah? Right. Is the type of data on every node important to the structure? Meaning does the data structure “linked list” need to know that it’s a linked list of ints, or strings, or Objects, or YourCustomTypes? No…that’s silly. Data should be separate from implementation right? There’s your generic.
In the past in .Net you had to fall back on explicitly up– and down-casting items in a collection to achieve generality of design, creating boxing and unboxing nightmares. Even when you didn’t think about it, like using an ArrayList, you were boxing like George Foreman. Or even worse you just had a ton of strongly-typed collections to keep track of. Too much specificity to even begin to be extensible or maintainable. But it’s how you had to do it.
So in 2.0, we have the Generic. What is it? It’s <T>. T is the “type identifier” for a generic data structure. It separates the data from the implementation. Let’s look at a little code so it’s not foreign to you.
public class MyGenericCollection<T>
{
public void Add(T t)
{
//add one to the collection. note use of T as the type of the parameter
}
// don’t really feel like implementing a whole thing here so…
}
Okay. What do we have? We have a custom collection class declaration…looks normal…then the <T>. That just means “hey, I am basically a template (yeah…template c++ guys) that can be used with any type”. Okay, good, got it. Now, down to the Add() method. The T shows up again in the parameter list. This time, it’s saying “whatever that <T> guy up there was…that’s what I’m looking for.”
Now, you have a generic collection. To use it, you could say:
MyGenericCollection<int> ints = new MyGenericCollection<int>();
or
MyGenericCollection<string> strings = new MyGenericCollection<string>();
or (here comes the part that saves us some actual code)
MyGenericCollection<MyCustomType> theseThings = new MyGenericCollection<MyCustomType>();
“Ok, great. We have a way to create our own custom generic types. I’m not really going to do that too much, so what’s in it for me?” Yeah, okay…not everyone has a need to create generic types. So why is the feature important to you? System.Collections.Generic (use MSDN2 for 2.0 stuff). That’s why. Now in 2.0 there’s a generic structure for many collection types that you can use every day. Instead of using the boxariffic ArrayList use a List<T>. A generic structure for an indexed sortable collection. We also have a LinkedList<T> (doubly-linked), a Queue<T> (FIFO collection), a Stack<T> (LIFO), a Dictionary<T> for key/value collections, and so on. These are all going to be more performant than your old ArrayList.
Okay…there’s Generics in a nutshell. There’s more to them, but it’s more advanced than you really need to get started using generics, and if you need to know more, there’s plenty of documentation. Stay tuned for more C# 2.0 language features.
Tags:
Code .Net