A collection that represents a set of objects that can be accessed by traversing each element (in particular, it can be accessed using foreach loop) A collection contains multiple elements, i.e. there is a collection class object and N element objects
Because any collection class implements the IEnumerable interface, any collection class object has a GetEnumerator() method, which can return an object that implements the IEnumerator interface, which is neither a collection class object nor a collection element class object, it is an independent class object. This object allows you to iterate through each element object in the collection class object
If the collection class is a user-defined collection class, the user must implement its GetEnumerator() method, otherwise loops cannot be used. Of course, the IEnumerator class corresponding to this custom collection class (the class that implements the interface) must also be customized
For example, the IEnumerator corresponding to the ArrayList collection class is ArrayListEnumeratorSimple The IEnumerator corresponding to the Array collection class is SZArrayEnumerator (Neither class is covered in the .NET Framework Class Library documentation (MSDN))
1. The interfaces in System.Colloctions that represent the behavior of the collection are: 1)ICollection Define the size, number of enums, and synchronization methods for all collections. Derived from IEnumerable It defines the most basic behavior of the collection class, and all collection classes implement this interface (base interface) But its behavior is too basic: it is mainly a Count property, and it doesn't make much sense to implement it alone
2)IEnumerable Expose an enumeration that supports simple iterations on a set It has only one method, GetEnumerator(), which returns an IEnumerator interface through which it can traverse the collection Basically all collection classes implement this interface
3)IList The IList implementation is a collection of values that are sortable and can be accessed by index for its members, and it itself implements the ICollection and IEnumerable interfaces is the abstract base class for all lists. There are three categories of IList implementations: read-only, fixed-size, and variable-size.
4)IDictionary The IDictionary implementation is a collection of key/value pairs that itself implements the ICollection and IEnumerable interfaces is the base interface of a collection of key/value pairs. There are three categories of IDictionary implementations: read-only, fixed-size, and variable-size. IDictionary can be called a dictionary, map, or shash list, and it accesses values based on keys (of any type).
2. The collection classes that can be used directly in System.Collections are: 1)ArrayList Implement interfaces: IList, ICollection, IEnumerable As long as the collection is not modified, ArrayList can safely support multiple readers at the same time As elements are added to the ArrayList, the capacity is automatically increased on demand by reallocating (2x increase) If you need to create an array of objects, but you don't know the size of the array beforehand, you can use ArrayList ArrayList refers to all elements as objects, so it needs to be typed when accessing elements in ArrayList Pros: Dynamically change the size, flexible and convenient insertion and removal of elements, sortability Disadvantages: Insertion performance is not as good as arrays, not strong types
2)BitArray Implement interfaces: ICollection and IEnumerable Manage compressed arrays of bit values.
3)Hashtable Implement interfaces: IDictionary, ICollection, IEnumerable You can freely add and remove elements to the Hashtable, some like ArrayList, but without the performance overhead
4)SortedList Implement interfaces: IDictionary, ICollection, IEnumerable SortedLIst combines the advantages of ArrayList and Hashtable, and can be sorted by key value
5)Queue Implement interfaces: ICollection and IEnumerable Queques are queues, first-in, first-out access to individual elements You can call the GetEnumerator() method of the Queque object to get the IEnumerator object to iterate through the elements in the queue
6)Stack Implement interfaces: ICollection and IEnumerable A stack is a stack that accesses individual elements on a last-in, first-out basis You can call the GetEnumerator() method of the Stack object to get the IEnumerator object to iterate through the elements in the stack
3. The collection classes mentioned above are all general collection classes, and most of the elements they accept are of the Object type, when the object is put in After the collection, the original type information is lost - that is, these general collection classes are not strongly typed The workaround is to use strongly typed collection classes System.Collections namespace System.Collections.Specialized namespace Some classes can meet the requirements and can be used directly or inherited
|