Julio Henriquez

hi,

Im new to the C# Programming Language and i would like to
know how to use Collections on c#.

to be a little more specific, i want to make a collection of objects
of a class i made.

can someone give me a hand

thx!




Re: Visual C# Language C# Collections

James Curran

Given

class MyClass {}

There are a number of different collections that could be made:

An Array:

MyClass[] myclasses = new MyClass[size];

This is the only type-specific collection available in .Net 1.1.

For .Net 2.0 or later, we can use generics:

A List:

List<MyClass> myclasses = new List<MyClass>();

A Stack:

Stack<MyCLass> myclasses = new Stack<MyCLass>();

A Queue:

Queue<MyCLass> myclasses = new Queue<MyClass>();

Each type has it's own uses, so to explain more, you'd need to know what you want to do with it.






Re: Visual C# Language C# Collections

Sean Fowler

I find List<T> is the one I use the most.

The other one I use a lot is Dictionary<T, U>. This is like List<T>, except when you add an object you give it a key. This makes it a lot easier to retrieve a specific object from the collection, you just pass in the key and it returns the object.

A more advanced version of the Dictionary is KeyedCollection<T, U>. It's similar to the Dictionary in that it takes a key, but the difference is that it knows how to work out the key from the object you're adding. But that's something to look at further down the line, for now I'd just use the Dictionary.

Sean