In preparing for 70-536 I've been looking at the Interfaces in System.Collections
(that's quite dated since the generic variants been here for a while). Looking at the Exam objective:
Developing applications that use system types and collections (15 percent)
- Manage a group of associated data in a .NET Framework application by using collections.
May include but is not limited to: ArrayList class; Collection interfaces; Iterators; Hashtable class; CollectionBase class and ReadOnlyCollectionBase class; DictionaryBase class and DictionaryEntry class; Comparer class; Queue class; SortedList class; BitArray class; Stack class
I've bought Tony Northup's Self-Paced Training Kit and it doesn't even mention the Interfaces! If it's for a god reason I guess it's since in practical use the Collection Classes are used and not the Interfaces. I don't really think it's a god idea, since hopefully there's a reason for the Interfaces and one of these should be implementing own collection and Classes with collection functionality.
The MSDN Library on System.Collections
has god coverage but I'm missing two things - the Interfaces as code and a class diagram. I've looked and searched but not found none, not even at the Mono Project. Here I'll present my own Class Diagram and reconstruct the 8 Interfaces in System.Collections
from the available information. One last hiccup is that the MSDN Library declares Interfaces that are extended redundantly, fx. IList
implements ICollection
but IEnumerable
is also shown in the snip like:
public interface IList : ICollection, IEnumerable
This reduandancy confuses me in trying to understand the hierarchy, it sort of disrespects the hierarchy and suggest some form of composite pattern instead (or I just missing a point). It seems like most of the documentation for C# is used this way, but I'm not totally alone with this view, at least pdavila on the Manning maillist has raised the same concern.
A Class Diagram for System.Collections
With the help of Argouml and my missing skills with I was able to create something that resembles a correct Class Diagram for System.Collections
:
The one thing that strikes me the most is that the two Base Classes CollectionBase
and DictionaryBase
isn't used by the framework itself so why should I use it? Perhaps there is a god reason for it and maybe I'll even find it as i read thorugh Krzysztof Cwalina's blog.
The Interfaces as C# code
IEnumerable
The root interface carries the following description:
Exposes the enumerator, which supports a simple iteration over a non-generic collection.
1 namespace System.Collections
2 {
3 [ComVisibleAttribute(true)]
4 [GuidAttribute("496B0ABE-CDEE-11d3-88E8-00902754C43A")]
5 public interface IEnumerable
6 {
7 /* Methods */
8
9 // Returns an enumerator that iterates through a collection.
10 IEnumerator GetEnumerator();
11 }
12 }
ICollection
Next in the Hierarchy has:
Defines size, enumerators, and synchronization methods for all nongeneric collections.
1 namespace System.Collections
2 {
3 public interface ICollection : IEnumerable
4 {
5 /* Properties */
6
7 // Gets the number of elements contained in the ICollection.
8 int Count { get; }
9
10 // Gets a value indicating whether access to the ICollection is synchronized (thread safe).
11 bool IsSynchronized { get; }
12
13 // Gets an object that can be used to synchronize access to the ICollection.
14 Object SyncRoot { get; }
15
16
17 /* Methods */
18
19 // Copies the elements of the ICollection to an Array, starting at a particular Array index.
20 void CopyTo(Array array, int index);
21 }
22 }
IList
Here the class diagram splits in IList
that:
Represents a non-generic collection of objects that can be individually accessed by index.
1 namespace System.Collections
2 {
3 [ComVisibleAttribute(true)]
4 public interface IList : ICollection
5 {
6 /* Properties */
7
8 // Gets a value indicating whether the IList has a fixed size.
9 bool IsFixedSize { get; }
10
11 // Gets a value indicating whether the IList is read-only.
12 bool IsReadOnly { get; }
13
14 // Gets or sets the element at the specified index.
15 Object Item[int index] { get; set; }
16
17
18 /* Methods */
19
20 // Adds an item to the IList.
21 int Add(Object value);
22
23 // Removes all items from the IList.
24 void Clear();
25
26 // Determines whether the IList contains a specific value.
27 bool Contains(Object value);
28
29 // Determines the index of a specific item in the IList.
30 int IndexOf(Object value);
31
32 // Inserts an item to the IList at the specified index.
33 void Insert(int index, Object value);
34
35 // Removes the first occurrence of a specific object from the IList.
36 void Remove(Object value);
37
38 // Removes the IList item at the specified index.
39 void RemoveAt(int index);
40 }
41 }
IDictionary
The other branch is IDictionary
that:
Represents a nongeneric collection of key/value pairs.
1 namespace System.Collections
2 {
3 [ComVisibleAttribute(true)]
4 public interface IDictionary : ICollection
5 {
6 /* Properties */
7
8 //Gets a value indicating whether the IDictionary object has a fixed size.
9 bool IsFixedSize { get; }
10
11 // Gets a value indicating whether the IDictionary object is read-only.
12 bool IsReadOnly { get; }
13
14 // Gets or sets the element with the specified key.
15 Object Item[Object key] { get; set; }
16
17 // Gets an ICollection object containing the keys of the IDictionary object.
18 ICollection Keys { get; }
19
20 // Gets an ICollection object containing the values in the IDictionary object.
21 ICollection Values { get; }
22
23
24 /* Methods */
25
26 // Adds an element with the provided key and value to the IDictionary object.
27 void Add(Object key, Object value);
28
29 // Removes all elements from the IDictionary object.
30 void Clear();
31
32 // Determines whether the IDictionary object contains an element with the specified key.
33 bool Contains(Object key);
34
35 // Overloaded. Returns an IDictionaryEnumerator object for the IDictionary object.
36 IDictionaryEnumerator GetEnumerator();
37
38 // Removes the element with the specified key from the IDictionary object.
39 void Remove(Object key);
40 }
41 }
IEnumerator
To iterate through a Collection there's an IEnumerator
similar to the Java Iterator
Supports a simple iteration over a nongeneric collection.
1 namespace System.Collections
2 {
3 [ComVisibleAttribute(true)]
4 [GuidAttribute("496B0ABF-CDEE-11d3-88E8-00902754C43A")]
5 public interface IEnumerator
6 {
7 /* Properties */
8
9 //Gets the current element in the collection.
10 Object Current { get; }
11
12
13 /* Methods */
14
15 // Advances the enumerator to the next element of the collection.
16 bool MoveNext();
17
18 // Sets the enumerator to its initial position, which is before the first element in the collection.
19 void Reset();
20 }
21 }
IDictionaryEnumerator
The pendant for Dictionaries (Java Map
) are an IDictionaryEnumerator
:
Enumerates the elements of a nongeneric dictionary.
1 namespace System.Collections
2 {
3 [ComVisibleAttribute(true)]
4 public interface IDictionaryEnumerator : IEnumerator
5 {
6 /* Properties */
7
8 // Gets both the key and the value of the current dictionary entry.
9 DictionaryEntry Entry { get; }
10
11 // Gets the key of the current dictionary entry.
12 Object Key { get; }
13
14 // Gets the value of the current dictionary entry.
15 Object Value { get; }
16 }
17 }
IComparer
There's two utility Interface where the first is for sorting:
Exposes a method that compares two objects.
1 namespace System.Collections
2 {
3 // Exposes a method that compares two objects.
4 [ComVisibleAttribute(true)]
5 public interface IComparer
6 {
7 /* Methods */
8
9 // Compares two objects and returns a value indicating whether one is less than, equal to, or greater than the other.
10 int Compare(Object x, Object y);
11 }
12 }
IEqualityComparer
And lastly the IEqualityComparer
for searching:
Defines methods to support the comparison of objects for equality.
1 namespace System.Collections
2 {
3 //Defines methods to support the comparison of objects for equality.
4 [ComVisibleAttribute(true)]
5 public interface IEqualityComparer
6 {
7 /* Methods */
8
9 // Determines whether the specified objects are equal.
10 bool Equals(Object x, Object y);
11
12 // Returns a hash code for the specified object.
13 int GetHashCode(Object obj);
14 }
15 }