Exam 70-553 - Improve type safety and application performance in a .NET Framework application by using generic collections.
Here is the third installment of my notes on Exam 70-553:
Section 1
-
Part 1
-
Topic 3
-
Improve type safety and application performance in a .NET Framework application by using generic collections. (Refer System.Collections.Generic namespace)
- Collection.Generic interfaces
- Generic Dictionary
- Generic Comparer class and Generic EqualityComparer class
- Generic KeyValuePair structure
- Generic List class, Generic List.Enumerator structure, and Generic SortedList class
- Generic Queue class and Generic Queue.Enumerator structure
- Generic SortedDictionary class
- Generic LinkedList
- Generic Stack class and Generic Stack.Enumerator structure
Summary
There are a number of Generic Interfaces that correspond to interface mentioned in the previous section. They all have similar functionality with the ability to declare type during instantiation. The Interface include: Generic IComparable, Generic ICollection, Generic IList, Generic IComparer, Generic IDictionary, and Generic IEnumerable. Using any of these classes avoids the boxing and unboxing that occurs with the standard interface.
The following three samples come from the C# Generic Interfaces Programming guide mentioned in the resources:
Multiple interfaces can be specified as constraints on a single type, as follows:
Class Stack<T> where T : System.IComparable<T>, IEnumerable<T>
{
}
An interface can define more than one type parameter, as follows:
interface IDictionary<K, V>
{
}
The same rules of inheritance apply to interfaces as to classes:
interface IMonth<T> { }
interface IJanuary : IMonth<int> { } //No error
interface IFebruary<T> : IMonth<int> { } //No error
interface IMarch<T> : IMonth<T> { } //No error
//interface IApril<T> : IMonth<T, U> {} //Error
The Generic dictionary and the Generic Sorted Dictionary are like the regular dictionarys except you can strong type the key and the value. For example:
Dictionary<string, string> StringDictionary = new Dictionary<string, string>();
Would create a Dictionary instance that can only accept strings as its name value paris .
The Generic Comparer is designed for use with the SortedList and SortedDictionary Generic Classes. The types of the instances are constrained in that they must implement IComparable or the Generic IComparable Interfaces.
The Generic EqualityComparer Class is designed to override the Equals or GetHashCode methods of implementations of Generic Collections.
The Generic Key Value Pair is used by the Generic Dictionary class to define the type of key and type of value being stored in the Dictionary.
The Generic List and Sorted List are used to easily create a strongly typed list of objects that can be accessed by index. Below is an example of creating an instance of the List:
List<string> StringList = new List<string>();
The Generic Queue Class is used to easily create a strongly typed collection of first in first out objects.
The Generic Stack Class is used to easily create a strongly typed collection of last in first out objects.
The Generic List Enumerator structure, Generic Queue Enumerator structure, and Generic Stack Enumerator structure are used for creating a Generic Method that returns a Enumerator or implemnentation of IEnumerable. You specify the type when you use it. The List Structure is for use with the Generic List, the Queue structure is for use with the Generic Queue, and the Stack structure is for use with the Generic Stack. Don’t get confused by the fact that these are structure, all these classes implement the Enumerable methods like MoveNext and Current.
Other Resources & Links:
Generic Interfaces (C# Programming Guide)
http://msdn2.microsoft.com/en-us/library/kwtft8ak.aspx
Dictionary Generic Class
http://msdn2.microsoft.com/en-us/library/xfhwa508.aspx
Comparer Generic Class
http://msdn2.microsoft.com/en-us/library/cfttsh47(en-US,VS.80).aspx
KeyValuePair Generic Structure
http://msdn2.microsoft.com/en-us/library/5tbh8a42(en-US,VS.80).aspx
List Generic Class
http://msdn2.microsoft.com/en-us/library/6sh2ey19(en-US,VS.80).aspx
Queue Generic Class
http://msdn2.microsoft.com/en-us/library/7977ey2c(en-US,VS.80).aspx
Stack Generic Class
http://msdn2.microsoft.com/en-us/library/3278tedw(en-US,VS.80).aspx
Introducing .NET Generics
http://www.15seconds.com/issue/031024.htm
Generics (C# Programming Guide)
http://msdn2.microsoft.com/en-us/library/512aeb7t(en-US,VS.80).aspx
Generics for C# and .NET CLR
http://research.microsoft.com/projects/clrgen/