C # collection class: arraylist, stringcollection, hashtable, list

Source: Internet
Author: User
1. array set
In fact, this concept is already included in the array section. In fact, the array set is new int [2];
Http://msdn.microsoft.com/zh-cn/library/57yac89c (vs.80). aspx
2. arraylist
The difference between arraylist and array: http://msdn.microsoft.com/zh-cn/library/41107z8a (vs.80). aspx
Instance:

Using... system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;
Using system. collections;

Namespace CSHARP
...{
Public class testarraylist
...{
Public testarraylist ()
...{
// Create an empty arraylist, and add some elements.
Arraylist stringlist = new arraylist ();

Stringlist. Add ("");
Stringlist. Add ("ABC ");
Stringlist. Add ("abcdef ");
Stringlist. Add ("abcdefg ");
Stringlist. Add (20 );

// Index or array subscript is a number, so no name is required.
Console. writeline ("element... {0} is \" {1} \ "", 2, stringlist [2]);

// Assign a value to the element whose subscript is 2
Stringlist [2] = "ABCD ";
Console. writeline ("element... {0} is \" {1} \ "", 2, stringlist [2]);

// Output the total element elements of the stringlist
Console. writeline ("number of elements in the list:... {0 }",
Stringlist. Count );

Try
...{
// Array subscript ranges from 0 to count-1. If you try to output a subscript smaller than 0 or greater than or equal to count, an exception is thrown.
Console. writeline ("element... {0} is \" {1 }\"",
Stringlist. Count, stringlist [stringlist. Count]);
}
Catch (argumentoutofrangeexception aoore)
...{
Console. writeline ("stringlist (... {0}) is out of range (out of bounds ).",
Stringlist. Count );
}

// You cannot use this method to add elements. You can only use stringlist. Add ("AA") to add elements.
Try
...{
Stringlist [stringlist. Count] = "42 ";
}
Catch (argumentoutofrangeexception aoore)
...{
Console. writeline ("stringlist (... {0}) is out of range (out of bounds ).",
Stringlist. Count );
}

Console. writeline ();
// Use for For Loop
For (INT I = 0; I <stringlist. Count; I ++)
...{
Console. writeline ("element... {0} is \" {1} \ "", I,
Stringlist [I]);
}

Console. writeline ();
// Use foreach For Loop
Foreach (Object o in stringlist)
...{
Console. writeline (O );
}

Console. Readline ();
}
}
}

The stringcollection is similar to arraylist, but stringcollection can only receive character types.
Official Address: http://msdn.microsoft.com/zh-cn/library/system.collections.specialized.stringcollection (vs.80). aspx

3. List <t>, which is the most commonly used one.
Http://msdn.microsoft.com/zh-cn/library/6sh2ey19 (vs.80). aspx
This is actually an example of combining generics with arrays.

Using... system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;

Namespace CSHARP
...{
Public class testlist
...{
// Default constructor
Public testlist ()
...{
// Declaration syntax. In other words, it defines the syntax of the objapplelist-set variable.
List <Apple> objapplelist = new list <Apple> ();

// Define three apple instances (also called objects)
Apple objapple1 = new Apple ();
Objapple1.color = "red ";
Objapple1.weight = 10;

Apple objapple2 = new Apple ();
Objapple2.color = "green ";
Objapple2.weight = 12;

Apple objapple3 = new Apple ();
Objapple3.color = "black ";
Objapple3.weight = 8;

// Add the three apple instances to the objapplelist file.

Objapplelist. Add (objapple1 );
Objapplelist. Add (objapple2 );
Objapplelist. Add (objapple3 );

// Traverse the objapplelist set.
Foreach (Apple o in objapplelist)
...{
Console. writeline ("color is... {0}, weight is {1}", O. Color, O. weight );
}

// Total number:
Console. writeline ("the total number of objapplelist files is:... {0}", objapplelist. Count );

Console. Readline ();
}
}

Public class Apple
...{
// Define Fields
Private string _ color = "";
Private decimal _ Weight = 0;

// Define the attribute corresponding to the field
Public String color
...{
Get... {return _ color ;}
Set... {_ color = value;} // The value here is the C # keyword. Indicates the value passed out.
}

Public decimal weight
...{
Get... {return _ weight ;}
Set... {_ Weight = value ;}
}
}
}

Here: List <Apple> objapplelist = new list <Apple> (); in fact, we can use arrays, for example, Apple [] objaapparray = new Apple [3]; however, the array size is fixed. Cannot be added dynamically.
Why not use arraylist here? In principle, arraylist can also be used, for example:

Arraylist obapplearraylist = new arraylist ();
Obapplearraylist. Add (objapple1 );
Obapplearraylist. Add (objapple2 );
Obapplearraylist. Add (objapple2 );

The purpose of the arraylist we don't need is to ensure type security. Because at this time, you can also obapplearraylist. Add ("string");, obapplearraylist. Add ("Heihei");, so that the elements of obapplearraylist are not simply Apple classes.
List <t>.

4. hashtable,
Official Address: http://msdn.microsoft.com/zh-cn/library/system.collections.hashtable (vs.80). aspx
Instance:

Using... system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;
Using system. collections;

Namespace CSHARP
...{
Public class testhashtable
...{
Public testhashtable ()
...{
Hashtable objhashtable = new hashtable ();

// It should be noted that the add here is a bit different from the arraylist. here we need to specify two values: Key and value.
// And must all be objects
Objhashtable. Add ("key", "value ");
Objhashtable. Add (1, 2 );
Objhashtable. Add (2.1, 3.2 );

// Obtain all keys
Icollection keys = objhashtable. Keys;
Foreach (Object key in keys)
...{
Console. writeline ("key is... {0}, values is {1}", key, objhashtable [Key]);
}

Console. writeline ();

// Use another Traversal method:
Foreach (dictionaryentry de in objhashtable)
...{
Console. writeline ("key is... {0}, values is {1}", De. Key, De. value );
}

Console. Readline ();
}
}
}

 

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.