Ign*_*cia 131 .net c# initialization list
今天我很惊讶地发现在C#中我能做到:
List<int> a = new List<int> { 1, 2, 3 };
Run Code Online (Sandbox Code Playgroud)
我为什么要这样做?调用什么构造函数?我怎么能用我自己的课程做到这一点?我知道这是初始化数组的方法,但数组是语言项,列表是简单的对象......
Jam*_*are 183
这是.NET中集合初始化程序语法的一部分.您可以在创建的任何集合上使用此语法,只要:
它实现IEnumerable
(最好IEnumerable<T>
)
它有一个名为的方法 Add(...)
发生的是调用默认构造函数,然后Add(...)
为初始化程序的每个成员调用.
因此,这两个块大致相同:
List<int> a = new List<int> { 1, 2, 3 };
Run Code Online (Sandbox Code Playgroud)
和
List<int> temp = new List<int>();
temp.Add(1);
temp.Add(2);
temp.Add(3);
List<int> a = temp;
Run Code Online (Sandbox Code Playgroud)
您可以拨打一个可选的构造,如果你想要的,例如,以防止过度大小的List<T>
增长,等时:
// Notice, calls the List constructor that takes an int arg
// for initial capacity, then Add()'s three items.
List<int> a = new List<int>(3) { 1, 2, 3, }
Run Code Online (Sandbox Code Playgroud)
请注意,该Add()
方法不需要采用单个项目,例如采用两个项目的Add()
方法Dictionary<TKey, TValue>
:
var grades = new Dictionary<string, int>
{
{ "Suzy", 100 },
{ "David", 98 },
{ "Karen", 73 }
};
Run Code Online (Sandbox Code Playgroud)
大致相同:
var temp = new Dictionary<string, int>();
temp.Add("Suzy", 100);
temp.Add("David", 98);
temp.Add("Karen", 73);
var grades = temp;
Run Code Online (Sandbox Code Playgroud)
因此,要将此添加到您自己的类中,您所需要做的就是实现IEnumerable
(再次,最好IEnumerable<T>
)并创建一个或多个Add()
方法:
public class SomeCollection<T> : IEnumerable<T>
{
// implement Add() methods appropriate for your collection
public void Add(T item)
{
// your add logic
}
// implement your enumerators for IEnumerable<T> (and IEnumerable)
public IEnumerator<T> GetEnumerator()
{
// your implementation
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
Run Code Online (Sandbox Code Playgroud)
然后就像BCL集合那样使用它:
public class MyProgram
{
private SomeCollection<int> _myCollection = new SomeCollection<int> { 13, 5, 7 };
// ...
}
Run Code Online (Sandbox Code Playgroud)
(有关更多信息,请参阅MSDN)
根据C#Version 3.0规范 "应用集合初始值设定项的集合对象必须是一个为一个T实现System.Collections.Generic.ICollection的类型."
但是,在撰写本文时,此信息似乎不准确; 请参阅Eric Lippert在下面的评论中的澄清.
关于集合初始化器的另一个很酷的事情是你可以有多个Add
方法的重载,你可以在同一个初始化器中调用它们!例如,这有效:
public class MyCollection<T> : IEnumerable<T>
{
public void Add(T item, int number)
{
}
public void Add(T item, string text)
{
}
public bool Add(T item) //return type could be anything
{
}
}
var myCollection = new MyCollection<bool>
{
true,
{ false, 0 },
{ true, "" },
false
};
Run Code Online (Sandbox Code Playgroud)
它调用正确的重载.此外,它只查找名称的方法Add
,返回类型可以是任何东西.
归档时间: |
|
查看次数: |
22684 次 |
最近记录: |