Collection Class Series "Go" for common collection classes and collection interfaces in C #

Source: Internet
Author: User
Tags new set

Common Collection Interface series: http://www.cnblogs.com/fengxiaojiu/p/7997704.html

Common Collection Class series: Http://www.cnblogs.com/fengxiaojiu/p/7997541.html

Common collection classes:

Array (Array) the deficiencies ( namely: the difference between a collection and an array)

1. The array is fixed size and cannot be scaled. Although System.Array.Resize this generic method resets the array size, the method is to recreate the new set size array, using the element initialization of the old array. Then the previous array is discarded! And the set is variable length.

2. Array to declare the type of the element, the element type of the collection class is object.

3. The array can be read and writable and cannot declare only the reading group. The collection class can provide the ReadOnly method to use the collection in a read-only manner.

4. Arrays have integer subscripts to access specific elements, but many times such subscripts are not very useful. The collection is also a list of data but does not use subscript access. Many times a collection has a custom subscript type that does not support subscript access for queues and stacks at all!

Array list (ArrayList)

ArrayList can store any type, not as strict as the List generic,list<t> more efficient, ArrayList is the equivalent of list<object>.

The ArrayList class object is designed to be a dynamic array type whose capacity is appropriately expanded as needed:

1:add () Adds an element to the array

2:remove () to delete an element in an array

3:removeat (int i) delete an element with an index value of I in an array

4:reverse () Reverses the elements of an array

5:sort () The elements of an array in order from small to large

6:clone () copy an array

The System.Collections namespace contains interfaces and classes that define a collection of various objects such as lists, queues, bit arrays, hash tables, and dictionaries. The System.Collections.Generic namespace contains interfaces and classes that define generic collections, which allow users to create strongly typed collections that provide better type security and performance than non-generic strongly typed collections. The System.Collections.Specialized namespace contains a dedicated and strongly typed collection, such as a linked list dictionary, a bit vector, and a collection that contains only strings.

Listing (list)

A strongly typed list of objects that can be accessed through an index. Provides methods for searching, sorting, and manipulating lists, remembering that the list class performs better and is type-safe in most cases when deciding whether to use list or the ArrayList class, which has similar functionality. If you use a reference type for the type T of the list class, the behavior of the two classes is exactly the same. However, if you use value types for type T, you need to consider implementation and boxing issues.

If you use a value type for type T, the compiler generates an implementation of the list class specifically for that value type. This means that you do not have to boxing the list elements of a lists object to use the element, and after you create approximately 500 list elements, the memory saved for not boxing the list elements will be greater than the memory used to generate the class implementation.

Hash table (HashTable)

First, hash table (Hashtable) Summary

In the. NET framework, Hashtable is a container provided by the System.Collections namespace that handles and behaves like Key/value key-value pairs, where key is usually used for quick lookups, while key is case-sensitive;

Value is used to store values corresponding to key. Key/value Key-value pairs in Hashtable are all object types, so hashtable can support any type of Key/value key-value pair.

Second, the simple operation of the hash table

Add a Key/value key value pair to the hash table: Hashtableobject.add (Key,value);

Remove a Key/value key value pair in the hash table: Hashtableobject.remove (key);

Remove all elements from the hash table: Hashtableobject.clear ();

Determines whether a hash table contains a specific key key:HashtableObject.Contains (key);

Dictionary (directonary)

Hashtable and dictionary<k, v> type
1: It is recommended to use dictionary in single thread, it has the generic advantage, and the reading speed is faster, the capacity utilization is fuller.
2: Hashtable is recommended in multi-threading, the default Hashtable allows single-threaded writes, multithreaded reads to Hashtable further calls to the synchronized () method to obtain a fully thread-safe type, and dictionary non-thread-safe, The lock statement must be used for protection, and the efficiency is greatly reduced.
3:dictionary has the ability to arrange data in the order in which they are inserted (note: But the order is disrupted when the call to remove () is removed), so it is convenient to use dictionary in situations where the order needs to be reflected.

Hashtable application: Do object cache, tree recursive algorithm substitution, and various need to improve the efficiency of the occasion. The Key/value in Hashtable are all object types, consisting of buckets containing the collection elements. Buckets are virtual subgroups of elements in Hashtable, and buckets make search and retrieval easier than search and retrieval in most collections. Each bucket is associated with a hash code that is generated using a hash function and is based on the key of the element. The advantage of Hashtable is that it is indexed in a very fast way. If you access the elements in any type of key value faster than other collections, especially when the amount of data is particularly large, the efficiency difference is particularly large.

Row sequence list (SortedList)

Similar to a hash table, the difference is that the key array in the SortedList is sorted

Name-value collection (NameValueCollection)

NameValueCollection and HashTable are very similar, but they are still different, HashTable key is unique, and NameValueCollection is not the only

Stacks (Stack)

Represents a simple last-in-first-out non-generic collection of objects. Push method into the stack, pop method out of the stack

Queuing (queue)

Queue FIFO, Enqueue method into queue, Dequeue method out queue

Collection Class Series "Go" for common collection classes and collection interfaces in C #

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.