Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Adding the elements of the specified collection to the end of the List in C#
The AddRange() method in C# allows you to add all elements from a specified collection to the end of a List<T>. This method is more efficient than adding elements one by one using Add() when you need to append multiple items from an existing collection.
Syntax
Following is the syntax for the AddRange() method −
public void AddRange(IEnumerable<T> collection)
Parameters
collection − The collection whose elements should be added to the end of the List. The collection itself cannot be null, but it can contain elements that are null.
Using AddRange() with String Arrays
Example
using System;
using System.Collections.Generic;
public class Demo {
public static void Main(String[] args) {
List<string> list = new List<string>();
list.Add("Andy");
list.Add("Gary");
list.Add("Katie");
list.Add("Amy");
Console.WriteLine("Elements in List...");
foreach (string res in list) {
Console.WriteLine(res);
}
string[] strArr = { "John", "Jacob" };
list.AddRange(strArr);
Console.WriteLine("Elements in List...UPDATED");
foreach(String str in list) {
Console.WriteLine(str);
}
}
}
The output of the above code is −
Elements in List... Andy Gary Katie Amy Elements in List...UPDATED Andy Gary Katie Amy John Jacob
Using AddRange() with Integer Arrays
Example
using System;
using System.Collections.Generic;
public class Demo {
public static void Main(String[] args) {
List<int> list = new List<int>();
list.Add(25);
list.Add(50);
list.Add(75);
list.Add(100);
Console.WriteLine("Elements in List...");
foreach (int val in list) {
Console.WriteLine(val);
}
int[] intArr = { 150, 200, 250, 300 };
list.AddRange(intArr);
Console.WriteLine("Elements in List...UPDATED");
foreach(int val in list) {
Console.WriteLine(val);
}
}
}
The output of the above code is −
Elements in List... 25 50 75 100 Elements in List...UPDATED 25 50 75 100 150 200 250 300
Using AddRange() with Another List
Example
using System;
using System.Collections.Generic;
public class Demo {
public static void Main(String[] args) {
List<string> fruits = new List<string> { "Apple", "Banana" };
List<string> moreFruits = new List<string> { "Orange", "Grapes", "Mango" };
Console.WriteLine("Original fruits:");
foreach (string fruit in fruits) {
Console.WriteLine(fruit);
}
fruits.AddRange(moreFruits);
Console.WriteLine("After AddRange:");
foreach (string fruit in fruits) {
Console.WriteLine(fruit);
}
}
}
The output of the above code is −
Original fruits: Apple Banana After AddRange: Apple Banana Orange Grapes Mango
Key Benefits of AddRange()
Performance − More efficient than multiple
Add()calls as it adds elements in bulk.Flexibility − Accepts any
IEnumerable<T>collection including arrays, lists, and LINQ query results.Simplicity − Single method call instead of loops to add multiple elements.
Conclusion
The AddRange() method provides an efficient way to append multiple elements from any collection to a List in C#. It accepts arrays, other lists, or any IEnumerable collection, making it a versatile tool for combining collections efficiently.
