除非你知道不同,否则请使用aList<T>.List在幕后实现所谓的动态数组,这是一个按需增加大小的常规数组.
用法示例:
// Create an integer array
List<int> list = new List<int>();
// Add 5 numbers
list.Add(1);
list.Add(5);
list.Add(3);
list.Add(4);
list.Add(2);
// Get value at index 0 and write to console
Console.WriteLine(list[0]); // 1
Run Code Online (Sandbox Code Playgroud)
以下是使用.NET 3.5中引入的LINQ的一些更高级的用法.
// Get all values less than 4 and put into an 'IEnumerable'
var lessThanFour = list.Where(e => e < 4);
// Write all values less than 4 to the console
foreach (var val in lessThanFour)
{
Console.WriteLine(val);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1643 次 |
| 最近记录: |