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
Insert more than one element at once in a C# List
The InsertRange() method in C# allows you to insert multiple elements into a List<T> at a specified position. This method is useful when you need to add a collection of elements at once rather than adding them one by one.
Syntax
Following is the syntax for the InsertRange() method −
public void InsertRange(int index, IEnumerable<T> collection)
Parameters
-
index − The zero-based index at which the new elements should be inserted.
-
collection − The collection whose elements should be inserted into the List<T>. The collection itself cannot be null, but it can contain elements that are null.
Using InsertRange() to Insert at the End
When you insert elements at the end of a list, specify the count of the list as the index −
using System;
using System.Collections.Generic;
public class Demo {
public static void Main() {
List<int> arr1 = new List<int>();
arr1.Add(10);
arr1.Add(20);
arr1.Add(30);
arr1.Add(40);
arr1.Add(50);
Console.WriteLine("Initial List...");
foreach (int i in arr1) {
Console.WriteLine(i);
}
int[] arr2 = new int[4];
arr2[0] = 60;
arr2[1] = 70;
arr2[2] = 80;
arr2[3] = 90;
arr1.InsertRange(5, arr2);
Console.WriteLine("After adding elements...");
foreach (int i in arr1) {
Console.WriteLine(i);
}
}
}
The output of the above code is −
Initial List... 10 20 30 40 50 After adding elements... 10 20 30 40 50 60 70 80 90
Using InsertRange() to Insert in the Middle
You can insert elements at any position within the list. Elements at and after the insertion point are shifted to accommodate the new elements −
using System;
using System.Collections.Generic;
public class Demo {
public static void Main() {
List<string> fruits = new List<string> {"Apple", "Banana", "Cherry"};
Console.WriteLine("Original list:");
foreach (string fruit in fruits) {
Console.WriteLine(fruit);
}
string[] newFruits = {"Mango", "Orange", "Grapes"};
fruits.InsertRange(1, newFruits);
Console.WriteLine("After inserting at index 1:");
foreach (string fruit in fruits) {
Console.WriteLine(fruit);
}
}
}
The output of the above code is −
Original list: Apple Banana Cherry After inserting at index 1: Apple Mango Orange Grapes Banana Cherry
Using InsertRange() with Different Collection Types
The InsertRange() method accepts any IEnumerable<T> collection, including arrays, lists, and other collections −
using System;
using System.Collections.Generic;
public class Demo {
public static void Main() {
List<int> numbers = new List<int> {1, 2, 5, 6};
Console.WriteLine("Original list:");
foreach (int num in numbers) {
Console.Write(num + " ");
}
Console.WriteLine();
// Insert another list
List<int> middleNumbers = new List<int> {3, 4};
numbers.InsertRange(2, middleNumbers);
Console.WriteLine("After inserting list at index 2:");
foreach (int num in numbers) {
Console.Write(num + " ");
}
Console.WriteLine();
}
}
The output of the above code is −
Original list: 1 2 5 6 After inserting list at index 2: 1 2 3 4 5 6
Conclusion
The InsertRange() method provides an efficient way to insert multiple elements into a C# List at a specific position. It accepts any IEnumerable<T> collection and automatically shifts existing elements to make room for the new ones.
