C # use of common collections,

Source: Internet
Author: User

C # use of common collections (reproduced ),

Most Collections are in the two namespaces System. Collections and System. Collections. Generic. Specifically, System. Collections. Generic is used for Generic sets.

The Collection types of specific types are located in System. Collections. Specialized; namespace;

The thread-safe collection class is located in the System. Collections. Concurrent; namespace.

The following interfaces are implemented for collections and lists:

 

I. List

    [Serializable]    [DebuggerTypeProxy(typeof(Mscorlib_CollectionDebugView<>))]    [DebuggerDisplay("Count = {Count}")]    public class List<T> : IList<T>, ICollection<T>, IEnumerable<T>, IList, ICollection, IEnumerable


From this we can see that the generic set List <T> has implemented so many interfaces, and the specific interface information can be viewed through tools.

Using System; using System. collections. generic; namespace ConsoleApplication1 {public class Program {static void Main (string [] args) {List <String> list = new List <string> (); list. add ("Zhang San"); list. add ("Li Si"); list. add ("Wang Wu"); list. add (""); list. add ("Zhao Qi"); for (int I = 0; I <list. count; I ++) {Console. writeLine ("for Loop:" + I. toString () + "=" + list [I]);} list. removeAt (0); foreach (String item in list) {Console. writeLine ("foreach iteration:" + item);} list. addRange (new String [] {"Hello1", "Hello2", "Hello3"}); list. forEach (Print); Console. read ();} private static void Print (String item) {Console. writeLine ("ForEach:" + item );}}}


Ii. Queue

Queue first-in-first-out, one-in-one-out, with Queue <T>

    [Serializable]    [DebuggerTypeProxy(typeof(System_QueueDebugView<>))]    [ComVisible(false)]    [DebuggerDisplay("Count = {Count}")]    public class Queue<T> : IEnumerable<T>, ICollection, IEnumerable

We can see that the queue implements the set interface and iterative interface.

 

Using System; using System. collections. generic; namespace ConsoleApplication1 {public class Program {static void Main (string [] args) {Queue <String> queue = new Queue <string> (); // enter the queue. enqueue ("Zhang San"); queue. enqueue ("Li Si"); queue. enqueue ("Wang Wu"); queue. enqueue (""); queue. enqueue ("Zhao Qi"); foreach (String item in queue) {Console. writeLine ("foreach iteration:" + item);} // while (queue. count> 0) {Console. writeLine ("coming out:" + queue. dequeue ();} Console. read ();}}}


Iii. Stack

Stack: from the same side first and then out, with Stack <T>

 [DebuggerDisplay("Count = {Count}")]    [DebuggerTypeProxy(typeof(System_StackDebugView<>))]    [ComVisible(false)]    public class Stack<T> : IEnumerable<T>, ICollection, IEnumerable


Stack also implements the set interface and iteration Interface

Using System; using System. collections. generic; namespace ConsoleApplication1 {public class Program {static void Main (string [] args) {Stack <String> stack = new Stack <string> (); // stack. push ("James"); stack. push ("Li Si"); stack. push ("Wang Wu"); stack. push (""); stack. push ("Zhao Qi"); foreach (String item in stack) {Console. writeLine ("foreach iteration:" + item);} // outbound stack while (stack. count> 0) {Console. writeLine ("output stack:" + stack. pop ();} Console. read ();}}}


Iv. Linked List

The sort list is a two-way linked list. The linked list has a bit, that is, inserting and deleting elements in the center of the linked list is very fast, but it is very slow to find the elements between the center and the end. You need to find one node and one node.

    [Serializable]    [DebuggerTypeProxy(typeof(System_CollectionDebugView<>))]    [DebuggerDisplay("Count = {Count}")]    [ComVisible(false)]    public class LinkedList<T> : ICollection<T>, IEnumerable<T>, ICollection, IEnumerable, ISerializable, IDeserializationCallback

It can be seen that the linked list also has the characteristics of the Set, can be iterated, and also has the characteristics of the linked list

 

Using System; using System. collections. generic; namespace ConsoleApplication1 {public class Program {static void Main (string [] args) {shortlist <String> lList = new shortlist <string> (); inclulistnode <String> node = new inclulistnode <string> ("root"); lList. addFirst (node); node = lList. addAfter (node, "James"); node = lList. addAfter (node, "Li Si"); node = lList. addAfter (node, "Wang Wu"); node = lList. addAfter (node, ""); node = lList. addAfter (node, "Zhao Qi"); foreach (String item in lList) {Console. writeLine ("foreach iteration:" + item);} node = lList. first; Console. writeLine ("first element:" + node. value); node = lList. last; Console. writeLine ("last element:" + node. value); Console. read ();}}}


5. ordered list

SortedList is stored using key-value pairs. Keys cannot be repeated and are sorted by key.

    [Serializable]    [DebuggerTypeProxy(typeof(System_DictionaryDebugView<,>))]    [DebuggerDisplay("Count = {Count}")]    [ComVisible(false)]    public class SortedList<TKey, TValue> : IDictionary<TKey, TValue>, ICollection<KeyValuePair<TKey, TValue>>, IEnumerable<KeyValuePair<TKey, TValue>>, IDictionary, ICollection, IEnumerable

We can see that SortedList not only has the dictionary feature, but also has the set and iteration functions.

 

Using System; using System. collections. generic; namespace ConsoleApplication1 {public class Program {static void Main (string [] args) {// Key must be unique. If it is not unique, consider Lookup <TKey, TElement> SortedList <int, string> sList = new SortedList <int, string> (); sList. add (100, "Zhang San"); sList. add (21, "Li Si"); sList. add (13, "Wang Wu"); sList. add (44, ""); sList. add (35, "Zhao Qi"); foreach (KeyValuePair <int, String> item in sList) {Console. writeLine ("key =" + item. key. toString () + "; value =" + item. value);} Console. read ();}}}


6. Dictionary

A dictionary is a complex data structure that allows you to search for values using keys. A dictionary can be used to add or delete elements without any overhead caused by moving elements.

[Serializable]    [DebuggerTypeProxy(typeof(Mscorlib_DictionaryDebugView<,>))]    [DebuggerDisplay("Count = {Count}")]    [ComVisible(false)]    public class Dictionary<TKey, TValue> : IDictionary<TKey, TValue>, ICollection<KeyValuePair<TKey, TValue>>, IEnumerable<KeyValuePair<TKey, TValue>>, IDictionary, ICollection, IEnumerable, ISerializable, IDeserializationCallback

It can be seen that the dictionary also has the set feature and can be iterated.

 

Using System; using System. collections. generic; namespace ConsoleApplication1 {public class Program {static void Main (string [] args) {// The Key must be a unique Dictionary <int, String> dict = new Dictionary <int, string> (); dict. add (11, "Zhang San"); dict. add (1, "Li Si"); dict. add (2, "Wang Wu"); dict. add (16, ""); dict. add (12, "Zhao Qi"); foreach (KeyValuePair <int, String> item in dict) {Console. writeLine ("key =" + item. key. toString () + "; value =" + item. value);} Console. read ();}}}


Speaking of the dictionary, let's talk about the ordered dictionary, which corresponds to the ordered list; SortedDictionary, SortedList, SortedSet

Sort by Key

Using System; using System. collections. generic; namespace ConsoleApplication1 {public class Program {static void Main (string [] args) {// Key must be unique SortedDictionary <int, String> dict = new SortedDictionary <int, string> (); dict. add (11, "Zhang San"); dict. add (1, "Li Si"); dict. add (2, "Wang Wu"); dict. add (16, ""); dict. add (12, "Zhao Qi"); foreach (KeyValuePair <int, String> item in dict) {Console. writeLine ("key =" + item. key. toString () + "; value =" + item. value);} Console. read ();}}}

 

VII. Set

Set: contains non-repeating elements, commonly used HashSet, SortedSet

 [Serializable]    [DebuggerDisplay("Count = {Count}")]    [DebuggerTypeProxy(typeof(HashSetDebugView<>))]    public class HashSet<T> : ISerializable, IDeserializationCallback, ISet<T>, ICollection<T>, IEnumerable<T>, IEnumerable

 

 [Serializable]    [DebuggerTypeProxy(typeof(SortedSetDebugView<>))]    [DebuggerDisplay("Count = {Count}")]    public class SortedSet<T> : ISet<T>, ICollection<T>, IEnumerable<T>, ICollection, IEnumerable, ISerializable, IDeserializationCallback


 

Using System; using System. collections. generic; namespace ConsoleApplication1 {public class Program {static void Main (string [] args) {HashSet <String> hSet = new HashSet <string> (); hSet. add ("Zhang San"); hSet. add ("Li Si"); hSet. add ("Wang Wu"); hSet. add (""); hSet. add ("Zhao Qi"); foreach (String item in hSet) {Console. writeLine ("foreach iteration:" + item);} Console. read ();}}}

 

Using System; using System. collections. generic; namespace ConsoleApplication1 {public class Program {static void Main (string [] args) {SortedSet <String> hSet = new SortedSet <string> (); hSet. add ("Zhang San"); hSet. add ("Li Si"); hSet. add ("Wang Wu"); hSet. add (""); hSet. add ("Zhao Qi"); foreach (String item in hSet) {Console. writeLine ("foreach iteration:" + item);} Console. read ();}}}


Performance Comparison:

 

Bytes ---------------------------------------------------------------------------------------------------------------------------

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.