C # Collection In-depth explanation

Source: Internet
Author: User
Tags data structures readable
1 Overview

Because the project is relatively tight, there is no time to write Bowen, while the recent more leisure, hurriedly write a few to make a memo for the use, but also want to those who have just started to help it.

The use of the collection is everywhere, but the role is no less than so many, containers, traversal, comparison, output, copy themselves then we will come together to uncover the veil of the. NET collection, and see. How the NETFramework class library is designed.

Ø all collections in. NET implement the Icollection,ienumerable,icloneable interface. In addition, there are IList (for ordered or unordered collections), IDictionary (for key-value pairs) (generics have corresponding interfaces here for the simple discussion of non-paradigm only)

Ø several auxiliary interfaces are mainly used to traverse the strategy like: IDictionaryEnumerator, IEnumerator, the former integrated from the latter.

Ø mainly used in the comparison strategy are: IComparable, IComparer interface. 1.1 ICollection Interface:

int Count {get;} This interface is primarily used to get the number of currently bound elements and to traverse a property that is often used in the collection. Read-only.

BOOL issynchronized {get;} This property primarily obtains access to the collection as thread-safe, which is used primarily in multithreaded programming when manipulating shared resources.

Object SyncRoot {get;} This property is not frequently used, as the official documentation explains: Gets the objects that can be used to synchronize System.Collections.ICollection access.

void CopyTo (array array, int index);//To implement all elements of the copy policy copy itself into array arrays, in fact, the array is equivalent to the one-dimensional array you define. Index is the indexing at which to start copying. 1.2 IEnumerable, IEnumerator interface

These two interfaces determine how the collection is traversed, and the traversal strategy. All sets that implement the IEnumerable interface can use the number of foreach traversal elements or the IEnumerator GetEnumerator (); method to get a IEnumerator type implementation traversal where the GetEnumerator () method is a IEnumerable interface that defines only one interface method. Here's a IEnumerator of the members of this interface include:

Object current {get;} Gets the current element in the collection to return the object type.

BOOL MoveNext ()//pushes to the next element in the collection, False if success is true. Can be used to determine whether an element in the current collection is empty. This method is used with the current property to traverse the entire collection.

void reset ()//resets the collection element to return the start position of the MoveNext () method to the original state, which precedes the first element in the collection.

The relationship between IEnumerable and IEnumerator can be seen as a dependency. 1.3 ICloneable Interface

The interface has only one interface method: Object Clone ();//Creates a new object as a copy of the current instance. The specific copy strategy has a specific set to implement. 1.4 IList Interface

The interface defines the following members:

BOOL Isfixedsize {get;} This property is used to get whether the collection is a fixed size collection.

BOOL IsReadOnly {get;} This property is used to get whether the collection is read-only.

Object This[int index] {get; set;} This is a readable and writable index. All the sets of integrated IList interfaces can be manipulated using [index] as an element, in fact, they are implemented internally using arrays. Interested friends can use reflector to view the source code.

int Add (object value);//Add elements to the collection, which is often used by the.

void clear ();//Empty collection

BOOL Contains (object value);//To determine whether the collection contains an element, which is often used by the method.

int IndexOf (object value);//Get the index position of the element by value

void Insert (int index, object value);//insert element at specified position

void remove (object value);//Remove an element from the collection

void RemoveAt (int index);//Remove an element at a specific location. 1.5 IDictionary, IDictionaryEnumerator, DictionaryEntry interface

1,idictionary is an unordered list interface, and his members are:

ICollection Keys {get;} Gets the collection object for the key value in the collection

ICollection Values {get;} Gets the collection object of value values in the collection.

Object This[object key] {get; set;} Readable writable index based on key value

void Add (Object key, object value);//Add a key value pair

void clear ();//Empty collection

BOOL Contains (object key);//Determine if the collection contains the built

IDictionaryEnumerator GetEnumerator ();//idictionaryenumerator type of object.

void remove (object key), removing a key-value pair. 、

2,idictionaryenumerator Interface:

DictionaryEntry Entry {get;} This method gets the key value of the current dictionary class to the entity DictionaryEntry object.

Object Key {get;} Gets the key value of the value pair of the current collection

Object Value {get;} Gets the value of the key value pair in the current collection.

3,dictionaryentry This is a simple entity class:

Members include key and value and a constructor

Public DictionaryEntry (Object key, object value);

Public object Key {get; set;}

Public object Value {get; set;} 1.6 IComparable, IComparer

Both interfaces are designed to implement comparisons of the elements in the collection. IComparable defines the type to be used for comparison, and specific comparison strategies have specific class implementations, such as Int,float,long, which implement the IComparable and Icomparable<t> interfaces.

The members of the IComparable interface are:

int CompareTo (object obj);//The implementation of this method is the same, by comparing if equal returns 0, greater than return 1 less than return-1;

How do you compare the size of an entity class if it is not a value type? The IComparer interface is used to allow you to define your own comparison strategy for two objects. His members are:

int Compare (object x, object y);//By comparison if equality returns 0, the first is greater than the second returns 1, the first is less than the second return-1; 1.7 Other sets

1. Stack

Stack: One of the most commonly used linear data structures, followed by the LIFO principle (LIFO)

Stack this collection we only focus on the common methods, as follows:

public virtual void Push (object obj); After the stack operation, the top of the stack pointer points to the next empty space on the topmost element (the stack is growing upwards).

Public virtual Object Peek ()//Read stack operation read out the top of the stack of elements, do not delete.

Public virtual Object Pop ()//stack operation reads the top of the stack while the top pointer moves down one unit. The first element is ejected.

Public virtual bool Contains (object obj);//Determine if the stack contains this element

Public virtual void clear ();//Empty the stack.

2. Queues

Queues are also one of the most commonly used linear data structures, adhering to the first-in first-served principle (FIFO)

The common operations of the queue are:

public virtual void Enqueue (object obj);/Enter queue line

Public virtual Object Peek (); Read the first element of the queue

Public virtual Object Dequeue ()

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.