我有以下的代码
var list = new List<IMyCustomType>();
list.Add(new MyCustomTypeOne());
list.Add(new MyCustomTypeTwo());
list.Add(new MyCustomTypeThree());
这当然可行,但我想知道:如何使用一条语句声明列表并填充值?
谢谢
我有以下的代码
var list = new List<IMyCustomType>();
list.Add(new MyCustomTypeOne());
list.Add(new MyCustomTypeTwo());
list.Add(new MyCustomTypeThree());
这当然可行,但我想知道:如何使用一条语句声明列表并填充值?
谢谢
var list = new List<IMyCustomType>{
new MyCustomTypeOne(),
new MyCustomTypeTwo(),
new MyCustomTypeThree()
};
编辑:提问者将“一行”更改为“一条语句”,这看起来更好。
var list = new List<IMyCustomType>
{
new MyCustomTypeOne(),
new MyCustomTypeTwo(),
new MyCustomTypeThree()
};
var list = new List<IMyCustomType>
{
new MyCustomTypeOne(){Properties should be given here},
new MyCustomTypeTwo(){Properties should be given here},
new MyCustomTypeThree(){Properties should be given here},
}
var list = new List<IMyCustomType>() { new MyCustomTypeOne(), new MyCustomTypeTwo(), new MyCustomTypeThree() };
var list = new List<IMyCustomType>{ new MyCustomTypeOne(), new MyCustomTypeTwo() };
var list = new List<IMyCustomType>(); list.Add(new MyCustomTypeOne()); list.Add(new MyCustomTypeTwo()); list.Add(new MyCustomTypeThree());
放在一行上。或者你是指 一个语句。 - David Heffernan