. NET Surface Question Series (v) data structure (Array, List, Queue, Stack) and thread safety issues

Source: Internet
Author: User

Collection

1. Array (arrays):

Allocated in contiguous memory and cannot be arbitrarily extended, the numeric types in the array must be consistent. The declaration of an array has two forms: define the length directly, then assign the value, and assign the value directly.

Cons: Inserting data slowly.

Advantages: High performance, no more performance of data and no impact

  Special note: The array is not thread-safe and requires a locking mechanism in multi-threading, and if you do not want to use a lock, you can replace the array with the thread-safe concurrentstack.

2. ArrayList (variable-length array)

You do not have to specify the length at the time of Declaration, that is, the length is variable, and you can store elements of different types.

Fatal disadvantage: No matter what type of deposit into the ArrayList, it becomes the object type, and when used, it is reverted to the original type, so it is type unsafe, when the value type is deposited,

A boxing operation takes place and becomes an object reference type, and when used, the object type is removed and changed to the original value type, Nima, can you tolerate it?

  Conclusion: It is recommended to use list instead!!

  Special Note: ArrayList is not thread safe and requires a locking mechanism in multi-threading.

3. list<t> (generic collection) Recommended use

An array is implemented internally, but there is no risk of unpacking and boxing, which is type-safe

  It is important to note that:list<t> is not thread-safe and requires a locking mechanism in multiple threads, and if you do not want to use locks, you can use Concurrentbag as a thread-safe array instead of list<t>

4. linkedlist<t> linked List

Storage in memory space is not necessarily contiguous, so the most important difference between arrays is that they cannot be accessed with subscript.

Advantage: Increase deletion fast, apply to the situation of frequent increment and decrease node.

Disadvantage: Can not use subscript access, query slow, need to find from scratch.

  It is important to note that:linkedlist<t> is not thread-safe and requires a locking mechanism in multiple threads.

5. Queue<t> Queue

FIFO, Queue (Enqueue) and Team (Dequeue) two operations

  It is important to note that:queue<t> is not thread-safe and requires a locking mechanism in multiple threads, and if you do not want to use locks, the thread-safe queue is concurrentqueue.

  Practical scenario: Using queues to solve high concurrency problems (see: http://www.cnblogs.com/yaopengfei/p/8322016.html)

6. stack<t> Stack

Last-in, first-out, on-stack (push) and out-of-stack (POP) two operations

  Special note that:stack<t> is not thread safe

7. Hashtable

Typical space change time, storage data can not be too much, but adding and deleting changes to check the speed very fast.

  Special Note: The Hashtable is thread-safe and does not need to be used with locks.

8. Dictionary<k,t> Dictionary (Hashtable of generics)

Adding and removing changes is very fast, can be used in place of the entity only ID and another attribute, greatly improve efficiency.

  It is important to note that:dictionary<k,t> is not thread-safe and requires a locking mechanism in multiple threads, and if you do not want to use locks, the thread-safe dictionary is concurrentdictionary.

emphasis: The above 8 types, in addition to Hashtable is thread-safe, the rest is not, all need to cooperate with lock lock, or use concurrentxxx to replace.

Four interfaces

1. IEnumerable

Is the most basic interface for iterative use, which has a GetEnumerator method.

2. ICollection

Inherited the IEnumerable interface, mainly for the collection, the internal Count property represents the number, such as ArrayList, List, LinkedList have implemented the interface.

3. IList

Inheriting the IEnumerable and ICollection, the data interface that implements the IList interface can use index access, which means that it is continuously allocated in memory, such as array, List.

4. IQueryable

The main and IEnumerable interfaces are compared here.

The parameter of the implementation method in enumerable is the Func delegate, and the parameter of the method implemented in the queryable is an expression.

Both implementations IQueryable and Ienumabler are deferred loads, but they are implemented in different ways, the former is the iterator pattern, the parameter is the Func delegate, and the latter is the expression tree implementation.

Yield keyword

1. Yield must appear in ieunmerable

2. Yield is the state machine of the iterator, can be deferred query, use the time only query, can be implemented in order to load

. NET Surface Question Series (v) data structure (Array, List, Queue, Stack) and thread safety issues

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.