Valence collection
There is no doubt that the most typical application of the model is too much set. A model version of the existing collection classes and interfaces is available in .NET 2.0, they are located in System.Collections.Generic namespace.
The new model set class in .NET 2.0 is not simple in the design of the existing non-flavoring set class. The design of the new model set class fully absorbs reasonably in the design and abandoned some unreasonable, and introduces new design for norms. Therefore, the design of new model classes and interfaces should be more reasonable and effective, but .Net programmers need some time to learn new design and understand what is different from the designed design, and in the unfair Compatibility issues that may occur when transplanted into a set set.
Below is a complimentary set and an existing non-flavorful collection of comparables:
Non-extinguished interface model interface non-flavor type Model class ienumerator ienumerator
ArrayList List
Ienumerable IEnumerable
Stack Stack
Icollection iCollection
Queue Queue
IList IList
DictionaryTry KeyValuePair
IDictionary iDictionary
Hashtable Dictionary
IComparable iComparable
Comparer Comparer
IComparer iComparer
It can be seen that some of the names of the class have modified, such as ArrayList now changed to List
Hashtable change to Dictionary
DictionaryEntry change to KeyValuepair
and many more. This name is of course more reasonable (because IList
Is the interface, List
Is the specific class; the same Idictionary
Is interface, Dictionary
It is the specific class; while KeyValuePair
It is obviously more easily understood and memorized than DictionaryEntry), but it may be a bit not looking for the north of the next programmer that has been accustomed to the previous name.
As mentioned earlier, the new model set interface / class and the previous non-flavorful version have a large design change, let's take a look at these changes.
Ienumerator
Ienumerator / IEnumerator
The interface allows traversal to a collection, mainly used in the .NET programming language traversal statement, such as a C # foreach statement. User code usually does not use this interface directly. Ienumerator
Reset methods have been removed compared to the non-flavoring version IENUMERATOR. This may be due to the following considerations:
l iumerator
The interface is mainly designed to support a statement such as Foreach, and these places cannot be used in the reset method. Remove the reset method makes the design more simplified and reduces difficulty in achieving the interface. If the caller needs to be similar to the RESET function, you can re-get an enumerator (eg, by calling the GetENUMERATOR method).
l C # 2.0 Iterators provides a method of automatically generating an enumerator, and the compiler automatically implements the IENUMERATOR interface for the specified class and the IEnumerator
It is very natural and reasonable to remove the reset method.
Icollection
Icollection
The interface design and non-flavoring version of Icollections have changed significantly. The initial design intent of the ICollection interface is to support replication set elements (by count attributes and copyto methods), and support synchronous access mode (via issynchronized attributes and syncroot properties). Icollection
The design retains support for replication set elements, but abandoned support for synchronous access modes because practices prove that the synchronous access mode of Icollection is confusing and inefficient. Many programs that have just learned .NET don't understand what Syncroot is something, what is used. In addition, from performance and logical consideration, when the lock set should be determined by the caller, not by the implementation. So always Issynchronized and Syncroot are not very ideal. Therefore, Icollection
There is no Issynchronized property and syncroot properties.
In addition to this, iCollection
Add some new properties and methods, they make iCollection
The interface becomes more useful. These attributes and methods are actually transplanted from the common attributes and methods of ILIST and IDictionary, including:
l IsReadonly, used to determine if a collection is read-only.
l Add / remove / clear, used to manage set elements. These methods are valid for the list and dictionary.
l Contains, used to determine whether or not the specified value is included in the collection.
In addition, there is a similar IREADOONLYCOLLECTION
Such an interface may make sense, it only needs to support the count property, the COPYTO method, and the Contains method. However, Microsoft did not use such a design, the main reason is to make the basic set interface as simple as possible as possible. Microsoft's suggestion is what the programmer needs to define such an interface.
IList
And List
Just mentioned, IList
Variations relative to ILIST are common attributes and methods being transplanted into ICOLLECTION
Only index access-based attributes and methods based on the list are left.
List
Very large design changes are also made relative to ArrayList. The main considerations for making these changes are performance, because the dynamic array is one of the most basic data structures used by .NET program, its performance affects the global in the application. For example, before the ARRAYLIST default Capacity is 16, while List
The default Capacity is 4, which minus the work set of the application. In addition, List
The method is not a virtual method (ArrayList method is a virtual method), which can utilize function inline to improve performance (virtual function is not in line). List
There is also a lot of SYNCHRONIZED synchronous access modes. List
An important feature added in ArrayList is support for Functional Programming. We will introduce this new feature in the Functional Programming section.
IDictionary
And Dictionary
IDictionary
And Dictionary
Compared to the non-flavoring version a big change is the processing logic of the indexer when the specified key does not exist. For iDictionary and hashtable, the value of the value is Object. When the key does not exist, the indexer will return null, and if the key is present, the corresponding value is NULL, and the designer may think that the caller is usually concerned. Is it valid, not a distinction between these two situations). However, for the model version, because storage may be a value type, it is not possible to return NULL to be identified as a key. Therefore, iDictionary
And Dictionary
The indexer will throw the keyNotFoundException in the case where the specified key does not exist. This will lead to incompatibility of the source code level, that is, the following code will not be portable in the case of the stored value, but must be rewritten (for example, use the Containskey method to determine if the specified key exists, then access; or use it Throw an exception's TrygetValue method):
Hashtable map = ...
IF (MAP ["S1"] == NULL) {// If it is a model version will throw an exception rather than return NULL
...
}
This problem reflects that the designer is not very thoughtful when the designer initially designed the HashTable class - uses the magic value null, which can be a case where the button does not exist, or the key to the value of null, And this point is not established. In addition, from the perspective of Design By Contract, when the specified key value does not exist, the exception is a natural thing (unrelated to whether or not using the model), it is like an array of crosses. It is estimated that the original designer mainly uses NULL instead of exception processing from performance angle considerations.
IComparable
, IComparer
Comparer
These interfaces / class are used for comparison and sorting. IComparable
It is of course to minimize Boxing compared to iComparable. IComparer
Relative IComparer not only adds the equals method, but also adds the GetHashCode method. Let's see and relatively related, but in fact, for strings, comparison and haveh values are close. In previous non-graded designs, there is a need to use both ICOMPARER and IHASHCODEPROVIDER two interfaces, such as HashTable constructor:
Public HashTable (ihashcodeProvider HCP, IComparer Comparer);
The two parameters of IHashCodeProvider and IComparer must match (for example, INVARIANTCULTUREIGNORECASE), otherwise the result will be incorrect. In order to allow programmers to quickly write the correct code, IComparer
Constructor:
Public Dictionary (iComparer
Comparer);
Comparer
Icomparer
Default implementation, Microsoft's latest design guide recommended COMPARER
Instead of other methods to compare and sort.
In addition, .NET 2.0 newly adds a string comparison class - StringComparer, located in the System namespace. StringComparer is not a model class, but it implements iComparer
The interface is very useful for strings that need to be sized. For example, the following code creates a single-write dictionary:
Dictionary
Dict = New Dictionary
StringComparer.invariantcultureIgnorecase;
DICT [Test "] = 10;
INT N = DICT ["Test"];