When you use a series of strings (strings) in C # code and need to create a list for them, the,list<string> generic class is a very good solution for storing a series of strings (strings). Here are some examples of list<string> generic classes, let's take a look at them.
List example
Here is an example of a list that uses C # to create a new series of strings, using a foreach statement to iterate through each of its strings. Notice that the desired namespace is added at the top of the code fragment: "using System.Collections.Generic;", and list is a generic type in the namespace.
List<string> Sample code:
1UsingSystem;
2UsingSystem.Collections.Generic;
3
4ClassProgram
5{
6 Static voidMain ()
7{
8List<String>Cities= NewList<String>();//List of city names
9Cities. ADD ("San Diego");//String element 1
10Cities. ADD ("Humboldt");//2
11Cities. ADD ("Los Angeles");//3
12Cities. ADD ("Auburn");//4
13
14 //Write each city string.
-foreach (string city in cities)
+ {
Console.WriteLine (city);
(+ }
Console.readkey ();
( }
+ }
Output:
San Diego
Humboldt
Los Angeles
Auburn
Note the angle brackets (angle brackets) in the code. In the declaration statement, angle brackets < and > enclose the string type in the middle, which means that list can only store elements of type string. The string type can be a string of lowercase letters, or a string that can make uppercase fonts.
Implementing an initialization example using collection
C # syntax allows for a clearer way to initialize a list. Using collection for initialization, you must enclose the value used for initialization with curly braces {}. The comments in the following example illustrate the code that the compiler uses when executing the program.
List Initialization Sample code:
1UsingSystem;
2UsingSystem.Collections.Generic;
3
4ClassProgram
5{
6 Static voidMain ()
7{
8List<String>Moths= NewList<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> ();
+ // <>g__initlocal0.add ("African Armyworm");
+///... four more ADD calls
/ / list<string> moths = <>g__initLocal0;
+ }
(+}
Explain the explanation. You can see that the initialization of the string list is compiled to call a series of add methods. Therefore, they are similar in execution. However, do not exceed your needs to initialize the list too much, because calling the Add method will increase your resource consumption.
var example:
Here is an example of how the VAR keyword can be used with list<string>. var is an implicit keyword that is the same as the result of compiling with a full-type name (Var is a newly added keyword in C # 3.0 that allows the local type to be inferred when the compiler can clearly determine the type of the variable).
list example using the var keyword:
1UsingSystem;
2UsingSystem.Collections.Generic;
3
4ClassProgram
5{
6 Static voidMain ()
7{
8var fish= NewList<String>();//Use var keyword for string List
9Fish. ADD ("Catfish");//ADD String 1
10Fish. ADD ("Rainbowfish");//2
11Fish. ADD ("Labyrinth Fish");//3
12Fish. Sort ();//Sort String List Alphabetically
13
14 foreach (string fishspecies in 15 {
16 Span style= "color: #000000;" > Console.WriteLine (fishspecies);
17 18 Console.readkey ();
19 20 }
Output:
Catfish
Labyrinth Fish
Rainbowfish
attention . The sort method of list<string> sorts its strings alphabetically by default. It uses substitution to implement sorting, meaning that you do not have to allocate new storage space for the results of the sort.
Summarize
Above is a list of string types for some examples. Because generic types are designed in the C # language, these examples do not cost a large boxing and unboxing process, so the list here is more efficient in any case than ArrayList. In this article, we learned to declare and use collection to initialize a list of string types, learn its sort method, and finally a sample program that uses list as a parameter.
Article citation: http://www.cnblogs.com/hans_gis/archive/2011/07/31/2122392.html
Examples of list<string> generic classes in C #