
- 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
How to insert an item in ArrayList in C#?
To insert an item in an already created ArrayList, use the Insert() method.
Firstly, set elements −
ArrayList arr = new ArrayList(); arr.Add(45); arr.Add(78); arr.Add(33);
Now, let’s say you need to insert an item at 2nd position. For that, use the Insert() method −
// inserting element at 2nd position arr.Insert(1, 90);
Let us see the complete example −
Example
using System; using System.Collections; namespace Demo { public class Program { public static void Main(string[] args) { ArrayList arr = new ArrayList(); arr.Add(45); arr.Add(78); arr.Add(33); Console.WriteLine("Count: {0}", arr.Count); Console.Write("ArrayList: "); foreach(int i in arr) { Console.Write(i + " "); } // inserting element at 2nd position arr.Insert(1, 90); Console.Write("
ArrayList after inserting a new element: "); foreach(int i in arr) { Console.Write(i + " "); } Console.WriteLine("
Count: {0}", arr.Count); } } }
Output
Count: 3 ArrayList: 45 78 33 ArrayList after inserting a new element: 45 90 78 33 Count: 4
- Related Articles
- How to add an item to an ArrayList in C#?
- How to add an item to an ArrayList in Kotlin?
- How to remove an item from an ArrayList in C#?
- How to remove an item from an ArrayList in Kotlin?
- How to check if ArrayList contains an item in Java?
- How to insert an image in a Tkinter canvas item?
- How to insert an object in an ArrayList at a specific position in java?
- How to insert an item to an array that is inside an object in MongoDB?
- How to insert an item in a list at a given position in C#?
- How to insert element to arraylist for listview in Android?
- How to insert an item into a C# list by using an index?
- How do I insert an item between two items in a list in Java?
- Insert an element into the ArrayList at the specified index in C#
- How to reverse an ArrayList in Java?
- How to synchronize an ArrayList in Java?

Advertisements