Introductory Declaration List.AddRange

This may seem like an easy question, but not for me, and the search came to nothing. So far, the only .net programming I have done is Delphi Prism. With Prism, I can do things like:

var l := new List<String>(['A','B','C']); 

or

 var l := new List<String>; l.AddRange(['A','B','C']; 

but can I do a similar thing in C #, or do I need to do this like:

 var a = new String[] {"A","B","C"}; var l = new List<String>(a); 
+7
c # delphi oxygene delphi-prism
source share
4 answers
  var l=new List<String>() {"A","B","C"}; 

it will work

+22
source share

collection initializer:

 var list = new List<string> { "A", "B", "C" }; 

or fix ctor (mixed with collection initializer):

 var list = new List<string>(new [] { "A", "B", "C" }); 
  • msdn for ctor information
  • msdn for collection initializer
+2
source share

You can use Collection Initializers to achieve the desired result.

+1
source share

As mentioned above, use collection initializers. Alternatively, if you want to convert from a string [] to a list, you can use the ToList () extension method in the System.Linq namespace as follows:

 string[] s = { "3", "4", "4"}; List<string> z = s.ToList(); 
0
source share

All Articles