.NET 2.0: Notes on Foundation Part 4 (C#)
Collections
Under System.Collections namespace are the following collections: ArrayList, SortedList, Queue, Stack, Hashtable, BitArray, StringCollection, StringDictionary, ListDictionary, HybridDictionary, and NameValueCollection.
ArrayList
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(s); arrayList.Add("World!!!"); arrayList.Add(100); arrayList.Add(new object()); // add multiple items string[] anArray = new string[] { "less", "is", "more" }; arrayList.AddRange(anArray); object[] anotherArray = new object[] { new object(), new ArrayList() }; arrayList.AddRange(anotherArray); // 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 arrayList.Add("Hello"); arrayList.Remove("Hello"); // remove first item in ArrayList arrayList.RemoveAt(0); // 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); arrayList.RemoveAt(index); } else { arrayList.Clear(); } |
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) { Console.WriteLine(coll[i]); } // using IEnumerator interface IEnumerator enumerator = coll.GetEnumerator(); while (enumerator.MoveNext()) { Console.WriteLine(enumerator.Current); } // using foreach loop foreach (object item in coll) { Console.WriteLine(item); } |
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()); |