Insert in Generic List of C#

Hi Friends,

In the below post we will learn about the method used to Insert elements to the generic list of C#. We have two methods as below.

1. Insert(int index, T item)

This method takes input of the specified type and insert the element to the given index in the list list. The input can be null for the reference types and the List allows duplicate values.This method throws ‘ArgumentOutOfRangeException’ exception if the given index is not a valid position for the List. Below is a sample program to demonstrate how to use this method.

using System;
using System.Collections.Generic;
namespace GenericListSample
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                List marksList = new List();
                int[] marks = { 12, 14, 18, 15 };
                marksList.AddRange(marks);
                marksList.Insert(1, 110);
                marksList.Insert(1, 110);
                foreach (var item in marksList)
                {
                    //display on console
                    Console.WriteLine(item);
                }
            }
            catch (ArgumentNullException ex)
            {
                Console.WriteLine(ex.Message);
            }
            catch(ArgumentOutOfRangeException ex)
            {
                Console.WriteLine(ex.Message);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            Console.ReadLine();
        }
    }
}

In the above code I have created a generic list ‘marksList’,then I have added some element to it using AddRange Method and then try to insert some items using ‘Insert’ method.

2. InsertRange(int index, IEnumerable<T> collection)

This method takes a collection as input to be inserted to the list at the given index. The collection should be of type ‘System.Collections.Generic.IEnumerable<T>’. The input cannot be null but contains null if the specified type is reference type.This method throws the ‘ArgumentOutOfRangeException’ exception if the index is not a valid position for the List. Below is a sample program for demonstrate the use the ‘InsertRange’ method.

using System;
using System.Collections.Generic;
namespace GenericListSample
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                List marksList = new List();
                int[] marks = { 12, 14, 18, 15 };
                marksList.AddRange(marks);
                marksList.Insert(1, 110);
                marksList.Insert(1, 110);
                marksList.InsertRange(1, new int[] { 74,84,57,86});
                foreach (var item in marksList)
                {
                    //display on console
                    Console.WriteLine(item);
                }
            }
            catch (ArgumentNullException ex)
            {
                Console.WriteLine(ex.Message);
            }
            catch(ArgumentOutOfRangeException ex)
            {
                Console.WriteLine(ex.Message);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            Console.ReadLine();
        }
    }
}

In the above sample I have used the ‘InsertRange’ method with index=1 as as first parameter and a new collection of integer values( since ‘marksList’ is of int type) as second parameter.

Hope this helps you.:)

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s