
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
Insert an element into the ArrayList at the specified index in C#
To insert an element into the ArrayList at the specified index, the code is as follows −
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); } } }
Output
This will produce the following output −
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
Example
Let us see another 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); } } }
Output
This will produce the following output −
Display elements... 100 200 300 400 500 Display elements...UPDATED 100 200 300 400 1000 500
- Related Articles
- Insert an element into Collection at specified index in C#
- Golang Program to insert an element into the array at the specified index
- Remove the element at the specified index of the ArrayList in C#
- Get or set the element at the specified index in ArrayList in C#
- Add an element to specified index of ArrayList in Java
- Insert at the specified index in StringCollection in C#
- Insert the specified element at the specified position in Java CopyOnWriteArrayList
- Insert into OrderedDictionary with key and value at specified index in C#
- How to insert the elements of a collection into the List at the specified index in C#?
- Replace an element at a specified index of the Vector in Java
- Insert all elements of other Collection to Specified Index of Java ArrayList
- Swift Program to insert multiple elements into the array from the specified index
- Golang Program to insert multiple elements into the array from the specified index
- Copying the entire ArrayList to 1-D Array starting at the specified index in C#
- Gets or sets the element at the specified index in StringCollection in C#

Advertisements