
- 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 a list at a given position in C#?
To insert an item in an already created List, use the Insert() method.
Firstly, set elements −
List <int> list = new List<int>(); list.Add(989); list.Add(345); list.Add(654); list.Add(876); list.Add(234); list.Add(909);
Now, let’s say you need to insert an item at 4th position. For that, use the Insert() method −
// inserting element at 4th position list.Insert(3, 567);
Let us see the complete example −
Example
using System; using System.Collections.Generic; namespace Demo { public class Program { public static void Main(string[] args) { List < int > list = new List < int > (); list.Add(989); list.Add(345); list.Add(654); list.Add(876); list.Add(234); list.Add(909); Console.WriteLine("Count: {0}", list.Count); Console.Write("List: "); foreach(int i in list) { Console.Write(i + " "); } // inserting element at 4th position list.Insert(3, 567); Console.Write("
List after inserting a new element: "); foreach(int i in list) { Console.Write(i + " "); } Console.WriteLine("
Count: {0}", list.Count); } } }
- Related Articles
- How to insert an object in a list at a given position in Python?
- Insert an element at second position in a C# List
- How to insert an object in an ArrayList at a specific position in java?
- 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?
- How to insert an image in a Tkinter canvas item?
- Delete a Linked List node at a given position in C++
- Program to insert new element into a linked list before the given position in Python
- C program to insert a node at any position using double linked list
- Delete a Doubly Linked List node at a given position in C++
- How to insert an item in ArrayList in C#?
- How to add an item to a list in Kotlin?
- How to find the index of an item given a list containing it in Python?
- How to search for an item in a Lua List?
- Insert a character at nth position in string in JavaScript

Advertisements