Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




C# - INSERT IN LIST


How to insert a new Item at the end of the List?


Let us say, we have a List that contains three names, Mohan, Kriti and Salim. And we want to insert a new name Mika at the end of the List.


We can use the Add() Method without any parameter to achieve the above.


Example :



using System.Collections.Generic;
    
public class MyApplication
{
    public static void Main(string[] args)
    {
        var x = new List<string>(){"Mohan", "Kriti", "Salim"};
        x.Add("Mika");
        foreach (var data in x)  
        {  
            System.Console.WriteLine(data);  
        }
    }    
}


Output :



  Mohan
  Kriti
  Salim
  Mika

So, in the above code we have created a List and initialised to the variable x.


var x = new List<string>(){"Mohan", "Kriti", "Salim"};

Below is how the values are positioned in the List,

C_Sharp

Next, we have used the Add() method to add the new name Mika at the end of the List.


x.Add("Mika");
C_Sharp


And we get the below output,

Output :



  Mohan
  Kriti
  Salim
  Mika