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 an element into the ArrayList at the specified index in C#
The ArrayList.Insert() method in C# allows you to insert an element at a specific index position in an ArrayList. This method shifts all existing elements from the specified index to the right, making room for the new element.
Syntax
Following is the syntax for the Insert() method −
arrayList.Insert(index, value);
Parameters
- index ? The zero-based index at which the new element should be inserted.
- value ? The object to insert into the ArrayList.
Using Insert() with String Elements
Example
using System;
using System.Collections;
public class Demo {
public static void Main() {
ArrayList list = new ArrayList();
list.Add("One");
list.Add("Two");
list.Add("Three");
list.Add("Four");
list.Add("Five");
list.Add("Six");
list.Add("Seven");
list.Add("Eight");
Console.WriteLine("ArrayList elements...");
foreach(string str in list) {
Console.WriteLine(str);
}
Console.WriteLine("ArrayList is read-only? = " + list.IsReadOnly);
Console.WriteLine("Does the element Six in the ArrayList? = " + list.Contains("Six"));
list.Insert(4, "Twelve");
Console.WriteLine("ArrayList elements...UPDATED");
foreach(string str in list) {
Console.WriteLine(str);
}
}
}
The output of the above code is −
ArrayList elements... One Two Three Four Five Six Seven Eight ArrayList is read-only? = False Does the element Six in the ArrayList? = True ArrayList elements...UPDATED One Two Three Four Twelve Five Six Seven Eight
Using Insert() with Integer Elements
Example
using System;
using System.Collections;
public class Demo {
public static void Main() {
ArrayList arrList = new ArrayList();
arrList.Add(100);
arrList.Add(200);
arrList.Add(300);
arrList.Add(400);
arrList.Add(500);
Console.WriteLine("Display elements...");
IEnumerator demoEnum = arrList.GetEnumerator();
while (demoEnum.MoveNext()) {
Object ob = demoEnum.Current;
Console.WriteLine(ob);
}
arrList.Insert(4, 1000);
Console.WriteLine("Display elements...UPDATED");
demoEnum = arrList.GetEnumerator();
while (demoEnum.MoveNext()) {
Object ob = demoEnum.Current;
Console.WriteLine(ob);
}
}
}
The output of the above code is −
Display elements... 100 200 300 400 500 Display elements...UPDATED 100 200 300 400 1000 500
How It Works
When you call Insert(index, value), the ArrayList automatically shifts all elements at the specified index and beyond one position to the right. This operation increases the Count property of the ArrayList by 1. If the index is equal to Count, the element is added to the end of the ArrayList, similar to using the Add() method.
Conclusion
The ArrayList.Insert() method provides a convenient way to add elements at specific positions within an ArrayList. It automatically handles the shifting of existing elements and maintains the ArrayList's capacity, making it useful for dynamic list manipulation in legacy C# code.
