About. NET Collection Summary _ Practical skills

Source: Internet
Author: User
Tags hash versions

A set is a collection of independent data items that have common characteristics, and we can use the same calling code to handle all the elements of a collection without having to deal with each individual item individually. The collection of. Net such as the System.Array class and System.Collections namespaces) arrays, lists, queues, stacks, hash tables, dictionaries, and even (System.Data) datasets, DataTable, There are also generic versions of the collections added in 2.0 (System.Collections.Generic and System.Collections.ObjectModel), a collection of valid thread-safe operations introduced in 4.0 (System.Collections.Concurrent).

In the face of so many collections, do you know what the advantages of each collection are, and which set is used in a particular scenario? This article attempts to explore this issue, generalities, does not involve in-depth memory data structure of the investigation, hoping to bring some benefits to everyone.

Collection interface
Before we discuss the various sets separately, we discuss the commonality of the set and the inheritance hierarchy of the whole set.

The ICollection interface is the base interface for classes in the System.Collections namespace, and the corresponding icollection<t> is the base interface for all generic version collections. All of the collection classes inherit directly or indirectly from them.

ICollection also inherits IEnumerable to provide convenient enumeration functionality, but it is more noteworthy that ICollection provides synchronous access to thread security controls:

IsSynchronized: Gets a value that indicates whether access to ICollection is synchronized (thread-safe).


SyncRoot: Gets the object that can be used to synchronize access to ICollection.

For example, we can make thread-safe access to the collection by coming down, but some collections provide synchronized methods to provide encapsulation of the thread-safe collection.

Copy Code code as follows:

ICollection mycollection = somecollection;
Lock (Mycollection.syncroot)
{
Insert your code here.
}

However, the collection is not thread-safe by default. If you need scalable and efficient multithreaded access to the collection, use a class in the System.Collections.Concurrent namespace.

Unlike a non-generic version, a generic version of a collection implements a Non-generic interface, in addition to the generic interface. Such as Icollection<t> implements IEnumerable and Ienumerable<t>, but generic collections do not provide thread-safe control for synchronous access, that is, synchronous access to generic collections, We have to work on our own to synchronize or use a class in the System.Collections.Concurrent namespace.

In addition, IList and IDictionary inherit from Icollection,ilist implementations (such as array, ArrayList, list<t>, and so on), as well as those of the ICollection (such as Queue, Each element of concurrentqueue<t>, Stack, concurrentstack<t>, or linkedlist<t> is a value, and the IDictionary's implementation, such as Hashtable and SortedList classes, Dictionary<tkey, tvalue> and Sortedlist<tkey, tvalue> generic classes) Each element is a key-value pair.

Next, we'll discuss and compare some of the common collections separately.

Arrays Array
The array is not part of the System.Collections, but it inherits from the IList interface.. NET array can have multidimensional arrays, jagged arrays, or even create a lower bound than 0 is an array, by default, the recommended lower bound is a one-dimensional array of 0. This commonly used array is optimized and has the highest performance.

Unlike the System.Collections collection, array has a fixed capacity, and to increase capacity, you must create a new array object with the required capacity, copy the elements from the old array object to the new object, and then delete the old array. The set under System.Collections automatically expands capacity when it reaches the current capacity: the memory is reassigned and the elements are copied from the old collection to the new collection. This reduces the code needed to use the collection, but the performance of the collection may still be negatively affected. So we should set the initial capacity to the estimated size of the collection to avoid poor performance caused by multiple reallocation.

Collection classes under System.Collections
Collections of this type have a sort function and most are indexed. can automatically handle memory management, capacity expansion on demand.

ArrayList and list<t>:list<t> are generic versions of ArrayList, which, like array, are based on index access, and each data item holds only one data value, but they provide more powerful functionality and operations than array, Make them easier to use. In performance terms, generic versions are always more preferred than non-generic, unless the member type is type Object, because the generic version eliminates boxing and unboxing operations, and,list<t> performance is very similar to the same type of array without the need to reallocate the collection capacity. In addition, ArrayList can easily create synchronized versions, but the synchronization of array and list<t> must be done by itself.

Hashtable and Dictionary Collection types: These collections each item is a key-value pair. Dictionary<tkey,tvalue> is a generic version of Hashtable. The Hashtable object is made up of buckets that contain collection elements, each associated with a hash code that is generated by using the element key based on the hash function, and contains multiple elements. So these collections are quicker to search and retrieve data than most other collections. The same dictionary<tkey,tvalue> is always better than Hashtable performance, so it is recommended to use, multithreading synchronization using Concurrentdictionary<tkey, Tvalue> class.

Sorted collection type: System.Collections.SortedList class, System.collections.generic.sortedlist<tkey, tvalue> Generic classes and System.collections.generic.sorteddictionary<tkey, tvalue> generic classes that implement IDictionary interfaces, Two generic classes also implement System.collections.generic.idictionary<tkey, Tvalue>, and hashtable like each element is a key-value pair, but they maintain elements in a sort order based on the key, There is no O (1) insertion and retrieval characteristics of the hash table. Non-generic enumeration items are DictionaryEntry objects, and two generic types return Keyvaluepair<tkey, tvalue> objects. Their most important point is that they are sorted according to System.Collections.IComparer implementations or system.collections.generic.icomparer<t> implementations. SortedList allows us to access via index and key, while SortedDictionary can only be accessed through key, SortedList also save memory.

Queues and stacks: No more introductions, you can use this collection if you want to store data temporarily, and the data is discarded after only one visit. The difference between the queue and the stack is that the access is different, I believe everyone is very clear. They also have their own generic versions and thread-safe versions: System.Collections.Queue class, system.collections.generic.queue<t> Class and System.collections.concurrent.concurrentqueue<t>,system.collections.stack classes and System.collections.generic.stack<t> and system.collections.concurrent.concurrentstack<t>.

Set collection: The two types of hashset<t> and Sortedset<t> for the type collection, all implemented the Iset<t> interface. Set sets are closest to the set in mathematics, and are designed to implement mathematical set operations, such as the convergence, intersection, and so on. Where hashset<t> is not sorted, cannot have duplicate elements, can be treated as a dictionary<tkey,tvalue> version without a value, and provides high-performance set operations based on hashing. Sortedset<t> provides a set of ordered set operations. The point here is that some collections also provide extended methods for set operations and set operations that LINQ also provides, but they all return a new Ienumerable<t> collection, and set operations for sets set are all modifications to the current collection and provide a larger, more reliable set of operations.

This is not all of the. NET collection, it also has a bit collection and a private collection.

-Bit Collection
Each element of it is an identity bit, not an object. Among them are BitVector32 and BitArray.

BitVector32 is a structure that stores only 32 bits of data and can be used to store bit identities or small integers, which are value types, so performance is better.

While BitArray is a reference type, its capacity is always the same as the count, and you can assign or delete elements through the length property.

Private collections
NameValueCollection is based on NameObjectCollectionBase, but NameValueCollection accepts one-key multivalued, while NameObjectCollectionBase only accepts one-click values.

Some of the strongly typed collections in the System.Collections.Specialized namespace include StringCollection and StringDictionary, all containing a collection of values and dictionaries that are completely strings.

The CollectionsUtil class provides a series of static methods that can be used to create instances of case-insensitive hashtable or SortedList collections.

Some collections can be converted. For example, the HybridDictionary class was initially listdictionary, and became Hashtable when it was enlarged.

In addition, Keyedcollection<tkey, Titem> is a mixed type between lists and dictionaries that provides a way to store objects that contain their own keys, and it can also create lookup dictionaries when the number of elements reaches a specified threshold.

ListDictionary: Use one-way link list to implement IDictionary. It is recommended that a collection that typically includes fewer than 10 projects provide better performance than Hashtable when data items are less.

LINQ to Objects
We can use LINQ queries to access the System.Collections.IEnumerable or System.collections.generic.ienumerable<t> interface objects in memory.

It provides a common pattern of data access, which is generally simpler and more readable than the standard foreach loop, and provides powerful filtering, sorting, and grouping capabilities.

How to choose
We first want to be clear, if there is a generic version, the use of precedence.

Please identify a few questions before choosing:
Do you need to access by sequence, and the element is discarded after access?

The order of access is advanced first out or LIFO, random access?

is indexed access or key-based access?

Is it only a value, or a key-value pair form?

Is it one-on-one, or is it a couple?

Do you want to allow duplicates?

Is it in the order of entry, or does it need to be sorted according to certain rules, or does it matter?

Do you need faster retrieval and access?

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.