Generics learn the list<string> generic class example in the second day of--c#

Source: Internet
Author: User

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:

1 using System;
2 using System.Collections.Generic;

4 Class Program
5 {
6 static void Main ()
7 {
8 list<string> cities = new list<string> ();//List of city names
9 cities. ADD ("San Diego"); String element 1
Ten cities. ADD ("Humboldt"); 2
One by one cities. ADD ("Los Angeles"); 3
cities. ADD ("Auburn"); 4

//Write each city string.
-foreach (String City in cities)
+ {
Console.WriteLine (city);
(+ }
Console.readkey ();
( }
21}

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:

1 using System;
2 using System.Collections.Generic;

4 Class Program
5 {
6 static void Main ()
7 {
8 list<string> moths = new list<string>
9 {
Ten "African Armyworm",
"Mottled Pug",
"Purple Thug",
"Short-cloaked Moth"
+ };
The List moth contains four strings.
+ //IL:
+ //
//list<string> <>g__initlocal0 = new list<string> ();
+ //<>g__initlocal0.add ("African Armyworm");
+///... four more ADD calls
/ /list<string> moths = <>g__initLocal0;
+ }
23}

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:

1 using System;
2 using System.Collections.Generic;

4 Class Program
5 {
6 static void Main ()
7 {
8 var fish = new list<string> ();//use var keyword for string List
9 fish. ADD ("Catfish"); ADD String 1
Ten fish. ADD ("rainbowfish"); 2
Fish. ADD ("Labyrinth Fish"); 3
Fish. Sort (); Sort String List Alphabetically

+ foreach (string fishspecies in fish)
{
Console.WriteLine (fishspecies);
+ }
Console.readkey ();
+ }
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.

This article is translated from:C # String List Examples compared to the original text, the translation is modified appropriately.

Generics learn the list<string> generic class example in the second day of--c#

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.