In c #, collection classes and collection interfaces are commonly used,

Source: Internet
Author: User

In c #, collection classes and collection interfaces are commonly used,

Common collection classes:

Array(Array)(That is:Differences between a set and an array)

1. arrays are fixed in size and cannot be scaled. Although the generic method System. Array. Resize can reset the Array size, this method re-creates an Array with the new size and initializes it with elements of the old Array. Then the previous array will be discarded! The set is variable length.

2. to declare the element type of an array, the element type of the Collection class is object.

3. the read-only array cannot be declared for readable and writable arrays. The Collection class can provide the ReadOnly method to use the set in read-only mode.

4. the array must have an integer subscript to access specific elements. However, such subscript is not very useful in many cases. The set is also a data list, but it does not use subscript access. Most of the time there is a set of custom subscript types, which does not support subscript access for queues and stacks!

ArrayList (ArrayList)

ArrayList can store any type. Unlike List generics, List <T> is more effective and ArrayList is equivalent to List <object>.

The ArrayList class object is designed as a dynamic array type, and its capacity will be expanded as needed:

1: Add () Add an element to the array

2: Remove () deletes an element in the array.

3: RemoveAt (int I) deletes the elements whose index value is I in the array.

4: Reverse () reverses the elements of the array.

5: Sort () arranges the elements of the array in ascending order.

6: Clone () to copy an array

The System. Collections namespace contains interfaces and classes that define a set of objects (such as lists, queues, bit arrays, hash tables, and dictionaries. System. collections. the Generic namespace contains interfaces and classes that define a Generic set. A Generic set allows you to create a strongly typed set. It provides better type security and performance than a non-Generic strong set. The System. Collections. Specialized namespace contains Specialized and strong Collections, such as list dictionaries for links, bitvectors, and Collections that only contain strings.

List)

List of strong types of objects that can be accessed through indexes. Provides methods for searching, sorting, and operating the List. When you decide whether to use the List or the ArrayList class (the two have similar functions, remember that the List class performs better and is type-safe in most cases. If the type T of the List class is referenced, the behavior of the two classes is identical. However, if you use the value type for type T, you need to consider implementation and packing.

If the value type T is used, the compiler will generate a List class for this value type. This means that you can use this element without packing the List element of the List object. After about 500 List elements are created, the memory saved by packing the list elements is greater than the memory used to generate this class.

Hash table (HashTable)

I. Brief introduction to the hash table (Hashtable)

In. in the. NET Framework, Hashtable is System. A container provided by the Collections namespace is used to process and present key-value pairs similar to key/value. The key is usually used for quick search, and the key is case sensitive;

Value is used to store the value corresponding to the key. In Hashtable, key/value pairs are of the object type, so Hashtable can support any type of key/value pairs.

Ii. Simple operations on Hash Tables

Add a key/value pair in the hash table: HashtableObject. Add (key, value );

Remove a key/value pair in the hash table: HashtableObject. Remove (key );

Remove all elements from the hash table: HashtableObject. Clear ();

Determine whether the hash table Contains the specified key: HashtableObject. Contains (key );

Dictionary (Directonary)

Hashtable and Dictionary <K, V> types
1: Dictionary is recommended in a single thread. It has the wildcard advantage, and the reading speed is fast, and the capacity utilization is more adequate.
2: Hashtable is recommended for multithreading. By default, Hashtable allows writing by a single thread. multi-threaded reading further calls the Synchronized () method to Hashtable to obtain the type of full thread security, while Dictionary is not thread-safe, the lock statement must be manually used for protection, greatly reducing the efficiency.
3: Dictionary has the ability to sort data by insertion Order (Note: however, when the Remove () function is called to delete a node, the order is disrupted ), therefore, it is convenient to use a Dictionary in a situation that needs to reflect the order.

HashTable application scenarios: Object Caching, replacement of tree recursive algorithms, and various scenarios that require improved efficiency. The key/value in HashTable is of the object type and is composed of buckets containing collection elements. A bucket is a virtual sub-group of each element in HashTable. Compared with searching and searching in most sets, a bucket can make searching and searching easier. Each bucket is associated with a hash code. The hash code is generated using a hash function and is based on the key of this element. The advantage of HashTable lies in its indexing method, which is very fast. If any type of key value is used to access the element, it is faster than other sets, especially when the data volume is very large, the efficiency difference is particularly large.

SortedList)

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

NameValueCollection)

NameValueCollection is similar to HashTable, but they are different. The KEY of HashTable is unique, and NameValueCollection is not unique.

Stack)

A simple post-first-out non-generic set of objects. Push method into Stack, Pop method out Stack

Queue)

Queue first-in-first-out, enqueue method into queue, dequeue method out queue

Common collection Interface Series: http://www.cnblogs.com/fengxiaojiu/p/7997704.html

Collection class series: http://www.cnblogs.com/fengxiaojiu/p/7997541.html


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.