Organization language in C #-----------single-and double-column, generic collection

Source: Internet
Author: User

Arrylist

Very similar to the array, but higher than the array, because arrylist can be dynamically maintained, not limited to the storage of the learned array as the number of limited values, you can dynamically add, delete, traverse the value inside.

Note: Arrylist is similar to an array, so you can also retrieve the value from the index, and the index starts at 0.

Additions, removal, and traversal in arrylist

ArrayList list = new ArrayList ();
List. ADD ("Hello!! ");
List. ADD ("World");

Add a value to the arrylist through the Add () method in Arrylist.

List. Remove ("World");

Removes the selected value from the arrylist by removing () in the Arrylist method.

List. RemoveAt (0);

Removes the value from the selected index position to the arrylist by using the RemoveAt () method in Arrylist.

List. Clear ();

Remove all the values in the Arrylist by using the Clear () method in Arrylist.


foreach (string item in list) {
Console.WriteLine (item);
}

You can use a for loop or a foreach loop to traverse all the values in the Arrylist

The for and foreach does not pass:

The For loop uses subscripts to traverse while foreach iterates over all the items in the Arrylist

List<t>

list<se> list = new list<se> ();

List single column set "<>" angle brackets can be a type, such as: Int,string, can be class: SE (Employee Class), PC (Computer class)
Se se = new SE ();
Se. Name = "hehe";
Se. Age = "25";
Se se2 = new SE ();
Se2. Name = "hehe";


Se2. Age= "24";

New out instance assigns value to instance
List. ADD (SE);
List. ADD (SE2);

Add ();, Remove (); method still applies

To iterate through all the collections in the list collection by using foreach
foreach (SE item in list)
{
Se seforeach = (SE) item;
Console.WriteLine (item. Name+item. Age);
}

Note in the type position in foreach, keep in mind what type is not going to be strong and note that traversal is what value you need to get. So you need to take the item that is traversed (the collective name of all the items in the collection). Out needed value

Hashtable (hash table)

Belong to the double column set category has Key,value value, like List, the key and value value can also be a type, can be a class

Hashtable table = new Hashtable ();
Se se = new SE ();
Se. Name= "hehe";
Se. Age= "25";
Se. Id = 0010;
Se se2=new se ();
Se2. Name= "hehe";
Se2. Age= "24";
Se2. Id = 0020;
Table. Add (SE. ID,SE);

Table. ADD (SE2. Id, SE2);

Iterate through the key values in the Hashtable for each item in the Hashtable

             foreach (Object item in table. Keys)
            {
                 Console.WriteLine (item);
           }

Iterate through the value in Hashtable for each item in the Hashtable
foreach (Object item in table. Values)
{
Se a = (SE) item;
Console.WriteLine (A.name+a.age);
}

Through the object table in Hashtable to iterate through each item in the Hashtable

foreach (DictionaryEntry item in table)
{

Console.WriteLine (item. Key);
Console.WriteLine (((SE) item. Value). Name);

}

Dictionary<key,value>

Double Column Set dictionary<key,value>

Dictionary<int, se> dic = new Dictionary<int, se> ();

Se se = new SE ();
Se. Name= "hehe";
Se. Age= "25";
Se. Id = 0010;
Se se2=new se ();
Se2. Name= "hehe";
Se2. Age= "24";
Se2. Id = 0020;
Dic. Add (SE. ID,SE);
Dic. ADD (SE2. ID,SE);

The same as a hash table through Key,value traversal
foreach (int item in DIC. Keys)
{
Console.WriteLine (item);
Console.Write (Dic[item]);
}
foreach (SE item in DIC. Values)
{
Se a = (SE) item;
Console.WriteLine (A.name+a.age);
}

                    through the object dic in dictionary to iterate through each item in the Hashtable
                   keyvaluepair<int,se> Write out the type of key and the type of value
            foreach (KeyValuePair <int,SE> item in DIC)
            {
                 Console.WriteLine (item. Key);
                Console.WriteLine (item. Value.name+item. Value.age);
           }

Add a value to the arrylist through the Add () method in Arrylist.

Organization language in C #-----------single-and double-column, generic collection

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.