c sharp

How to Insert an Item into a C# List

A “List” in C# represents a group of objects of the same type. “List” comes in the “Generic” collections and uses the “index” to store objects. Thus, we can access the objects of a “List” using these “indexes”. We can add, remove, and insert objects from a list.

This article will explain how to “insert” an item into a C# list.

Insert an Item into a C# List

In C#, we can add and insert items into a “List” using the “Add()” and “Insert()” methods. The difference between these two methods is that the “Add()” method is used to add an item at the end of the list. In contrast, the “Insert()” function in C# is used to add an item at a specified “index” in a list.

The “insert” method’s syntax is as follows:

List<Type>.Insert(Int32, item)

In the above-given syntax:

  • List<Type>” represents the “List”. Here “Type” is the type of the “List”.
  • Int32” is the value of the “index” on which we want to insert the new value.
  • The new item that we wish to add is “Item“.

The capacity of the “List” automatically grows each time we add a new item to it. To understand the working of the “Insert()” method try the following example:

using System;
using System.Collections.Generic;
 
public class List_insert_example
{
    public static void Main()
    {

       List<int> a = new List<int>() { 101, 102, 104, 105 };

       List<string> b = new List<string>() { "Apple", "Mango", "Banana", "Peach" };

       Console.WriteLine("--------------Original Lists-----------------");

       Console.WriteLine("List 1: ");

       Console.WriteLine(string.Join(", ", a));

       Console.WriteLine("List 2: ");

       Console.WriteLine(string.Join(", ", b));

       int c=103;

       a.Insert(2,c);

       string d="Pineapple";

       b.Insert(3,d);

       Console.WriteLine("-----------Lists after insertion------------");
       
       Console.WriteLine("List 1: ");

       Console.WriteLine(string.Join(", ", a));

       Console.WriteLine("List 2: ");

       Console.WriteLine(string.Join(", ", b));

    }

}

In the above-generated code:

  • We have used the “System” namespace and then the “Generic” collections of “System” as “List” is included in the “Generic” collection.
  • After that, we declare a class “List_insert_example”.
  • In this class, there is a static “Main()” method.
  • The two “Lists” “a” and “b” have been declared.
  • List “a” is of type “int” and has “{ 101, 102, 104, 105 }” items.
  • List “b” is of type “string” and has “{ “Apple”, “Mango”, “Banana”, “Peach” }” items. We used different types of “Lists” to show how the “Insert()” method will work for different types of “Lists”.
  • Then, we print the original lists to show the values of these lists.
  • After that, we declared two variables “c” and “d”. Variable “c” is an “int” type and has the value “103” whereas variable “d” is of type “string” and has the value “Pineapple”.
  • We used the “Insert()” method to insert “103” at index “2” and “Pineapple” at index “3” in the given lists.
  • Lastly, we printed the modified lists after inserting the required values.

Here is the output of the previously generated code:

In this output, we can see that:

  • The values of lists “a” and “b” are first printed.
  • Then, we can see that “103” is added at index “2” in “List 1”.
  • Lastly, we can see “Pineapple” at index “3” in “List 2”.

That’s all, we have seen how we can Insert an Item into a C# List with an example.

Conclusion

In C#, “List” is the collection of elements having the same type. Users can add, remove, insert, delete, and manipulate elements in the “List”. “List” uses indexes to store and access elements. To insert an element in a List the “Insert()” method is used which has two parameters, one is the “Index” of the list having type int, and the other one is the “new item” that we want to insert. We have briefly discussed inserting an item into a C# list.

About the author

Umar Hassan

I am a Front-End Web Developer. Being a technical author, I try to learn new things and adapt with them every day. I am passionate to write about evolving software tools and technologies and make it understandable for the end-user.