sha*_*oth 0 .net c# collections
我需要一个或多或少等同于C++的.NET 3.5类std::vector:
早些时候我用过ArrayList,它是正确的,除了它存储object,我必须将检索到的对象转换为正确的类型,我可以添加任何东西,这使编译时类型检查更难.
有没有像ArrayList包含类型参数化的东西?
听起来像你在追求List<T>.例如,要创建整数列表:
List<int> integers = new List<int>();
integers.Add(5); // No boxing required
int firstValue = integers[0]; // Random access
// Iteration
foreach (int value in integers)
{
Console.WriteLine(value);
}
Run Code Online (Sandbox Code Playgroud)
请注意,您可能希望揭露这种名单通过IEnumerable<T>,ICollection<T>或IList<T>而不是通过具体类型.
您不需要.NET 3.5 - 它们是在.NET 2中引入的(这是将泛型作为一项功能引入的时候).但是,在.NET 3.5中,LINQ可以更轻松地处理任何类型的序列:
IEnumerable<int> evenIntegers = integers.Where(x => x % 2 == 0);
Run Code Online (Sandbox Code Playgroud)
(以及更多).