34,333
edits
Changes
no edit summary
The C# Collection classes are a set of classes designed specifically for grouping together objects and performing tasks on them. A number of collection classes are available with C# and we will be looking at the key classes in this chapter.
== Creating C# List Collections - List<T> and ArrayList ==
Both the List<T> and ArrayList classes have properties very similar to C# arrays (for details on arrays refer to [[Introducing C Sharp Arrays|Introducing C# Arrays]]. One key advantage of these classes over arrays is that they can grow and shrink as the number of stored objects changes.
List<''type''> ''name'' = new List<''type''>();
An ArrayList object is created in a similar manner, althoug without the ''type'' argument:
ArrayList ''name'' = new ArrayList();
With the above syntax in mind we can now create a List<T> object called ''colorList'':
<pre>
using System;
using System.Collections.Generic;
public class Lists
{
static void Main()
{
List<string> colorList = new List<string>();
}
}
</pre>
== Adding Items to Lists ==
Once a List object has been created there are a number of methods which may be called to perform tasks on the list. Once such method is the ''Add()'' method which, as the name suggests, is used to add items to the list object:
<pre>
List<string> colorList = new List<string>();
colorList.Add ("Red");
colorList.Add ("Green");
colorList.Add ("Yellow");
colorList.Add ("Purple");
colorList.Add ("Orange");
</pre>
== Accessing List Items ==
Individual items in a list may be accessed using the index value of the item (keeping in mind that the first item is index 0, the second index 1 and so on). The index value is placed in square brackets after the list name. For example, to access the second item in the ''colorList'' object:
<pre>
Console.WriteLine (colorList[1]);
</pre>
A list item value can similarly be changed using the index combined with the assignment operating. For example, to change the color from Yellow to Ingido:
<pre>
colorList[2] = "Indigo";
</pre>
All the items in a list may be accessed using a ''foreach'' loop. For example:
<pre>
foreach (string color in colorList)
{
Console.WriteLine ( color );
}
</pre>
When compiled and executed, the above code will output each of the color strings in the colorList object.
== Removing Items From Lists ==
Items may be removed from a list using the ''Remove()'' method. This method takes the value of the item to be removed as an argument. For example, to remove the "Red" string from the colorList object:
<pre>
colorList.Remove("Red");
</pre>
It is important to note that items in a list may be duplicated. In the case of duplicated items, the Remove() method will only remove the first matching instance.
== Sorting Lists in C# ==
There is no way to tell C# to automatically sort a list as items are added. If the items in a list are required to be always sorted into into order the ''Sort()'' method should be called after new items are added:
<pre>
colorList.Sort();
</pre>
== Finding Items in a C# List or ArrayList ==
A number of methods are provided with the List and ArrayList classes for the purposes of finding items. The most basic method is the ''Contains()'' method, which when called on a List or ArrayList object. The ''Contains()'' method returns ''true'' if the specified item is found in the list, or false if it is not.
The ''IndexOf()'' method returns the index value of a matching item in a list. For example, the following code sample will output the value 2, which is the index position of the "Yellow" string:
<pre>
List<string> colorList = new List<string>();
colorList.Add ("Red");
colorList.Add ("Green");
colorList.Add ("Yellow");
colorList.Add ("Purple");
colorList.Add ("Orange");
Console.WriteLine(colorList.IndexOf("Yellow"));
</pre>
If the item is not found in the List a value of -1 is returned by the ''IndexOf()'' method.
This technique could be used to replace a specific value with another. For example, without knowing in advance the index value of the "Yellow" string we can change to "Black":
<pre>
colorList[colorList.IndexOf("Yellow")] = "Black";
</pre>
The ''LastIndexOf()'' method returns the index value of the last item in the list to match the specified item. This is particularly useful when a list contains duplicate items.