C#中的List<string>泛型類樣本

來源:互聯網
上載者:User

標籤:

在C#代碼中使用一系列字串(strings)並需要為其建立一個列表時,List<string>泛型類是一個用於儲存一系列字 符串(strings)的極其優秀的解決辦法。下面一起有一些List<string>泛型類的樣本,一起來看看吧。

  List樣本

  下面是一個使用C#建立一個新的一系列字串的列表的樣本,利用foreach語句迴圈使用其每一個字串。請注意在程式碼片段的頂部添加所需的命名空間:“using System.Collections.Generic;”,List是該命名空間裡的一個泛型型別。

List<string>範例程式碼:

 1 using System;
2 using System.Collections.Generic;
3
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
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 }

輸出:

San Diego
Humboldt
Los Angeles
Auburn

  注意代碼中的角括弧(angle brackets)。在聲明語句中角括弧<和>將string類型圍在中間,這意味著List僅能夠儲存String類型的元素。string類型可以是小寫字型的string,也可以使大寫字型的String。

  使用Collection實現初始化樣本

  C#文法允許以一種更加清晰的辦法來實現List的初始化。使用collection進行初始化,必須使用大括弧{}包圍作初始化用的值。下面樣本中的注釋說明了在執行該程式時編譯器所使用的代碼。

List初始化範例程式碼:

 1 using System;
2 using System.Collections.Generic;
3
4 class Program
5 {
6 static void 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 calls
21 // List<string> moths = <>g__initLocal0;
22 }
23 }

  解釋說明。可以看到字串列表的初始化編譯為調用一系列的Add方法。因此,二者執行起來是相似的。然而,不要超出你的需要來過多的初始化List,因為調用Add方法會增加你的資源消耗。

  Var樣本:

  下面是一個關於var關鍵字如何與List<string>一起使用的樣本。var是一個隱式關鍵字,它與使用全類型名稱編譯的結果是相同的(var是C# 3.0中新增加的一個關鍵字,在編譯器能明確判斷變數的類型時,它允許對本地類型進行推斷)。

使用var關鍵字的List樣本:

 1 using System;
2 using System.Collections.Generic;
3
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
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 }

輸出:

catfish
labyrinth fish
rainbowfish

  注意。List<string>的Sort方法預設按照字母順序對其字串進行排序。它使用替換的方式實現排序,意味著你不必為排序的結果分配新的儲存空間。

  總結

  上面是字串類型的List的一些樣本。因為C#語言中設計了泛型型別,這些樣本中沒有花費較大的裝箱與拆箱過程,因此,這裡的List與 ArrayList相比,在任何情況下其效率都要高一些。在這篇文章裡,我們學習了聲明並使用collection對字串類型的List進行初始化,還 學習了其Sort方法,最後還有一個使用List作為參數的樣本程式。

 

文章引自:http://www.cnblogs.com/hans_gis/archive/2011/07/31/2122392.html

C#中的List<string>泛型類樣本

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.