Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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']);

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);
like image 518
AJ. Avatar asked Mar 26 '10 06:03

AJ.


People also ask

What is AddRange in list C#?

An array of strings is created and passed to the constructor, populating the list with the elements of the array. The AddRange method is called, with the list as its argument. The result is that the current elements of the list are added to the end of the list, duplicating all the elements.

What is AddRange in Linq?

List<T>. AddRange(IEnumerable<T>) Method is used to add the elements of the specified collection to the end of the List<T>.

Is the AddRange feature used whenever adding multiple values to collection objects?

AddRange is used to add multiple elements. The add method inserts the item at the end of a collection. AddRange method is used to insert a set of records into a collection. The addRange method is also used to add an array of nodes that are previously created in the collection.


1 Answers

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

this will work

like image 138
RameshVel Avatar answered Sep 22 '22 06:09

RameshVel