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
}
}