static initialization of lists

  • Thread starter Thread starter jeffc
  • Start date Start date
J

jeffc

I have the following declaration, and I want to initialize the list at the
same time. Is there a way?
private static List<String> FILE_LIST = new List<String>();
 
I have the following declaration, and I want to initialize the list at the
same time. Is there a way?
private static List<String> FILE_LIST = new List<String>();

Not in C# 1 or 2 :(

Actually, that's not quite true... it's ugly as heck, but this works:

static readonly List<string> FileList = new List<string>
(new string[] {"First file", "Second file"});

In C# 3 you can use collection initializers:

static readonly List<string> FileList = new List<string>
{
"First file"
"Second file"
};

Much nicer :)

Jon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top