Adding an Array in a List : AddRange() method



How to add an array in a list in c#?

We can use ArrayRange() method to add an array in a list. This method adds all the elements from an array or antoher collection to list. 

Signature: 

void AddRange(IEnumerable<T> collection)

Example:

string[] fruits = new string[4]{ "Mango", "Banana", "Orange", "Papaya" };

var popularFruits= new List<string>();

// adding an array in a List
popularFruits.AddRange(fruits);

var favouriteFruits = new List<string>();

// adding a List 
favouriteFruits.AddRange(popularFruits);

0 Comment's

Comment Form

Submit Comment