This article only applies to. Net 1.x, so it does not involve generics and is not comprehensive.
. NET provides many types of sets for us. Each type has its own applicable scenarios, and none of them can be applied to all scenarios. These collections include linked lists, arrays, queues, and stacks. All sets implement the icollection interface. Therefore, we can quickly obtain all set types by searching the icollection interface. Both the ilist and idictionary interfaces are derived from the icollection interface, ilist is a set of values, and its elements are accessed through indexes. For example, arraylist; idictionary is a set of key/value pairs, such as hashtable. If neither of the above two types meets your needs, you can directly create your own collection type from the icollection interface.
In. net 1. in the era of X, the most common set type is array, or an array of certain feature types. The most common reason is that the elements in other types of sets are system. object type. during use, you may encounter frequent packing/unpacking operations. After. NET 2.0 introduced generics, other types of collections have the opportunity to make full use of them.
For array, Note: If the array element is of the reference type, the array element is not initialized when the new method is used to initialize the array. If the array element is accessed, the result is null. You also need to instantiate the array elements when accessing the program.
For multi-dimensional arrays ,. NET provides us with two different types of arrays, one is a multi-dimensional array in the form of "sawtooth", and the other is a multi-dimensional array in the general sense, the declaration and traversal methods are different. See the following code.
Multi-dimensional array
1 /// <summary>
2 // test multi dimension array
3 /// </Summary>
4 Private Static void multidimarraytest ()
5 {
6 // jagged array
7 int [] [] arrjaggedtest = new int [3] [];
8 arrjaggedtest [0] = new int [5];
9 arrjaggedtest [1] = new int [2];
10 arrjaggedtest [2] = new int [4];
11 foreach (INT [] arrtemp in arw.aggedtest)
12 {
13 foreach (INT ntemp in arrtemp)
14 {
15 console. Write (ntemp );
16}
17}
18 console. writeline ();
19 // common multi-dimensional array
20 int [,] arrmuldim = new int [3, 5];
21 foreach (INT ntemp in arrmuldim)
22 {
23 console. Write (ntemp );
24}
25}
It can be seen that for multi-dimensional arrays in the form of "sawtooth", the foreach statement is required for each dimension. For multi-dimensional arrays in the sense of "universal, you can directly use a foreach to traverse it.
Defects caused by array: 1. Dynamic size adjustment is not allowed; 2. dynamic insertion and deletion of array elements cannot be performed.
To solve the first defect ,. NET provides arraylist, which is based on arrays, but maintains the size of arraylist internally without the user's concern ,. NET provides us with dictionary-type data. In this type of set, key and value are stored in hashtable mode, which is convenient for us to find, add, and delete elements.
When the defined type contains a set, we should expose the set to the user. There are two ways to achieve this: 1. compile the indexer for the type; 2. implement the ienumerable interface for the type.
In actual use, which type of set is used depends on the operation to be executed and the overall goal of the Application for space and speed. In most cases, the array class provides the most efficient collection container (only. net 1. x ).