List.AddRange inline declaration

This may seem an easy question, but not to me, also a search has led to nothing. Up until now the only .net programming I have done is with Delphi Prism. With Prism I can do things like:

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


var l := new List<String>;

but can I do a similar thing in C#, or do I have to do it like:

var a = new String[] {"A","B","C"};
var l = new List<String>(a);


 var l=new List<String>() {"A","B","C"};  

this will work

collection initializer:

var list = new List<string>

or correct ctor (mixed with collection initializer):

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

You can use Collection Initializers to achieve desired result.

As mentioned above, use collection initializers. In addition, if you are looking to convert from string[] to List , you can use the ToList() extension method in the System.Linq namespace like so:

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

Need Your Help

Bash checking if folder has contents

bash if-statement directory

I'm trying to create a Bash script that will delete everything in my .waste directory. I have a basic script I wrote but I want it to first check if the .waste directory has contents, and if so, to...