C# - How can I initilalize List<string[]> in the same line

Viewed 53

I want to initialize a list consisting of string arrays in the same line. How can I do that?

public List<string[]> list = new List<string>(/** What should I put here? */);
1 Answers
public List<string[]> list = new List<string[]> {
    new [] {"a","b","C"},
    new string[0],
    new [] {"d"}
};

That said, however:

  • public fields are usually a bad idea
  • list/collection members with setters are often a bad idea
  • initializing and populating collections in a constructor is usually a bad idea
Related