[RESOLVED] Initialising a List based collection:
Hi folks,
The following code will compile OK:
Code:
//Method 1
String[] myStrings = {"String_1", "String_2", "String_3"};
List<String> myList = new List<String>(myStrings);
But I'm wondering if it's possible to declare and initialise a List all in one line of code something like this:
Code:
//Method 2
List<String> myList = new List<String>("String_1", "String_2", "String_3");
I've looked through the documentation for working with list based collections but I didn't see any constructors or methods for doing it via method 2. I'm assuming then that it's not possible to do it that way, but If that's the case could someone give me a reason why that's not allowed?
Thanks again for your help.
Re: Initialising a List based collection:
Code:
List<string> list = new List<string>(new string[] { "string1", "string2", "string3" });
Re: Initialising a List based collection:
Thanks again jmcilhinney :thumb:
As soon as I read your answer I thought "Now why didn't I think of that!!!" :(
Should I mark this post and my others as being resolved when ever I get a satisfactory answer?
Re: Initialising a List based collection:
Quote:
Originally Posted by The Thing
Should I mark this post and my others as being resolved when ever I get a satisfactory answer?
Yes you should. Use the Thread Tools menu for ease.