C # Introduction to various languages

Source: Internet
Author: User

A set of objects that can be accessed by traversing each element (especially through the foreach loop)
A collection contains multiple elements, that 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, the returned IEnumerator object is neither a collection class object nor a collection element class object. It is an independent class object. This object allows you to traverse every element object in the Collection class object.

If a collection class is a user-defined collection class, you must implement its GetEnumerator () method. Otherwise, loops cannot be used. Of course, the IEnumerator class corresponding to this custom collection class (the class implementing this interface) also needs to be customized.

For example, the IEnumerator of the ArrayList collection class is ArrayListEnumeratorSimple.
The IEnumerator corresponding to the Array collection class is SZArrayEnumerator.
(These two classes are not described in the. net framework class library documentation (msdn)

1. The interfaces in System. Colloctions that indicate the behavior of a set include:
1) ICollection
Defines the size, enumerative number, and synchronization method of all sets. Derived from IEnumerable
It defines the most basic behavior of the Collection class, and all the collection classes implement this interface (basic interface)
However, its behavior is too basic: it is mainly a Count attribute. It doesn't make much sense to implement it independently.

2) IEnumerable
Public enumeration number, which supports simple iteration on the set
It only has one GetEnumerator () method. This method can return an IEnumerator interface through which the set can be traversed.
Basically, all the collection classes have implemented this interface.

3) IList
IList is a set of values that can be sorted and accessed by indexes. It implements the ICollection and IEnumerable interfaces.
Is the abstract base class of all lists. There are three types of IList implementation: Read-only, fixed size, and variable size.

4) IDictionary
IDictionary is a set of key/value pairs. It implements the ICollection and IEnumerable interfaces.
Is the base interface of the set of key/value pairs. There are three types of IDictionary implementations: Read-only, fixed size, and variable size.
IDictionary can be called a dictionary, ing, or hash. It accesses values based on keys (any type ).


2. The collection classes that can be directly used in System. Collections include:
1) ArrayList
Implemented interfaces: IList, ICollection, and IEnumerable
As long as the set is not modified, ArrayList supports multiple readers at the same time safely.
With the addition of elements to the ArrayList, the capacity increases automatically by reallocation as needed (2 times increase)
If you need to create an object array but do not know the size of the array in advance, you can use ArrayList
ArrayList references all elements as object objects. Therefore, type conversion is required when accessing the elements of ArrayList.
Advantages: dynamic resizing, flexible and convenient insertion and deletion of elements, and sorting
Disadvantage: The insertion performance is not as good as that of an array or a strong type.

2) BitArray
Implemented interfaces: ICollection and IEnumerable
Manage the compressed array of bits.

3) Hashtable
Implemented interfaces: IDictionary, ICollection, and IEnumerable
You can add or delete elements to or from Hashtable. Some elements are like ArrayList, but they do not have such high performance overhead.

4) SortedList
Implemented interfaces: IDictionary, ICollection, and IEnumerable
SortedLIst combines the advantages of ArrayList and Hashtable, and can be sorted by pressing values.

5) Queue
Implemented interfaces: ICollection and IEnumerable
Queque is a queue for first-in-first-out access to various elements.
You can call the GetEnumerator () method of the Queque object to obtain the IEnumerator object and traverse each element in the queue.

6) Stack
Implemented interfaces: ICollection and IEnumerable
A Stack is a Stack, followed by first-in-first-out access to each element.
You can call the GetEnumerator () method of the Stack object to obtain the IEnumerator object and traverse each element in the Stack.


3. The collection classes mentioned above are all generic collection classes, and most of the elements they accept are Object types. when an Object is put
After the set, all the original type information is lost-that is, these general set classes are not strongly typed
The solution is to use a strongly typed collection class.
CollectionBase, DictionaryBase, and ReadOnlyCollectionBase in the System. Collections namespace
Some classes in the System. Collections. Specialized namespace can meet the requirements and can be directly used or inherited.

 

4. Set Performance

Many collection classes provide the same functions. For example, SortedList and SortedDictionary have almost identical functions. However, its performance is often quite different. One set uses less memory, and the element retrieval speed of the other set is fast. In the MSDN document, collection methods often have performance prompts: O (1), and the time is the same as the operation time. O (log n)

As the number of elements increases in the Set, the time required for each element is not linear, but a logarithm curve.

Set Add Insert Remove Item Sort Find

List <T> If the set must reset the size of O (n) O (1) O (n log n), O (n)

That is, O (1) or O (n) Worst Case O (n ^ 2)

Stack <T> Push (). If it is narrow, you must reset Pop (), O (1)

The size is O (1) or O (n)

Queue <T> Enqueue (). If the Queue must be Dequeue (),

Reset size, that is, O (1) or O (n) O (1)

HashSet <T> If the set size must be reset, Add () O (1)

Is O (1) or O (n) O (1) or O (n)

Counter list <T> AddLast () AddAfter () O (1) O (n)

Dictionary O (1) O (1) O (1)

<TKey, TValue>

SortedDictionary

<TKey, TValue> O (log n)

SortedList O (n) read/write is O (log n), if

<TKey, Tvalue> the key is in the list, that is, O (logn );

If the key is not in the list, it is O (n)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.