ArticleDirectory
- 1. List <t>
- 2. dictionary <tkey, tvalue>
Namespace: using system. Collections. Generic;
Normal Array: the length and Data Type of the array must be specified during declaration. arraylist: the data type and length of the array elements are not limited, but the efficiency is slightly lower. Generic: similar to the array function, its length is not limited, and the data type must be specified during declaration.
C # pre-defines common generic classes in a centralized manner, such as list <t>, Dictionary <tkey, tvalue>, and queue <t>.
1. List <t>
Syntax: List <type> variable = new list <type> (); List <type> variable = new list <type> {XXX, XXX, xxx}; // declare and initialize
Example 1: simple and practical: Declaration, initialization, assignment, and read operations. As shown in the following example, the list1 variable can be used to add any element with unlimited length.
// Declaration method 1 List list1 = new list (); list1.add (211); list1.add (985); // declaration method 2 list list2 = new list {211,985 }; list2.add (136); For (INT I = 0; I <list2.count; I ++) {console. write (list2 [I]. tostring ());}
2. dictionary <tkey, tvalue>
In the above example, the elements stored in the generic variables list1 are int-type variables, which are more complex in actual applications. Dictionary <tkey, tvalue> generic classes are C # pre-defined generic classes. Each element stored by dictionary consists of a pair of {key: Value} variables.
- Each element can be considered as a record row. The key must be unique and can be int, string, or other types.
- The value item can be of any type, such as int, String, array, or instantiated object.
① Simple use
class program {static void main (string [] ARGs) {// instantiate three user objects: User user1 = new user ("Li Ning", 21 ); user user2 = new user ("Nike", 42); User user3 = new user ("Adi", 31 ); // declare the dictionary generic variable users // The Key type is int; the value type is user dictionary
Users = new dictionary
(); // Add "element" to users. The key of "element" cannot be repeated with users. add (2, user1); users. add (3, user2); users. add (5, user3); // cyclically traverse each "element" foreach (var p in users) {console. write (P. key); console. write (P. value. name); console. write (P. value. age);} // directly traverse the values foreach (User U in users. values) {console. write (U. name); console. write (U. age) ;}}// user class user {public string name; // name public int age; // age public user (string name, int age) {This. name = Name; this. age = age ;}}
② Used as a parameter
Class program {static void main (string [] ARGs) {user user1 = new user ("Li Ning", 21); User user2 = new user ("Nike", 42 ); user user3 = new user ("Adi", 31); dictionary <int, user> Users = new dictionary <int, user> (); users. add (2, user1); users. add (3, user2); users. add (5, user3); program. show (users); // use a generic variable as a function parameter} // note that the writing format of the parameter type is static void show (Dictionary <int, user> List) {foreach (var p in list) {console. write (P. key); console. write (P. value. name); console. write (P. value. age) ;}} class user {public string name; // name public int age; // age public user (string name, int age) {This. name = Name; this. age = age ;}}