List<T> is a class with a type parameter. This is called "generics" and allows you to easily manipulate objects within a class, especially useful for container classes such as a list or a queue.
A container simply stores things, it does not need to know what it stores. We could implement this without generics:
class List { public List( ) { } public void Add( object toAdd ) { } public void Remove( object toRemove ) { } public object operator []( int index ) { } }
However, we do not have type safety. I could abuse the hell from this collection as follows:
List list = new List( ); list.Add( 1 ); list.Add( "uh oh" ); list.Add( 2 ); int i = (int)list[1];
Using generics in C # allows us to use these types of container classes in a safe manner.
class List<T> {
Now, if we try to add something that does not belong, we get a compile-time error, much preferable to an error that occurs when our program is executed.
List<int> list = new List<int>( ); list.Add( 1 );
Hope this helps. Sorry if I made any syntax errors there, my C # is rusty;)
source share