Home >> Blog >> c# list 教學

偶爾在編寫SEO的特定優化程式過程中,我們會遇到c# list到底要如何正確使用的狀況,一起來了解。

c# list 教學

C# List 類表示可以通過索引訪問的強類型對象的集合。在本教學中,我們將學習如何使用 C# List 類在 C# 中使用列表,以使用 List 類方法和屬性在對象集合中添加、查找、排序、反轉和搜索項目。

什麼是 C# 列表?

C# 中的 List 類表示對象的強類型列表。List 提供創建對象列表、查找列表項、排序列表、搜索列表和操作列表項的功能。在 List 中,T 是對象的類型。

List 中的“T”是什麼?

List 類表示一個強類型的對象列表。List 提供創建對象列表、查找列表項、排序列表、搜索列表和操作列表項的功能。在 List 中,T 是對象的類型。

如何在 C# 中創建列表?

List 是一個泛型類,在 System.Collections.Generic 命名空間中定義。您必須在項目中導入此命名空間才能訪問 List 類。

1 using System.Collections.Generic;

List 類構造函數用於創建 T 類型的 List 對象。它可以為空,也可以將 Integer 值作為定義列表初始大小(也稱為容量)的參數。如果構造函數中沒有傳遞整數,則列表的大小是動態的,並且每次將項目添加到數組時都會增長。您還可以在初始化對象時傳遞元素的初始集合。

清單 1 中的程式碼片段創建了一個 Int16 列表和一個字符串類型列表。程式碼的最後一部分使用現有集合創建一個 List 對象。

// List with default capacity
List< Int16> list = new List< Int16>();
// List with capacity = 5
List < string > authors = new List< string>(5);
string[] animals = { "Cow", "Camel", "Elephant" };
List< string > animalsList = new List< string>(animals);

清單 1

從清單 1 中可以看出,List 的初始容量僅設置為 5。但是,當列表中添加超過 5 個元素時,它會自動展開。

如何將項目添加到 C# 列表?

Add 方法將元素添加到 List。清單 2 中的程式碼片段創建了兩個 List 對象,並分別向它們添加整數和字符串項。

// Dynamic ArrayList with no size limit
List numberList = new List();
numberList.Add(32);
numberList.Add(21);
numberList.Add(45);
numberList.Add(11);
numberList.Add(89);
// List of string
List authors = new List(5);
authors.Add("Mahesh Chand");
authors.Add("Chris Love");
authors.Add("Allen O'neill");
authors.Add("Naveen Sharma");
authors.Add("Monica Rathbun");
authors.Add("David McCarter");

清單 2。

我們還可以將集合添加到列表中。AddRange 方法用於將集合添加到列表。清單 3 中的程式碼片段將字符串數組添加到 List。

// Collection of string
string[] animals = { "Cow", "Camel", "Elephant" };
// Create a List and add a collection
List animalsList = new List();
animalsList.AddRange(animals);
foreach (string a in animalsList)
Console.WriteLine(a);

清單 3。

如何讀取 C# 列表項?

列表是項目的集合。我們可以使用 foreach 循環來遍歷它的項目。清單 6 中的程式碼片段讀取 List 的所有項並顯示在控制台上。

foreach (string a in authors)
Console.WriteLine(a);

清單 4。

清單 4 的輸出類似於圖 1。

c-sharp-list

要檢索列表中特定位置的項目,我們可以使用集合的索引。以下程式碼片段讀取列表中的第 3 項。

1 Console.WriteLine(authors[2]);

如何使用 C# 列表屬性

列表類屬性包括以下內容:

  • 容量 – List 可以包含的元素數。List 的默認容量為 0。
  • Count – List 中的元素數。

清單 5 中的程式碼片段獲取這些屬性的值。

ArrayList authorsArray = new ArrayList();
authorsArray.Add("Mahesh Chand");
authorsArray.Add("Praveen Kumar");
authorsArray.Add("Raj Kumar");
authorsArray.Add("Dinesh Beniwal");
authorsArray.Add("David McCarter");
Console.WriteLine("Count: " + authors.Count);
Console.WriteLine("Capacity: " + authors.Capacity);

清單 5。

如何在 C# 列表中的某個位置插入元素?

List 類的 Insert 方法在給定位置插入一個對象。該方法的第一個參數是 List 中的第 0 個索引。

InsertRange 方法可以在給定位置插入一個集合。

清單 6 中的程式碼片段在 List 的第 3 個位置插入一個字符串,在第 2 個位置插入一個數組。

authors.Insert(3, "Bill Author");
// Collection of new authors
string[] newAuthors = { "New Author1", "New Author2", "New Author3" };
// Insert array at position 2
authors.InsertRange(2, newAuthors);

清單 6。

如何從 C# 列表中刪除項目?

st 類提供了可用於刪除一個項目或一系列項目的 Remove 方法。

Remove 方法刪除列表中給定項目的第一次出現。以下程式碼片段刪除了第一次出現的“New Author1”。

// Remove an item
authors.Remove("New Author1");

RemoveAt 方法刪除給定位置的項目。以下程式碼片段刪除了第三個位置的項目。

// Remove 3rd item
authors.RemoveAt(3);

RemoveRange 方法將項目列表從起始索引中刪除到項目數。以下程式碼片段從第三個位置開始刪除兩個項目。

// Remove a range
authors.RemoveRange(3, 2);

Clear 方法從 List 中刪除所有項目。以下程式碼片段從列表中刪除所有項目。

// Remove all items authors.Clear();

如何在 C# 列表中查找元素?

IndexOf 方法在列表中查找項目。如果在 List 中沒有找到任何項目,IndexOf 方法將返回 -1。

以下程式碼片段查找一個字符串並返回項目的匹配位置。

int idx = authors.IndexOf("Naveen Sharma");
if (idx > 0)
Console.WriteLine($"Item index in List is: {idx}");
else
Console.WriteLine("Item not found");

我們還可以在 List 中指定 IndexOf 方法可以開始搜索的位置。

例如,以下程式碼片段查找從字符串中的第 3 個位置開始的字符串。

1 Console.WriteLine(authors.IndexOf("Naveen Sharma", 2));

LastIndexOf 方法從 List 的末尾找到一個項目。

以下程式碼片段向後查找字符串,如果找到則返回項目的索引。

Console.WriteLine(authors.LastIndexOf("Mahesh Chand"));

清單 7 中列出了完整的示例。

// List of string
List< string> authors = new List< string>(5);
authors.Add("Mahesh Chand");
authors.Add("Chris Love");
authors.Add("Allen O'neill");
authors.Add("Naveen Sharma");
authors.Add("Mahesh Chand");
authors.Add("Monica Rathbun");
authors.Add("David McCarter");
int idx = authors.IndexOf("Naveen Sharma");
if (idx > 0)
Console.WriteLine($"Item index in List is: {idx}");
else
Console.WriteLine("Item not found");
Console.WriteLine(authors.IndexOf("Naveen Sharma", 2));
Console.WriteLine(authors.LastIndexOf("Mahesh Chand"));

清單 7。

如何對 C# List 元素進行排序?

List 的 Sort 方法使用 QuickSort 算法對 List 的所有項目進行排序。

清單 8 中的以下程式碼示例對 List 項進行排序,並顯示 List 項的原始順序和排序順序。

// List of string
List< string> authors = new List< string>(5);
authors.Add("Mahesh Chand");
authors.Add("Chris Love");
authors.Add("Allen O'neill");
authors.Add("Naveen Sharma");
authors.Add("Mahesh Chand");
authors.Add("Monica Rathbun");
authors.Add("David McCarter");
Console.WriteLine("Original List items");
Console.WriteLine("===============");
// Print original order
foreach (string a in authors)
Console.WriteLine(a);
// Sort list items
authors.Sort();
Console.WriteLine();
Console.WriteLine("Sorted List items");
Console.WriteLine("===============");
// Print sorted items
foreach (string a in authors)
Console.WriteLine(a);

清單 8。

清單 8 的輸出類似於圖 2。

c-sharp-list

如何反轉 C# List 元素?

List 的 Reverse 方法反轉 List 中所有項目的順序。

以下程式碼片段反轉了一個列表。

// List of string
List authors = new List(5);
authors.Add("Mahesh Chand");
authors.Add("Chris Love");
authors.Add("Allen O'neill");
authors.Add("Naveen Sharma");
authors.Add("Mahesh Chand");
authors.Add("Monica Rathbun");
authors.Add("David McCarter");
Console.WriteLine("Original List items");
Console.WriteLine("===============");
// Print original order
foreach (string a in authors)
Console.WriteLine(a);
// Reverse list items
authors.Reverse();
Console.WriteLine();
Console.WriteLine("Sorted List items");
Console.WriteLine("===============");
// Print reversed items
foreach (string a in authors)
Console.WriteLine(a);

清單 9。

清單 9 的輸出類似於圖 3。

c-sharp-list

如何搜索 C# List 元素?

List 的 BinarySearch 方法搜索排序列表並返回找到的項目的從零開始的索引。必須先對 List 進行排序,然後才能使用此方法。

以下程式碼片段返回列表中字符串的索引。

int bs = authors.BinarySearch("Mahesh Chand");

如何從另一個列表導入列表中的添加元素?

您可以使用 List 的 AddRange 方法將一個列表的項目導入另一個列表。但請確保兩個列表中的項目類型相同。以下程式碼片段創建兩個 List 對象並將 listTwo 的所有項目複製到 listOne 中。

// Program: Copy items from one list to another list
Console.WriteLine("Import one list to another!");

// Create List1
List listOne = new();
listOne.Add("One");
listOne.Add("Two");
listOne.Add("Three");
listOne.Add("Four");
listOne.Add("Five");

// Create List2
List listTwo = new();
listTwo.Add("A");
listTwo.Add("B");
listTwo.Add("C");

// Add List2 to List1
listOne.AddRange(listTwo);

// Display
foreach(string item in listOne)
Console.WriteLine(item);

Console.ReadKey();

總結

本文章show出了如何使用 List 類來處理對象集合。本文還演示瞭如何在 List 中添加、查找、搜索、排序和反轉項目。