C# has a number of classes whch are containers for data. These are referred to as "collections".
Some collections are not strongly typed and can contains objects of various types in general. Others are strongly typed.
The foreach construct can be used with any of them, as they implement either the IEnumerable interface or the IEnumerable<T> interface.
The strongly-typed collections are in the System.Collections.Generic namespace. These all use one or more parameters such as <T>.
The weakly-typed collections are in the System.Collections namespace, and they store elements as type object (the lowest common denominator).
Any of the collections can be copied to a suitable array using the CopyTo method.
The collections will resize themselves, growing or shrinking as appropriate (although the ArrayList collection has an option to make it of a fixed size).
These notes are primarily about some of the strongly-typed collections and are by no means complete.
List<T> class
This is in the System.Collections.Generic namespace.
This is a strongly-typed list of items of type T. It can be regarded as a generalization of an array.
A List<T> instance will resize itself as needed.
For some features, we need the type we supply as T to implement the IComparer<T> interface.
There are far too many features to list. Here are some:
Constructors
Properties
Methods (here are a few out of a vast number)
Example adapted from a MSDN page:
// Create a list of strings. var salmons = new List<string>(); salmons.Add("chinook"); salmons.Add("coho"); salmons.Add("pink"); salmons.Add("sockeye"); // Iterate through the list. foreach (var salmon in salmons) { Console.Write(salmon + " "); } // Output: chinook coho pink sockeye // Create a list of strings by using a collection initializer. var salmons = new List<string> { "chinook", "coho", "pink", "sockeye" }; // Iterate through the list. foreach (var salmon in salmons) { Console.Write(salmon + " "); } // Output: chinook coho pink sockeye
You can use a for statement instead of a foreach statement to iterate through a collection. You accomplish this by accessing the collection elements by the index position. The index of the elements starts at 0 and ends at the value count - 1.
The following example iterates through the elements of a collection by using for instead of foreach.
// Create a list of strings by using a collection initializer. var salmons = new List<string> { "chinook", "coho", "pink", "sockeye" }; for (var index = 0; index < salmons.Count; index++) { Console.Write(salmons[index] + " "); } // Output: chinook coho pink sockeye // Create a list of strings by using a collection initializer. var salmons = new List<string> { "chinook", "coho", "pink", "sockeye" }; // Remove an element from the list by specifying the object. salmons.Remove("coho"); // Iterate through the list. foreach (var salmon in salmons) { Console.Write(salmon + " "); } // Output: chinook pink sockeye
Other strongly-typed collections
Among others we have:
This does about what one would expect:
Constructors
Properties
Methods (a few of the many)
This does about what one would expect:
Constructors
Properties
Methods (a few of the many)
Notice that this uses two types; the key and value need not be of the same type.
This will store pairs (key, value). If we have a key, retrieval of the the corresponding value is fast, searching by means of a hash table.
It has various constructors, properties and methods. For instance, we have
This stores pairs (key, value), kept in order by using the corresponding IComparer<TKey> implementation. (That is, the type we specify for TKey must implement the IComparer<T> interface.)
It has various constructors, properties and methods.
Weakly-Typed Collections
These are in the Systems.Collections namespace and store items of type object. A few of them are: