在 C# 中,List<T>
是一个强类型的泛型集合,可以存储任意类型的数据,但存储的类型必须在创建列表时指定(即通过类型参数 <T>
定义)。
可以存储的类型
-
值类型(Value Types)
- 例如:
int
、float
、bool
、char
、struct
等。 List<int>
、List<double>
、List<struct>
等都是合法的。- 存储值类型时,数据直接保存在列表中。
示例:
List<int> numbers = new List<int> { 1, 2, 3, 4, 5 };
- 例如:
-
引用类型(Reference Types)
- 例如:
string
、class
、object
等。 List<string>
、List<object>
、List<MyClass>
等也是合法的。- 存储引用类型时,列表存储的是引用,而不是对象本身。
示例:
List<string> names = new List<string> {
- 例如: