Can anybody help me where to start and what are the essential things to learn about collections (non-generics) and generics?
3 Answers
For a really brief explanation: Regular collections store objects. The system doesn't know what kind of object is stored, so you have to cast them to the desired type when you work with them. Generic collections declare what kind of object is being put in at the time you create it. Then you always know what is there. it's like the difference between an object array and a String array.
I would definitely check out the list of links on the page PK posted for a more thorough understanding.

- 2,891
- 30
- 37
I also recommend the following book which has pretty much all the details you could want on Generics in .NET 2.0 onwards, including Generic classes, methods, delegates and constraints, how they differ from C++ templates, and the generics in the BCL.

- 124,184
- 33
- 204
- 266
1) Classes can be defined with a generic type.
public class MyClass<TClass>
2) The types can be constrained using this syntax.
where TClass: struct
3) Methods also can gave generic types.
public TMethod ConvertTo<TMethod>()
4) Full Example
public class MyClass<TClass> where TClass: struct
{
private TClass _Instance;
public MyClass(TClass instance)
{
_Instance = instance;
}
public TMethod ConvertTo<TMethod>()
{
return (TMethod)Convert.ChangeType(_Instance, typeof(TMethod));
}
}

- 77,506
- 18
- 119
- 157
-
This is a good example of beginning generics, but should it be encouraged to reinvent the wheel with so many explanations out there? – Yuriy Faktorovich Aug 23 '09 at 14:30