c#5.0 Generic collection type Brief

Source: Internet
Author: User

. The generic collection in net

Here is a general introduction to common generic collections, and many times the thread safety of concurrency often makes us worry. Thus the brief. NET concurrency, see MSDN for details on the thread safety features.

    • Multiple concurrent write operations are not supported for normal collections
    • Partial support for single-threaded write and concurrent read operations
    • While. NET4 added a large number of concurrent collections

The common generic collection interfaces are introduced first, most of which are located in the System.Collection.Generic namespace.

    • Ienumerable<t>, which can get a ienumerator<t> iterator, which is a table from the database's point of view, the latter is a cursor, and both interfaces are the only set interfaces that have a variability.
    • Icollection<t>, which expands the Ienumerable<t>, adds the count and IsReadOnly properties, the Add and remove methods, the contains, and so on. All standard generic collections implement this interface.
    • ILIST<T> Provides positioning capabilities, including an indexer, insert, and removeat, which we generally think can be randomly accessed through an index on the generic collection. 、
    • Idictionary<tkey, Tvalue>, which represents the set of key-value pairs, extends the Icollection<keyvaluepair<tkey, Tvalue>>, and the value can be used in tryxxx mode.
    • Iset<t> represents a unique set of values that contains a number of collection operations: intersection, and, and complement.

The following describes the specific collection generic collection types, which in practice need to select the most appropriate collection type based on the specific scenario.

  • List<t>, which is the default selection of the list, contains an array, and provides the logical size of the list count and the size of the background array capacity, which expands when the array is full. Because it is a continuous data structure, the cost of adding and removing operations is higher, providing two-point lookup and high efficiency. At the same time, its sort operation modifies the contents of the original list, which differs from the order by, and the sort is unstable, and there is a case in which the equal elements are ordered differently.
  • Arrays, the most basic collection, are derived from System.Array, including one-dimensional array t[10], two-dimensional array t[10, 20], etc., through the static method of the array class ConvertAll, FindAll and BinarySearch operations.
  • Colletion<t>, located in the System.Colletion.ObjectModel namespace, for bindinglist<t> and observablecollection<t> The base class is provided as an extension type. Collection types related to bidirectional binding, note that they only notify when the wrapper changes, and no event is raised when the underlying list changes.
  • Readonlycollection<t> and Readonlyobservablecollection<t>, which also resemble the wrapper, which implements the INotifyCollectionChanged, INotifyPropertyChanged two interfaces.
  • Dictionary<tkey, Tvalue> Using a hash table, finding performance depends on the merits of the hash function, using Equals and GetHashCode by default, which can be done by making a iequalitycomparer<tkey > as a parameter.
  • Sortlist<tkey, tvalue> and Sorteddictionary<tkey, Tvalue> both are dictionary classes, the former maintains a sorted array, the event complexity of the add delete operation is O (n), The latter maintains a red-black tree, adding the delete operation event complexity to O (log n), but consumes more heap memory and uses icomparer<tkey> for comparison.
  • Hashset<t>, is a dictionary<,> with no value, has the same performance characteristics, and the order of maintenance is generally independent of the order of addition.
  • Sortedset<t>, is not worth sorteddictionary<,> To maintain a red-black tree, add the event complexity for delete and check operations to O (log n). Provides a Getviewbetween method that returns another sortedset<t> between the upper and lower bounds of the original set; Note that this is a dynamic view that changes as the original set changes. Although it may seem convenient, it is important to note that "there is no free lunch" and that the cost of operation is greater in order to maintain internal consistency.
  • Queue<t>, constructs a ring buffer, actually maintains an underlying array, contains two indexes, remembers the position (Slot) of the team and the queue, and then expands if the queue pointer catches up with the team pointer. Provide Enqueue, Dequeue, Peek and other methods to queue, team, check operation.
  • STACK<T>, its implementation is simpler and can be seen as a list<t> that provides push, POP, peek operations.

Finally, we introduce the parallel collection, which is the thread-safe collection. (Note that all concurrency types do not implement the Ilist<t> interface)

    • Iproducerconsumercollection<t> and Blockingcollection<t>, the former is the abstraction of data storage in the producer/consumer model, the latter is its wrapper class, Using concurrentqueue<t> as a background store, providing toarray methods to get a snapshot of the current state of the collection, the Tryxxx method allows an effective failure mode to reduce the need for locks. (for example, when there is only one item in the queue, two threads simultaneously determine whether it has an item and all return True, which is one thread that performs the out-of-line operation, while the other thread throws an exception when it executes the out-of-line operation, and therefore needs to verify that the queue has an item operation and that there is an item on the Need to add a lock)
    • Concurrentbag<t>,concurrentqueue<t>,concurrentstack<t> and they are to iproducerconsumercollection<t The implementation of the >, whose GetEnumerator () method returns a collection snapshot, can change the collection at iteration, but the change does not respond to the iterator.
    • Concurrentdictionary<tkey, Tvalue>, implements the Idictionary<tkey, tvalue> interface. Support for concurrent read-write and thread-safe iterations, but the difference is that the changes to the dictionary during the iteration do not determine whether or not the iterator is being reflected.

Subsection: In daily work, locks are required when encountering global variables that require concurrent operations for non-collection types, and when they are collection types, they need to be handled using the corresponding parallel collection classes, which are well-coordinated by the TPL. In particular, when using a non-thread-safe dictionary class for concurrent operations, there are situations such as a dead loop, which is especially important.

Reference documents

    1. Jon, Skeet. Deep Understanding C # ( version 3 ) [M]. Beijing : People's Post and Telecommunications publishing house , 469-483.

c#5.0 Generic collection type Brief

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.