C # Collection Classes

Source: Internet
Author: User

ARRAYLIST Collection Class

The Remove method removes an element from ArrayList, ArrayList reorder, remove (value), RemoveAt (index)

Add (value) adds values to the ArrayList tail

Insert (PARA1,PARA2) The first parameter is the position to be joined (where the Para2 is added), and the second parameter is the value to be inserted, if number={1,2,3,4,5}

QUEUE Collection Class

First in, first out mechanism (FIFO) queued at queue tail (Enqueue), from Queue Head (Dequeue)

Enqueue () method

Dequeue () method

STACK Collection Class

The Stack class is the post-first-out mechanism (lifo,list-in, first-out), and the new member is in the first line

Push () method into the stack

Pop () method out of stack

Hashtable Collection Class

A hash table provides a mapping in which each key corresponds to a value, and if an existing key is specified, the value can only be indexed by square brackets.

Add only adds [Key,value] that does not already exist, cannot add only the KEY value, does not establish a mapping

1 New Hashtable (); 2 ages. ADD (" Wangxiao ",N); 3 ages[" Wangxiao "]=;

A dictionaryentry (struct type) is returned when a hash table is traversed using foreach, and the contents of the Hashtable can be accessed through the Key/value property

foreach inch ages)            {                = (String) element. Key;                 int age = (int) element. Value;                Console.WriteLine ("name: {0} Age: {1}", name,age);                Console.ReadLine ();                           }

SortedList Collection Class

The Sortlist class is similar to a hash table, except that sortlist is always sorted by key, and the data is re-sorted after the change.

1SortedList ages =NewSortedList ();2Ages. ADD ("James", A);3Ages. ADD ("Edward", -);4Ages. ADD ("Lucy", -); 5             foreach(dictionaryentry Elementinchages)6             {7String name =(String) element. Key;8                 intAge = (int) element. Value;9Console.WriteLine ("name: {0} Age: {1}", name, age);Ten console.readline (); One}

Collection initialization

Add value when a simple collection class can be named directly

Arrylist numbers=new ArrayList () {1,2,3,4,5,6};

For hash tables and SortedList collections to declare Key/value

Hashtable ages=New Hashtable () {"James",},{"Edward  "Lucy","},{";

C # Collection Classes

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.