Use of C # common collections

Source: Internet
Author: User

Most collections are system.collections,system.collections.generic two namespaces. Where System.Collections.Generic is specifically used for generic collections.

The collection type for a particular type is located in the System.Collections.Specialized namespace;

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

The following are the interfaces for collection and list implementations:

One, 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 collection list<t> implements so many interfaces, and the information of the concrete interface can be viewed through the 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 ("John"); List.
            Add ("Dick"); List.
            Add ("Harry"); List.
            ADD ("Tian Six"); 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); }
    }

}


Second, the queue

Queue advanced First out, a head out, with queue<t> to achieve

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

It can be seen that the queue implements the interface of the set, and the interface of the iteration

Using System;
Using System.Collections.Generic;

Namespace ConsoleApplication1
{public
    class program
    {
        static void Main (string[] args)
        {
            queue<string> queue = new queue<string> ();
            Get in the team
            queue. Enqueue ("John");
            Queue. Enqueue ("Dick");
            Queue. Enqueue ("Harry");
            Queue. Enqueue ("Tian Six");
            Queue. Enqueue ("Zhao Qi");

            foreach (String item in queue)
            {
                Console.WriteLine ("foreach Iteration:" + Item);
            }

            Out team while
            (queue. Count > 0)
            {
                Console.WriteLine ("Out Team:" + queue). Dequeue ());
            }

            Console.read ();}}



Third, stack

Stack: Advanced from the same side, with stack<t> implementation

[DebuggerDisplay ("Count = {count}")]
    [DebuggerTypeProxy (typeof (System_stackdebugview<>))]
    [ComVisible (false)]
    public class Stack<t>: Ienumerable<t>, ICollection, IEnumerable


The stack also implements the interface of the set interface and the iterative

Using System;
Using System.Collections.Generic;

Namespace ConsoleApplication1
{public
    class program
    {
        static void Main (string[] args)
        {
            stack<string> stack = new stack<string> ();
            into Stack stack
            . Push ("John");
            Stack. Push ("Dick");
            Stack. Push ("Harry");
            Stack. Push ("Tian Six");
            Stack. Push ("Zhao Qi");

            foreach (String item in stack)
            {
                Console.WriteLine ("foreach Iteration:" + Item);
            }

            Out stack while
            (stack. Count > 0)
            {
                Console.WriteLine ("Out Stack:" + stack.) Pop ());

            Console.read ();}}



Four, linked list

LinkedList is a two-way linked list, the list has a bit, is in the middle of the list inserted, delete elements quickly, but find the middle and end of the element is very slow, need a node to find a node.

    [Serializable]
    [DebuggerTypeProxy (typeof (System_collectiondebugview<>))]
    [DebuggerDisplay ("Count = {count}")]
    [ComVisible (false)]
    public class linkedlist<t>: Icollection<t>, Ienumerable<t>, ICollection, IEnumerable, ISerializable , Ideserializationcallback

This shows that the list is also a set of characteristics, you can iterate, but also have the characteristics of the list

using System using System.Collections.Generic;
            Namespace ConsoleApplication1 {public class program {static void Main (string[] args) {
            linkedlist<string> llist = new linkedlist<string> ();
            linkedlistnode<string> node = new linkedlistnode<string> ("root");
            Llist.addfirst (node);
            node = llist.addafter (node, "John");
            node = llist.addafter (node, "Dick");
            node = llist.addafter (node, "Harry");
            node = llist.addafter (node, "Tian Six");

            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 (); }
    }
}


Five, ordered list

SortedList is stored with key-value pairs, keys cannot be duplicated and are sorted according to 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 characteristics of the dictionary, but also the function of collection and iteration.

Using System;
Using System.Collections.Generic;

Namespace ConsoleApplication1
{public
    class program
    {
        static void Main (string[] args)
        {
            // Key must be unique if not the only one can consider lookup<tkey,telement>
            sortedlist<int, string> slist = new Sortedlist<int, String > ();
            Slist.add (100, "John");
            Slist.add (21, "Dick");
            Slist.add (13, "Harry");
            Slist.add (44, "Tian Six");
            Slist.add (35, "Zhao Qi");

            foreach (Keyvaluepair<int, string> item in slist)
            {
                Console.WriteLine ("key=" + item.) Key.tostring () + "; value=" + Item. Value);
            }

            Console.read ();}}



Six, dictionaries

Dictionaries are complex data structures that allow you to find values by key, and dictionaries are free to add and remove elements, without the overhead of a collection due to 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

You can see that the dictionary also has the attributes of the collection, which can be iterated

Using System;
Using System.Collections.Generic;

Namespace ConsoleApplication1
{public
    class program
    {
        static void Main (string[] args)
        {
            // Key must be unique
            dictionary<int, string> dict = new Dictionary<int, string> ();
            Dict. ADD (11, "John");
            Dict. ADD (1, "Dick");
            Dict. ADD (2, "Harry");
            Dict. ADD (16, "Tian Six");
            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 dictionaries, by the way, an orderly dictionary corresponds to an ordered list; Sorteddictionary,sortedlist,sortedset

is sorted according to 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, "John");
            Dict. ADD (1, "Dick");
            Dict. ADD (2, "Harry");
            Dict. ADD (16, "Tian Six");
            Dict. ADD (12, "Zhao Qi");

            foreach (Keyvaluepair<int, string> item in Dict)
            {
                Console.WriteLine ("key=" + item.) Key.tostring () + "; value=" + Item. Value);
            }

            Console.read ();}}


Seven, set

Set: Contains not duplicate elements, commonly used Hashset,sortedset

[Serializable]
    [DebuggerDisplay ("Count = {count}")]
    [DebuggerTypeProxy (typeof (Hashsetdebugview<>))]
    public class hashset<t>: ISerializable, Ideserializationcallback, Iset<t>, Icollection<t> Ienumerable<t> a 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 ("John");
            Hset.add ("Dick");
            Hset.add ("Harry");
            Hset.add ("Tian Six");
            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 ("John");
            Hset.add ("Dick");
            Hset.add ("Harry");
            Hset.add ("Tian Six");
            Hset.add ("Zhao Qi");

            foreach (String item in Hset)
            {
                Console.WriteLine ("foreach Iteration:" + Item);
            }

            Console.read ();}}



Performance comparisons:

--------------------------------------------------------------------------------------------------------------- ------------

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.