RememberThe textbook has the following sentence: "If you need to use multiple objects of the same type, you can use a set and an array. ", Yes, no error. The array size is fixed. If the number of elements is dynamic, use the collection class. In. NET Framework, there are many collections, such as List <T> and ArrayList are collection classes equivalent to arrays. There are other types of collections: queue, stack, linked list, and dictionary. This article will not describe these set objects in detail, but will discuss common set objects.
I. Array
In practical applications, Arrays can be divided into simple arrays, multi-dimensional arrays, sawtooth arrays, and Array arrays. the most widely used simple array and multi-dimensional array are simple arrays. Here I will take simple arrays as an example to briefly introduce the simple use of arrays. For other knowledge points, please refer to relevant materials and books.
For example, how should we define a simple array of integer types?
1classProgram
2{
3 staticvoidMain(string[]args)
4 {
5 int[]users=newint[5];
6 users[0]=10;
7 users[1]=20;
8 //
9 }
10}
Is it defined and used in this way? I want a friend who has learned programming to give a positive answer. I don't want to explain this too much. :
Here we use the basic type (int) to define the array, in the use of development we will also use the custom type array, let me briefly talk about this knowledge. to define a custom type array, you should first have a custom type. Of course, this type can block other related attributes. OK, the following code snippet:
1 /// <summary>
2 // User of the custom type, which encapsulates an attribute name.
3 /// </summary>
4 publicclassUser
5 {
6 privatestringname;
7 publicstringName
8 {
9 get {returnname ;}
10 set {name = value ;}
11}
12}
The above is a custom type. We encapsulate a name attribute in it. so, the type has been defined, So how should we use this custom type to define an array? Is that true?
1 classProgram
2 {
3 staticvoidMain (string [] args)
4 {
5 User [] user = newUser [2]; // defines an array of User types
6 Useru = newUser ();
7 u. Name = "Beniao ";
8 user [0] = u;
9 Console. WriteLine (user [0]. Name );
10}
11}
As shown above, we have completed the definition of a custom object array and array-related Operation . See:
Since arrays such as multi-dimensional arrays, sawtooth arrays, and Array arrays are not used in actual development, we will not introduce them here.
Ii. Collection object
Collection classes can be combined into collections to store Object-type Elements and generic collection classes. No generics existed before. NET 2.0. Currently, generic collection classes are usually the first type of the set. Generic collection classes are type-safe. If you use value types, you do not need to bind them. If you want to add different types of objects in the collection, and these objects are not mutually derived, for example, to add int and string objects in the collection, you only need to use the object-based collection class.
The collection of the image type is located in the System. Collections namespace; the Generic collection class is located in the System. Collections. Generic namespace; of course, there are other methods to combine the collection class. A set can be combined into a list, set, and Dictionary Based on the interfaces executed by the set class. The interface and its functions are shown in Table 10-1 .. NET 2.0 adds a new generic interface for the Collection class, such as IEnumerable and IList. The non-generic versions of these interfaces define an object Method And its generic version uses the generic type T.
1. List
. NET Framework provides ArrayList and List classes for dynamic lists. The usage of the List class in the System. Collections. Generic namespace is very similar to the ArrayList class in the System. Collections namespace. This class implements the IList, ICollection, and IEnumerable interfaces. The following code snippet:
1 classProgram
2 {
3 staticvoidMain (string [] args)
4 {
5 // create a list
6 ArrayListlist = newArrayList ();
7 // Add Element
8 list. Add ("Zhang San ");
9 // insert element
10 list. Insert (1, "Li Si ");
11 // access element
12 stringname = list [0]. ToString ();
13 // delete an element
14 list. RemoveAt (1); // Delete the element with index number 1 | Remove (Objectobj );
15 // retrieve Elements
16 intindex = list. IndexOf ("Zhang San"); // obtain the subscript of the object
17 // sorting Element
18 list. Sort (); // There are multiple reloads
19
20 List <int> l = newList <int> (); // create a List
21 l. Add (1); // Add an element
22 l. Add (7 );
23 l. Add (3 );
24 l. Add (9 );
25 l. Add (6 );
26 l. Add (10 );
27 l. Sort ();
28 foreach (int iinl)
29 {
30 Console. Write (I + "");
31}
32
33 l. Remove (10); // delete an element with a value of 10
34}
35}
For more information about list algorithms, see my previous article. Connection: List Algorithms
2. Queue
The queue is its element in the first-in-first-out (FIFO) mode. Processing . Elements placed in the queue are read first. It can be understood in combination:
In. NET, the System. Collections namespace contains non-Generic Queue and the System. Collections. Generic namespace contains a wildcard Queue <T>. The functions of these two classes are very similar, but generic classes are strongly typed and define type T, rather than generic classes are based on Object types.
When creating a queue, you can use a constructor similar to the List <T> type. The default constructor creates an empty queue, or you can use the constructor to specify the capacity. When an element is added to a queue, the capacity increases, including 4, 8, 16, and 32 elements. Similar to the List <T> type, the queue capacity is always multiplied as needed. Unlike the default constructor of a non-generic Queue, it creates an empty array containing 32 items.
But average Project Not many are used, so the sample code is not used here.
3. Linked List
Linked List calculation is actually very simple, because the frequency of use in project development is not high, so I will not detail it here.
I will briefly introduce the combined objects. In addition, collection objects such as stacks and dictionaries will also be used in some cases. For details, refer to relevant books and materials.
I recommend a book here. If you are interested, you can refer to C #2005 &. NET 3.0 advanced programming (version 5th).