When a series of strings (strings) are used in the C # code and you need to create a List for them, the List <string> generic class is a string used to store a series of strings (strings) is an excellent solution. Here are some examples of List <string> generic classes. Let's take a look.
List Example
The following is an example of creating a list of new strings using C #. Each of these strings is cyclically used using the foreach statement. Note that the required namespace "using System. Collections. Generic;" is added at the top of the code snippet. List is a Generic type in the namespace.
List <string> sample code:
1 using System;
2 using System. Collections. Generic;
3
4 class Program
5 {
6 staticvoid Main ()
7 {
8 List <string> cities = new List <string> (); // List of city names
9 cities. Add ("San Diego"); // String element 1
10 cities. Add ("Humboldt"); // 2
11 cities. Add ("Los Angeles"); // 3
12 cities. Add ("Auburn"); // 4
13
14 // Write each city string.
15 foreach (string city in cities)
16 {
17 Console. WriteLine (city );
18}
19 Console. ReadKey ();
20}
21}
Output:
San Diego
Humboldt
Los Angeles
Auburn
Note the angle brackets in the code ). In the declaration statement, angle brackets <and> enclose the string type in the middle, which means that the List can only store elements of the String type. The string type can be a string in lowercase or a String in uppercase.
Initialization example using Collection
The C # syntax allows you to initialize a List in a clearer way. Use collection for initialization. values must be enclosed by braces {} for initialization. The annotations in the following example illustrate the code used by the compiler when the program is executed.
Sample Code for List initialization:
1 using System;
2 using System. Collections. Generic;
3
4 class Program
5 {
6 staticvoid Main ()
7 {
8 List <string> moths = new List <string>
9 {
10 "African armyworm ",
11 "Mottled pug ",
12 "Purple thug ",
13 "Short-cloaked moth"
14 };
15 // The List moth contains four strings.
16 // IL:
17 //
18 // List <string> <> g _ initLocal0 = new List <string> ();
19 // <> g _ initLocal0.Add ("African armyworm ");
20 ///... four more Add CILS
21 // List <string> moths = <> g _ initLocal0;
22}
23}
Description. We can see that the initialization compilation of the string list is to call a series of Add methods. Therefore, they are executed similarly. However, do not initialize the List more than you need, because calling the Add method will increase your resource consumption.
Var example:
The following is an example of how to use the var keyword with List <string>. Var is an implicit keyword, which is the same as the result of compiling with a full-type name (var is a newly added keyword in C #3.0. When the compiler can clearly determine the type of a variable, it allows inference of local types ).
List Example Using the var Keyword:
1 using System;
2 using System. Collections. Generic;
3
4 class Program
5 {
6 staticvoid Main ()
7 {
8 var fish = new List <string> (); // Use var keyword for string List
9 fish. Add ("catfish"); // Add string 1
10 fish. Add ("rainbowfish"); // 2
11 fish. Add ("labyrinth fish"); // 3
12 fish. Sort (); // Sort string list alphabetically
13
14 foreach (string fishSpecies in fish)
15 {
16 Console. WriteLine (fishSpecies );
17}
18 Console. ReadKey ();
19}
20}
Output:
Catfish
Labyrinth fish
Rainbowfish
Note. By default, the Sort method of List <string> sorts the strings alphabetically. It uses the replacement method to implement sorting, meaning that you do not have to allocate a new bucket for sorting results.
Summary
The above are some examples of string type List. Because the generic type is designed in the C # language, there is no costly packing and unpacking process in these examples. Therefore, the List here is compared with the ArrayList, it is more efficient under any circumstances. In this article, we learned how to declare and use collection to initialize the List of string types, also learned its Sort method, and finally a sample program using List as a parameter.