.NET 2.0: Notes on Foundation Part 4 (C#)


Under System.Collections namespace are the following collections: ArrayList, SortedList, Queue, Stack, Hashtable, BitArray, StringCollection, StringDictionary, ListDictionary, HybridDictionary, and NameValueCollection.



ArrayList is the most basic of all collections.  It is a simple resizeable, index-based collections of objects.

ArrayList arrayList = new ArrayList();

// add individual items
string s = "Hello";
arrayList.Add(new object());

// add multiple items
string[] anArray = new string[] { "less", "is", "more" };
object[] anotherArray = new object[] { new object(), new ArrayList() };

// insert item at specific position
arrayList.Insert(3, "Hi All");

// insert multiple items at specific position
string[] moreStrings = new string[] { "good morning", "nice to meet you" };
arrayList.InsertRange(4, moreStrings);

// using indexer to set specific item
arrayList[3] = "Hi All";

// remove item

// remove first item in ArrayList

// remove first four items in ArrayList
arrayList.RemoveRange(0, 4);

// using Contains(), IndexOf(), and Clear() methods
string myString = "My String";
if (arrayList.Contains(myString))
  int index = arrayList.IndexOf(myString);


Iterating Over Items In Collections

To iterate items in a collection such as the ArrayList, you can use either the for loop, IEnumerator interface, or the foreach loop.

// using for loop
for (int i = 0; i < coll.Count; ++i)

// using IEnumerator interface
IEnumerator enumerator = coll.GetEnumerator();
while (enumerator.MoveNext())

// using foreach loop
foreach (object item in coll)


Interfaces in Collections

.NET supports the following interfaces that are implemented by collections: IEnumerable, ICollection, and IList

IEnumerable interface is used to provide a common way to iterate over a collection.  Its important properties and methods are: Current, MoveNext(), and Reset().

ICollection is derived from IEnumerable and is used to provide a common way to get the items in the collection as well as to copy the collection to an Array object.  Its important properties and methods are: Count, and CopyTo().

IList is derived from ICollection and is used to provide a common way to expose lists of items.  Its important properties and methods are: Item, Add(), Clear(), Contains(), IndexOf(), Insert(), Remove(), and RemoveAt().


Sorting Items In Collections

To sort items in an ArrayList, you use the Sort() method.  The Sort() method uses the Comparer class which is a default implementation that supports the IComparer interface.

To specify an IComparer object to use instead of the default, you use the overloaded Sort() method that takes in an IComparer object.

coll.Sort(new CaseInsensitiveComparer());

You can also write your own comparer by implementing the Compare() method of the IComparer interface.

public class DescendingComparer : IComparer
  CaseInsensitiveComparer _comparer = new CaseInsensitiveComparer();

  public int Compare(object x, object y)
    // reversing the compared objects to get descending comparisons
    return _comparer.Compare(y, x);

coll.Sort(new DescendingComparer());


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s