How to Insert an Item into a C# List

Insert Items 

The Insert method inserts an item to a List at the specified position. The following code snippet creates a List and inserts an item to the List by using the Insert method. 

// Create a list
List<string> AuthorList = new List<string>();

// Add items using Add method 
AuthorList.Add("Mahesh Chand");
AuthorList.Add("Praveen Kumar");
AuthorList.Add("Raj Kumar");
AuthorList.Add("Nipun Tomar");
AuthorList.Add("Dinesh Beniwal");

// Insert an item at position 2
AuthorList.Insert(1, "Second Author");

The AddRange method is used to add a collection of items. The following code snippet adds a collection of items to a List. 

// Insert a range of items
string[] authors = { "Mike Gold", "Don Box",
                        "Sundar Lal", "Neel Beniwal" };
AuthorList.InsertRange(4, authors);

Download Free book: Programming List with C#


Up Next
    Ebook Download
    View all
    Learn
    View all