Several common collections and usage ranges for C #

Source: Internet
Author: User
Tags ming ranges

Common collections in C # are divided into two main categories: generic and non-generic collections

Using a non-generic collection requires the introduction of a namespace system.collectionarraylist----An array hashtable that can be dynamically incremented as needed----the hash table used to store the key-value pairs queue----loop first-out queued stack-----follows a last-in-first-out stack Using a generic collection requires the introduction of a namespace system.collection.genericlist<t>--an array dictionary<tkey,tvalue> that can be dynamically incremented as needed--- Hash table used to store key-value pairs queue<t>----Follow FIFO queue stack<t>----Follow a last-in-first-out stack 1.1:arraylist: Its main application is that there are many types of data that need to be stored, and the program is not too fussy about the gains and losses of performance, because it has to be boxed and unboxing operation, this operation is very consumption performanceBoxing: is to package data of a value type into an instance of a reference typeUnpacking: Extracting value types from reference dataThe method code is as follows
            //1. Create an object firstArrayList arr=NewArrayList (); //add elements using the Add () method, no restrictions on element typesArr. ADD ( A); Arr. ADD ("1234"); Arr. ADD (12.7f); //use/subscript/To get the element at the specified positionConsole.WriteLine ("arr[0]="+arr[0]); //gets the number of the current array            intCount=arr.            Count; //use the Insert () method to insert an element into the specified subscript positionArr. Insert (1,"Lao Zhang"); Console.WriteLine ("arr[1]="+arr[1]); //Remove the specified element from the array using the Remove () methodArr. Remove ("Lao Zhang"); Console.WriteLine ("arr[1]="+arr[1]); //Use the RemoveAt () method to delete the element that specifies the location of the subscriptArr. RemoveAt (0);            Log (arr); //determines whether the specified element exists in the current array            BOOLB=arr. Contains ("Lao Wang"); if(b) {Console.WriteLine ("Lao Wang in the array"); }            Else{Console.WriteLine ("Lao Wang is not in the array!!!!"); }

1.2:list: The design of the program type is relatively safe, and performance is relatively high

            //1. Create an object firstlist<string> list=Newlist<string>(); //add elements using the Add () method, only elements of the string type can be addedList. ADD ("123"); List. ADD ("778"); //useful [subscript] to get the element at the specified positionConsole.WriteLine ("arr[0]="+arr[0]); //gets the number of elements in the current array            intcount1=arr.            Count; //use the Insert () method to insert an element into the specified subscript positionArr. Insert (1,"Lao Zhang"); Console.WriteLine ("arr[1]="+arr[1]); //Remove the specified element from the array using the Remove () methodArr. Remove ("Lao Zhang"); //Use the Removear () method to delete an element that specifies the location of the subscriptList. RemoveAt (0); Console.WriteLine (list[0]); //Contains () determines whether the specified element exists in the current array            BOOLB1=list. Contains ("Lao Wang"); if(B1) {Console.WriteLine ("Lao WANG!!!! In the array"); }            Else{Console.WriteLine ("Lao Wang is not in the array!!!!"); }            //make a collection emptylist.            Clear (); Console.WriteLine (list. Count);

1.3: Dictionary objects (Dictionary): generally as aggregated statistics or fast-using feature access

            //Dictionary is a collection of stored keys and values//dictionary are unordered, key keys are uniqueConsole.WriteLine (); Console.WriteLine ("Dictionary The contents of the Dictionary object:"); //Create a Dictionary object, the type of key is String,value type is int typedictionary<string,int> dic =Newdictionary<string,int> (); //The Add method is used for adding key-value pairsDic. ADD ("Laowang", A); Dic. ADD ("Laozhang", A); //remove a key-value pair from a dictionary//DiC.            Remove ("Laowang"); //Empty Current Dictionary//DiC.            Clear (); //gets the number of keyvalue in the current dictionary            intCount2=DiC.            Count; Console.WriteLine ("the current dictionary has"+count2+"a keyvalue"); //get value from key            intage=dic["Laowang"];            Console.WriteLine (age); //Checks if the dictionary contains the specified key            BOOLB2=dic. ContainsKey ("Xiaowang");            Console.WriteLine (B2); //checks whether the dictionary contains the specified value            BOOLB3=dic. Containsvalue ( A);            Console.WriteLine (B3); //attempt to get the value corresponding to the specified key//If the current dictionary contains Laowang this key, then the corresponding value is obtained and stored in S, bb=true//If the current dictionary does not contain laowang this key, then S=0,bb=false;            ints=0; BOOLbb = dic. TryGetValue ("Laowang", outs);            Console.WriteLine (BB);                Console.WriteLine (s); 

1.4: Stack: general stack used in DFS (depth-first traversal) You can check out this algorithm doesn't know

            //stacks and queues automatically grow according to the required capacity//stack and queue allow repeating elementsConsole.WriteLine ("contents of the Stack object:"); Stack<string> ss=Newstack<string>(); intcount4=SS.            Count; Ss.            Clear (); BOOLB4=ss. Contains ("Lao Wang");            Console.WriteLine (B4); //push elements into the stackSs. Push ("Lao Wang"); Ss. Push ("Xiao Zhang"); Ss. Push ("Xiao Ming"); //gets and removes elements from the stack//Pop the element out of the stack, there is no such element in the stack//Pop the element out of the stack, and its rules are in the LIFO principle            strings1=SS.            Pop ();            Console.WriteLine (S1); stringS2 =SS.            Pop ();            Console.WriteLine (S2); stringS3 =SS.            Pop ();            Console.WriteLine (S3); //cannot output again because there are no more elements in the stack.//String s4 = ss. Pop ();//Console.WriteLine (S4);

1.5: Queue: General used in BFS (breadth-first traversal)

Console.WriteLine ("Queue Contents:"); Queue<string> q =Newqueue<string> ();            Q.clear (); intcount6=Q.count; BOOLB7=q.contains ("Lao Wang"); //adding elements to the queueQ.enqueue ("Lao Wang"); Q.enqueue ("Lao Zhang"); Q.enqueue ("Xiao Ming"); //gets the elements in the queue            stringss1=Q.dequeue ();            Console.WriteLine (SS1); stringSs2=Q.dequeue ();            Console.WriteLine (SS2); stringss3=Q.dequeue (); Console.WriteLine (SS3);

The above is my summary of some of the small knowledge, hope to help everyone, if there are errors can be written in the comments, we discuss together!!!!!!!

Several common collections and usage ranges for C #

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.