Last modified on 25 November 2010, at 00:53

.NET Development Foundation/Using generic collections


System types and collections: Using generic collections


Using generic collectionsEdit

Exam objective: Improve type safety and application performance in a .NET Framework application by using generic collections.

(Refer System.Collections.Generic namespace MSDN )

Collection.Generic interfacesEdit
Generic IComparable interface - MSDN
Note that IComparable<T> is a member of the System namespace.
You use this interface when you create a class and you want it to be used with generic types that support ordering (ex. SortedList<T> or List<T>.Sort()) without having to specify a comparer object. The only method of IComparable<T> is CompareTo<T>(T other). There is an example on MSDN.
The following example implements IComparable<T> for a custom made Point class. The example uses a List<T> instead of a SortedList<T> or OrderedDictionnary<T> because the comparaison is done based on the distance of the points from the origin which can give the same value for many points.
Simple use IComparable<T> (C#)

Simple use IComparable<T> (C#)

   using System;
   using System.Collections.Generic;
   using System.Text;
   namespace GenericsLab05
   {
       class Program
       {
           static void Main(string[] args)
           {
               List<Point> lst = new List<Point>();
               lst.Add(new Point(-2, -2));
               lst.Add(new Point(1, 1));
               lst.Add(new Point(2, 2));
               // Sort uses IComparable of Point
               lst.Sort();
               foreach (Point pt in lst)
               {
                   Console.WriteLine(pt.ToString());
               }
               // Wait to finish
               Console.WriteLine("Press ENTER to finish");
               Console.ReadLine();
           }
       }
       // This is out custom version of a point
       public struct Point : IComparable<Point>
       {
           public double x;
           public double y;
           public Point(double px, double py)
           {
               x = px;
               y = py;
           }
           // Comparaison done based on distance from origin
           public int CompareTo(Point other)
           {
               return Math.Sqrt(Math.Pow(x, 2) + Math.Pow(y, 2)).CompareTo
                   (Math.Sqrt(Math.Pow(other.x, 2) + Math.Pow(other.y, 2)));
           }
           public override string ToString()
           {
               return "(" + x.ToString() + "," + y.ToString() + ")";
           }
       }
   }
Generic ICollection interface and Generic IList interface
Generic ICollection interface - MSDN
Generic IList interface - MSDN
Generic IComparer interface and Generic IEqualityComparer interface
Generic IComparer interface - MSDN
Generic IEqualityComparer interface - MSDN
Generic IDictionary interface - MSDN
Generic IEnumerable interface and Generic IEnumerator interface
Generic IEnumerable interface - MSDN
see also ONDotnet
Generic IEnumerator interface - MSDN
IHashCodeProvider interface - MSDN - Interface is now obsolete (as of .NET 2.0)
Generic DictionaryEdit
Generic Dictionary class and Generic Dictionary.Enumerator structure
Generic Dictionary class - MSDN
Generic Dictionary.Enumerator structure - MSDN
Generic Dictionary.KeyCollection class and Dictionary.KeyCollection.Enumerator structure
Generic Dictionary.KeyCollection class - MSDN
Dictionary.KeyCollection.Enumerator structure - MSDN
Generic Dictionary.ValueCollection class and Dictionary.ValueCollection.Enumerator structure
Generic Dictionary.ValueCollection class - MSDN]
Dictionary.ValueCollection.Enumerator structure - MSDN]
Generic Comparer class and Generic EqualityComparer classEdit
Generic Comparer class - MSDN
The Comparer<T> class serves as a base class to easily implement the IComparer<T> interface.
The example is the same then for the IComparable<T> except that now a Comparer<T> derived object is given to the List<T>.Sort() method instead of implementing the IComparable<T> interface on Point.
This way of proceding has 2 advantages:
  • It can be used even if you dont have access to the source code of Point
  • You can have more than one Comparer derived class for the same Point class
Custom Comparer<T> (C#)

Custom Comparer<T> (C#)

   using System;
   using System.Collections.Generic;
   using System.Text;
   namespace GenericsLab06
   {
       class Program
       {
           static void Main(string[] args)
           {
               List<Point> lst = new List<Point>();
               lst.Add(new Point(-2, -2));
               lst.Add(new Point(1, 1));
               lst.Add(new Point(2, 2));
               // Sort uses IComparable of Point
               lst.Sort(new DistanceComparer());
               foreach (Point pt in lst)
               {
                   Console.WriteLine(pt.ToString());
               }
               // Wait to finish
               Console.WriteLine("Press ENTER to finish");
               Console.ReadLine();
           }
       }
       // This is out custom version of a point
       public struct Point 
       {
           public double x;
           public double y;
           public Point(double px, double py)
           {
               x = px;
               y = py;
           }
           public override string ToString()
           {
               return "(" + x.ToString() + "," + y.ToString() + ")";
           }
       }
       // Derive from base comparer class to implement IComparer<T>
       public class DistanceComparer : Comparer<Point>
       {
           public override int Compare(Point p1, Point p2)
           {
               return Math.Sqrt(Math.Pow(p1.x, 2) + Math.Pow(p1.y, 2)).CompareTo
                   (Math.Sqrt(Math.Pow(p2.x, 2) + Math.Pow(p2.y, 2)));
           }
       }
   }
Generic EqualityComparer class - MSDN
Generic KeyValuePair structureEdit
see MSDN
Generic List class, Generic List.Enumerator structure, and Generic SortedList classEdit
Generic List class - MSDN
A generic list class instance is simply declared using the List<T> syntax where T is the specific type.
Generic List.Enumerator structure - MSDN
Generic SortedList class - MSDN
Generic Queue class and Generic Queue.Enumerator structureEdit
Generic Queue class - MSDN
Generic Queue.Enumerator structure - MSDN
Generic SortedDictionary classEdit
See MSDN
For differences between SortedList and SortedDictionary are explained see MSDN
Generic LinkedListEdit
A Generic Linked List represents a doubly linked list and is a general-purpose linked list. It supports enumerators and implements the ICollection interface, consistent with other classes in the .NET Framework.
Generic LinkedList class - MSDN
Generic LinkedList.Enumerator structure - MSDN
Generic LinkedListNode class - MSDN
Generic Stack class and Generic Stack.Enumerator structureEdit
Generic Stack class - MSDN
Generic Stack.Enumerator structure - MSDN