Inserting elements in List : Insert() Method



How to insert an element in the list in c#?

We can use the Insert() method for inserting an element into the List<T> collection at the specified index.

Signature:

void Insert(int index, T item);

Example:

var age = new List<int>(){ 22, 45, 12, 36, 57 };

age.Insert(2, 41); // inserts 41 at 2nd index: after 45.

foreach (var selectedage in age)
    Console.Write(selectedage); // prints 22 45 41 12 36 57 

0 Comment's

Comment Form

Submit Comment