List.AddRange inline declaration
-
22-09-2019 - |
Question
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']);
or
var l := new List<String>;
l.AddRange(['A','B','C'];
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);
Solution
var l=new List<String>() {"A","B","C"};
this will work
OTHER TIPS
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();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow