In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces the collection of .NET, which has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, let the editor take you to understand it.
A collection is made up of independent data items with common characteristics, through which we can use the same calling code to process all the elements of a collection instead of dealing with each individual item separately. Net collections such as (System.Array classes and System.Collections namespaces) arrays, lists, queues, stacks, hash tables, dictionaries and even (under System.Data) DataSet, DataTable, generic versions of collections added in 2.0 (System.Collections.Generic and System.Collections.ObjectModel), and collections of effective thread-safe operations introduced in 4. 0 (System.Collections.Concurrent).
Aggregate interface
Before we discuss the various sets separately, let's discuss the commonness of sets and the inheritance level of the whole collection system.
The ICollection interface is the base interface for classes in the System.Collections namespace, and the corresponding ICollection is the base interface for all generic version collections. All collection classes inherit them directly or indirectly.
ICollection inherits IEnumerable to provide convenient enumeration, but it is worth noting that ICollection provides thread-safety control for synchronous access:
IsSynchronized: gets a value indicating 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 access the collection thread-safe by following it, but some collections provide Synchronized methods to provide encapsulation for thread-safe collections.
The code is as follows:
ICollection myCollection = someCollection
Lock (myCollection.SyncRoot)
{
/ / Insert your code here.
}
However, collections are 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 the non-generic version, the collection of the generic version implements not only the generic interface, but also the corresponding interface of the non-generic version. For example, ICollection implements IEnumerable and IEnumerable, but generic collections do not provide thread-safe control for synchronous access, that is, synchronous access to generic collections, we have to handle synchronization or use a class in the System.Collections.Concurrent namespace.
In addition, each element of IList and IDictionary inherits from implementers of ICollection,IList (such as Array, ArrayList, or List, etc.) and implementers of ICollection (such as Queue, ConcurrentQueue, Stack, ConcurrentStack, or LinkedList) is a value, while implementers of IDictionary (such as Hashtable and SortedList classes, Dictionary and SortedList generic classes) each element is a key-value pair.
Next, we will discuss and compare some commonly used collections respectively.
Array Array
Array is not part of System.Collections, but it inherits from the IList interface. Net Array can have multi-dimensional arrays, interlaced arrays, and even create arrays whose lower limit is not 0. By default, it is recommended to use one-dimensional arrays with a lower limit 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. Collections under System.Collections can automatically expand their capacity when they reach the current capacity: memory is reallocated and elements are copied from the old collection to the new one. This reduces the code required to use the collection, but the performance of the collection may still be negatively affected. Therefore, we should set the initial capacity to the estimated size of the collection to avoid poor performance due to multiple redistributions.
Collection classes under System.Collections
This type of collection has sorting capabilities and most of them are indexed. Can automatically handle memory management, capacity expansion as needed.
ArrayList and List:List are generic versions of ArrayList, and like Array, they are index-based, saving only one data value per data item, but they provide more powerful features and operations than Array, making them easier to use. In terms of performance, the generic version always takes precedence over the non-generic version, unless the member type is an object type, because the generic version eliminates boxing and unboxing; the performance of List is very similar to that of an array of the same type without the need to reallocate the set capacity. In addition, ArrayList can easily create synchronous versions, but the synchronization of Array and List must be done on its own.
Hashtable and Dictionary collection types: each item in these collections is a key-value pair. Dictionary is a generic version of Hashtable. The Hashtable object consists of buckets that contain collection elements, each associated with a hash code generated based on the hash function using the element key, and contains multiple elements. Therefore, this kind of collection is faster than most other collections in searching and retrieving data. The same Dictionary always performs better than Hashtable, so it is recommended that multithreaded synchronization use the ConcurrentDictionary class.
Sorted collection types: the System.Collections.SortedList class, the System.Collections.Generic.SortedList generic class, and the System.Collections.Generic.SortedDictionary generic class, all of which implement the IDictionary interface, and the two generic classes also implement System.Collections.Generic.IDictionary. Like Hashtable, each element is a key-value pair, but they maintain the elements in a key-based sort order, without the O (1) insert and retrieval features of the hash table. The enumerated item of a non-generic type is a DictionaryEntry object, while two generic types return a KeyValuePair object. Their most important point is that they are sorted according to the implementation of System.Collections.IComparer or System.Collections.Generic.IComparer. SortedList allows us to access through indexes and keys, while SortedDictionary can only be accessed through keys, and SortedList also saves memory.
Queues and stacks: without much introduction, you can use such collections if you want to store data temporarily and then discard it after only one access. The difference between queues and stacks lies in the sequence of visits, which I believe everyone knows very well. They also have their own generic and thread-safe versions: System.Collections.Queue class, System.Collections.Generic.Queue class, and System.Collections.Concurrent.ConcurrentQueue,System.Collections.Stack class, as well as System.Collections.Generic.Stack and System.Collections.Concurrent.ConcurrentStack.
Set collection: the two types of this type collection, HashSet and SortedSet, implement the ISet interface. Set set is closest to the set in mathematics and is specially used to realize the Set operation of mathematics, such as union, intersection and so on. Hashset has no sorting and can not have repeating elements, so it can be regarded as a value-free version of Dictionary, which provides high-performance Set operations based on hash keys. SortedSet, on the other hand, provides a sorted collection of Set operations. It is important to mention that some collections also provide extension methods for Set operations and Set operations provided by LINQ, but they all return new IEnumerable collections, while the Set operations of Set collections modify the current collection and provide a larger and more reliable set of operations.
This is not the whole of. Net collections, it also has bit collections and dedicated collections.
Bit set
Each element of it is an identity bit, not an object. Among them are BitVector32 and BitArray.
BitVector32 is a structure that can only store 32-bit data and can be used to store bit identifiers or small integers. It is a value type, so its performance is better.
BitArray, on the other hand, is a reference type, its capacity is always the same as the count, and elements can be assigned or deleted through the Length attribute.
Dedicated set
NameValueCollection is based on NameObjectCollectionBase;, but NameValueCollection accepts multiple values per key, while NameObjectCollectionBase accepts only one value per key.
Some strongly typed collections in the System.Collections.Specialized namespace include StringCollection and StringDictionary, both of which contain value collections and dictionaries that are entirely strings.
The CollectionsUtil class provides a series of static methods that can be used to create instances of a case-insensitive Hashtable or SortedList collection.
Some collections can be converted. For example, the HybridDictionary class starts out as ListDictionary and becomes Hashtable when it grows.
In addition, KeyedCollection is a mixed type between lists and dictionaries, which 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 an one-way link list to implement IDictionary. It is recommended that a collection of less than 10 items usually provide better performance than Hashtable when there are fewer data items.
LINQ to Objects
We can use LINQ queries to access objects that implement System.Collections.IEnumerable or System.Collections.Generic.IEnumerable interfaces in memory.
It provides a general data access pattern; it is usually more concise and readable than the standard foreach loop; and it provides powerful filtering, sorting, and grouping capabilities.
How to choose
First of all, we should make it clear that if there is a generic version, it will be used first.
Please identify a few questions before choosing:
Do you need to access it sequentially, and the element is discarded after access?
Is the order of visits first-in-first-out or last-in-first-out, random access?
Index-based access or key-based access?
Is there only value, or is it in the form of key-value pairs?
Is it one-to-one or one-to-many?
Is repetition allowed?
Is it saved 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?
Thank you for reading this article carefully. I hope the article "what is the collection of .NET" shared by the editor will be helpful to you. At the same time, I also hope that you will support and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.