C # collection of knowledge points

Source: Internet
Author: User

In C #, the set includes the commonly used arraylist (dynamic array), hashtable (search table for key words and values), and non-commonly used bitarray (Bit Array ), queue, sortedlist, and stack.
In fact, a set is a combination of ordered data that can be effectively processed. Here we will mainly introduce the commonly used arraylist and hashtable.

Arraylist
Similar to a one-dimensional dynamic array, arraylist can store any object. There are three common methods for arraylist: add (), insert (), and remove ().
Example:
First introduce the namespace: using system. collections;

Hashtable
Is used to store a set of key/value pairs. If you need to store keys at the same time and have corresponding values, you can use hashtable.
Example:

Code
Public static void main ()
{
Hashtable hash = new hashtable (); // defines a hashtable set.
Hash. Add ("one", 1); // enter the key and value in the set.
Hash. Add ("two", 2 );
Hash. Add ("three", 3 );
Hash. Add ("four", 4 );

Foreach (string a in hash. Keys) // traverses all healthy values
{
Console. writeline ("{0}, {1}", A, hash [a]); // output key and Value
}
}
Code
Public static void main ()
{
Arraylist arr = new arraylist ();
Arr. Add (10); // Add a value for the set
Arr. Add (10); // Add the second value
Arr. insert (0, 8); // insert a value of 8 at the index position of 0th
Console. writeline (ARR. indexof (10, 2); // search refers to the number of 10 values from 0 to 2.
Foreach (int A in ARR) // traverses the set arr
{
Console. writeline (a); www.elivn.com
}

}

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.