One thing you need to understand, and which I haven't read in the existing answers yet, is that Collections and Generics have NOTHING to do with each other. They simply complement each other, but Collections can exist without Generics and Generics can exist without Collections.
A collection is simply a collection of whatever. Object, strings, integers, Persons, all of the above...
So what is a 'Generic'? From wikipedia:
"In the simplest definition, generic programming is a style of computer programming in which algorithms are written in terms of to-be-specified-later types that are then instantiated when needed for specific types provided as parameters."[
^].
That might still not be very simple. But basically a 'Generic' is a method to make something work with ANY type. The pro's to this are, as already mentioned, that you avoid 'boxing' and 'unboxing' and that your classes are type-safe.
For further reading on Generics:
Generics in the .NET Framework[
^]
Generics Explained (.NET Framework version 2.0)[
^]
.NET Generics in a Nutshell[
^]
So why is it so often mentioned with Collections? Well, think about it. Collections are very important in any application and therefore they are very much used. A Collection is exactly that, a collection. But a collection of what? Object? Of course. But what kind of Object? Wouldn't you like to know... In .NET 1.0 we DIDN'T know! Every collection was a collection of Objects and since everything in .NET is an Object a collection could contain just about anything. Since collections never do anything with the objects they store a collection really doesn't have to know anything about the object. So every type is allowed.
With Generics this is a lot easier. We have collections containing only strings, only integers, only your own custom class type. This, of course, makes it type-safe. We'll never try to cast a string to an integer ever again, because we know the collection contains only strings and not integers.
I like the VB syntax for Generics, since they are more readable than C# and actually imply what it does. The syntax is (Of T). So we could have a List(Of String). Clear, isn't it?
That said, Generics can be used on any Class you want to make (except Controls and Attributes and perhaps some other exceptions).
You can create a generic class or method simply by putting <T> begin it ((Of T) in VB).
public class Test<T>
{
public T SomeFunction() {
}
In this example SomeFunction returns something of type T. What is T? You don't know yet until you actually create an instance of the class.
Test<int> t1 = new Test<int>();
int i = t1.SomeFunction();
Test<Person> t2 = new Test<Person>();
string name = t2.SomeFunction().Name;
In the example above the problem lies of course in the implementation of SomeMethod because you don't know what T is and how you should return it. In any case, I hope the difference is clear and that you understand Collections and Generics are unrelated, just really complimentary.
ArrayList arrList = new ArrayList();
arrList.Add(1);
arrList.Add("Hello");
arrList.Add(new Person());
Object o = arrList(1);
List<string> list = new List<string>();
list.Add("Hello");
list.Add("Goodbye");
list.Add(new Person());
if (list(2).Contains("some string"))
{
}
Good luck!
<Shameless self-promotion>
P.S. If you are interested, I wrote an article on custom (Generic) Collections.
Having fun with custom collections![
^]
</Shameless self-promotion>