C#-如何在同一行中初始化List<string[]>
c#
我想初始化一个由同一行中的字符串数组组成的列表。我怎样才能做到这一点?
public List<string[]> list = new List<string>(/** What should I put here? */);
回答
public List<string[]> list = new List<string[]> {
new [] {"a","b","C"},
new string[0],
new [] {"d"}
};
不过话说回来:
- 公共领域通常是个坏主意
- 带有 setter 的列表/集合成员通常是个坏主意
- 在构造函数中初始化和填充集合通常是一个坏主意
- @Developer505 usually the contents are for the *caller* to define, plus it can really mess up serialization - several serialization frameworks assume that collections will be empty, so if you deserialize an object, you could end up with the .ctor items *plus* the ones from the payload