Home > Backend Development > C#.Net Tutorial > What are the C# collection classes?

What are the C# collection classes?

零下一度
Release: 2017-06-24 10:50:47
Original
3178 people have browsed it

ARRAYLIST collection class

Remove method removes an element from Arraylist, Arraylist is reordered, Remove(value), RemoveAt(index)

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

Insert (para1, para2) The first parameter is the position to be added (the position after adding para2), and the second parameter is the position to be inserted Value, if number={1,2,3,4,5}

QUEUE collection class

First-in-first-out mechanism (FIFO) is queued at the end of the queue Queue (Enqueue), dequeue (Dequeue) from the head of the queue

Enqueue() method

Dequeue() method

STACK collection class

The stack class is a last-in-first-out mechanism (LIFO, list-in, first-out), and new members are placed at the head of the queue

The push() method is pushed into the stack

The pop() method pops off the stack

Hashtable collection class

The hash table provides a mapping. Each key corresponds to a value. If you specify an existing key The value value can only be indexed through square brackets

Add can only add [KEY, VALUE] that does not exist yet, and cannot only add the key value without establishing a mapping

1 Hashtable ages = new Hashtable();2 ages.Add("王小二",12);3 ages["王小二"]=15;
Copy after login

When using foreach to traverse the hash table, a DictionaryEntry (struct type) will be returned, and the content in the hash table can be accessed through the key/value attributes

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

SortedList collection class

The SortList class is similar to a hash table. The difference is that the SortList is always sorted according to the key. It will be re-sorted after adding, deleting or modifying data

 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             }
Copy after login

Collection initialization

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

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

For hash tables and SortedList collections, key/value must be declared at the same time

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

The above is the detailed content of What are the C# collection classes?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template