
- 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 at second position in a C# List
Here is our list −
List<string> val = new List<string> (); // list of strings val.Add("water"); val.Add("food"); val.Add("air");
Use the Insert() method to insert an element in the list. With that, also set where you want to add it. We have set the new text at position 1st −
val.Insert(1, "shelter");
The following is the complete code −
Example
using System; using System.Collections.Generic; public class Demo { public static void Main() { List<string> val = new List<string> (); // list of strings val.Add("water"); val.Add("food"); val.Add("air"); Console.WriteLine("Initial list:"); // Initial List foreach (string res in val) { Console.WriteLine(res); } // inserting an element at second position val.Insert(1, "shelter"); Console.WriteLine("New list after inserting an element:"); foreach (string res in val) { Console.WriteLine(res); } } }
Output
Initial list: water food air New list after inserting an element: water shelter food air
- Related Articles
- How to insert an object in a list at a given position in Python?
- How to insert an item in a list at a given position in C#?
- Insert the specified element at the specified position in Java CopyOnWriteArrayList
- Insert more than one element at once in a C# List
- How to insert an object in an ArrayList at a specific position in java?
- C program to insert a node at any position using double linked list
- Program to insert new element into a linked list before the given position in Python
- Insert a specified element in a specified position in JavaScript?
- Insert an element to List using ListIterator in Java
- Insert a character at nth position in string in JavaScript
- Insert an element into Collection at specified index in C#
- Python program to insert an element into sorted list
- Adding an element at a given position of the array in Javascript
- Java Program to Add Element at First and Last Position of a Linked list
- Python Pandas - Insert a new index value at a specific position

Advertisements