编写内联集合初始值设定项的最简洁/最短方法是什么?
我不关心引用名,索引就可以,item只需要在方法范围内使用即可。
我认为匿名类型集合会更困惑,因为我每次都必须继续写键名。
我现在有
var foo = new Tuple<int, string, bool>[]
{
new Tuple<int, string, bool>(1, "x", true),
new Tuple<int, string, bool>(2, "y", false)
};
我希望 C# 4.0 能有一些我错过的东西。
最佳答案
最短的方法是使用 Tuple.Create
而不是 new Tuple
:
var foo = new [] { Tuple.Create(1, "x", true), Tuple.Create(2, "y", false) };
关于c# - 最短的内联集合初始值设定项? C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7158649/