Overview
- Allows you to use placeholders for parameters, which get defined later by the caller.
- Allows for strongly typed collections, which enable compile-time checking of types.
- Requires the developer to write generic code, which is valid for all types of T.
- Can be avoided by using constraints on allowed types for T, demonstrated below.
- You can only use one class as a constraint, but multiple interfaces.
Good practice
- Generics FAQ: Best Practices - MSDN document describing possible problems/solutions with generics.
Examples
Generic Placeholder
class MyHolder<T>
//where T: struct
//where T: MyNumber
//where T: class
//where T: class, IComparable
//where T: new()
{
private T _thing;
public T Thing
{
get { return this._thing; }
}
public MyHolder(T thing)
{
this._thing = thing;
}
}
MyHolder<int> nr = new MyHolder<int>(5);
MyHolder<string> name = new MyHolder<string>("CumpsD");
Console.WriteLine(nr.Thing);
Console.WriteLine(name.Thing);
You May Also Like

Exam 70-536 Study Notes
My study notes for Microsoft exam 70-536: .NET types, collections, generics, I/O, serialization and more, with C# examples.

Starting Exam 70-536 Study
After a burnout, I'm putting my own project on hold to study for Microsoft exam 70-536.

Design Patterns - Proxy Pattern
The Proxy Pattern, and the last article in the design patterns series, now that I've finished reading Head First Design Patterns.




