C# 中的泛型集合是什麼?
C# 中的泛型集合包括 <List>、<SortedList> 等。
列表
List<T> 是泛型集合,而 ArrayList 是非泛型集合。
我們看一個示例。在此處,列表中有六個元素 −
示例
using System;
using System.Collections.Generic;
class Program {
static void Main() {
// Initializing collections
List myList = new List() {
"one",
"two",
"three",
"four",
"five",
"six"
};
Console.WriteLine(myList.Count);
}
}輸出
6
分類列表
分類列表是陣列和雜湊表的組合。它包含可以使用鍵或索引訪問的專案列表。
我們看一個示例。在此處,SortedList 中有四個元素 −
示例
using System;
using System.Collections;
namespace CollectionsApplication {
class Program {
static void Main(string[] args) {
SortedList sl = new SortedList();
sl.Add("001", "Tim");
sl.Add("002", "Steve");
sl.Add("003", "Bill");
sl.Add("004", "Tom");
if (sl.ContainsValue("Bill")) {
Console.WriteLine("This name is already in the list");
} else {
sl.Add("005", "James");
}
ICollection key = sl.Keys;
foreach (string k in key) {
Console.WriteLine(k + ": " + sl[k]);
}
}
}
}輸出
This name is already in the list 001: Tim 002: Steve 003: Bill 004: Tom
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP