C# Collection List

How to Insert Elements at a Position in a C# List?

The Insert method of List class inserts an object at a given position. The first parameter of the method is the 0th based index in the List.

The InsertRange method can insert a collection at the given position. 

Add an item to a List

This shows how to add an item to a List using the List.Add() method. The .Add method adds an item to the end of the List by default.

There’s another method you can use if you want to add items to a C# List – AddRange(). It allows you to add one List to another List.

Add an item to the start of a C# List – Insert

If you want to add an item to the start of the List you would use the List.Insert() method which inserts an item to the given List at the position provided. So to insert an item to the start of the List you would specify the position as 0, which is the first position in the list.

But to insert an item to the 3rd row of the List you would specify the position as 2.

And similarly to the AddRange() method I talked about above, there is an InsertRange() method which allows you to insert a List into another List.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.