Home »
C# Tutorial
C# List<T>.AddRange() Method with Example
C# List<T>.AddRange() Method: In this tutorial, we will learn about the AddRange() method of List collection with its usage, syntax, and an example using C# program.
By IncludeHelp Last updated : April 15, 2023
C# List<T>.AddRange() Method
List<T>.AddRange() method is used to add the objects/elements of a specified collection at the end of the list.
Syntax
void List<T>.AddRange(IEnumerable<T> collection);
Parameter(s)
It accepts a collection of elements (like, arrays) of T type to add in the List.
Return Value
It returns nothing – it's return type is void
Example
int list declaration:
List<int> a = new List<int>();
int array to be added to the list:
int[] int_arr = { 100, 200, 300, 400 };
Adding elements:
a.AddRange(int_arr);
Output:
100 200 300 400
C# program to add items to the list using List<T>.AddRange() method
using System;
using System.Text;
using System.Collections.Generic;
namespace Test {
class Program {
static void Main(string[] args) {
//integer list
List <int> a = new List <int> ();
//int array to add in the list
int[] int_arr = {100, 200, 300, 400 };
//adding elements
a.Add(10);
a.Add(20);
//adding range
a.AddRange(int_arr);
//printing elements
Console.WriteLine("list elements are...");
foreach(int item in a) {
Console.Write(item + " ");
}
Console.WriteLine();
//string list
List <string> b = new List <string> ();
//string array to add in the list
string[] str_arr = {"Abhi", "Radib", "Prem"};
//adding elements
b.Add("Manju");
b.Add("Amit");
//adding range
b.AddRange(str_arr);
//printing elements
Console.WriteLine("list elements are...");
foreach(string item in b) {
Console.Write(item + " ");
}
Console.WriteLine();
//hit ENTER to exit
Console.ReadLine();
}
}
}
Output
list elements are...
10 20 100 200 300 400
list elements are...
Manju Amit Abhi Radib Prem