C # collection class,

Source: Internet
Author: User

C # collection class,

ARRAYLIST collection class

The Remove method removes an element from the Arraylist, And the Arraylist is reordered. Remove (value), RemoveAt (index)

Add (value) adds a value to the end of Arraylist

Insert (para1, para2) the first parameter is the location to be added (the location where para2 is added), and the second parameter is the value to be inserted. If number = {1, 2, 3, 4, 5}

QUEUE collection class

The first-in-first-out mechanism (FIFO) is used to Enqueue at the end of the queue and Dequeue from the queue header)

Enqueue () method

Dequeue () method

STACK collection class

The Stack class is the post-in, first-out mechanism (LIFO, list-in, first-out). New members are at the top of the team.

Push () method into Stack

Pop () method output Stack

Hashtable collection class

A hash table provides a ing. Each key corresponds to a value. If you specify a value for an existing key, you can only add an index using square brackets.

Add can only Add non-existing [KEY, VALUE], not only Add key VALUE, do not create ing

1 Hashtable ages = new Hashtable (); 2 ages. Add ("", 12); 3 ages [""] = 15;

When you use foreach to traverse a hash table, a DictionaryEntry (struct type) is returned. You can access the content of the hash table through the key/value attribute.

foreach (DictionaryEntry element in ages)            {                String name = (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 the hash table. The difference is that SortList is always sorted by key. After adding, deleting, and modifying data, it will be sorted again.

 1             SortedList ages = new SortedList(); 2             ages.Add("James", 22); 3             ages.Add("Edward", 25); 4             ages.Add("Lucy", 20);             5             foreach (DictionaryEntry element in ages) 6             { 7                 String name = (String)element.Key; 8                 int age = (int)element.Value; 9                 Console.WriteLine("name: {0} age: {1}", name, age);10                 Console.ReadLine();11             }    

Set Initialization

For simple collection classes, you can add value values directly when naming them.

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

For a hash table and a SortedList set, the key/value must be declared simultaneously.

Hashtable ages=new Hashtable(){{"James",22},{"Edward",25},{"Lucy",20}};

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.