Home » C#.Net

Inserting an element at specified index in a Collection<T> in C#

Here, we are going to learn how to insert an element at specified index in a Collection<T> in C#?
Submitted by IncludeHelp, on September 23, 2019

Given a Collection<T> of Integer and we have to insert an element at given index.

To insert an element in Collection<T>, we use Insert() method, it accepts two parameters index and item, where index is an integer type and item is of T type i.e. the type of the Collection<T>.

Syntax:

    public void Insert (int index, T item);

Note: Insert() method may return exception (ArgumentOutOfRangeException), if index is either less than 0 or greater than the count.

C# code to insert an element in the Collection<T>

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;

class IncludeHelp
{
    public static void Main()
    {
        // declaring a collection of integers
        Collection<int> iColl = new Collection<int>();

        // adding elements to the collection
        iColl.Add(100);
        iColl.Add(200);
        iColl.Add(300);
        iColl.Add(400);

        // displaying total number of elements
        Console.WriteLine("Total number of elements: " + iColl.Count);

        // displaying all elements 
        foreach (int ele in iColl)
        {
            Console.WriteLine(ele);
        }

        // now inserting 900 at 3rd index
        iColl.Insert(3, 900);

        // displaying total number of elements
        Console.WriteLine("Total number of elements: " + iColl.Count);

        // displaying all elements 
        foreach (int ele in iColl)
        {
            Console.WriteLine(ele);
        }
    } 
}

Output

Total number of elements: 4
100
200
300
400
Total number of elements: 5
100
200
300
900
400

Displaying exception

Here, we will insert an element at -1 index that will generate "ArgumentOutOfRangeException" exception.

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;

class IncludeHelp
{
    public static void Main()
    {
        // declaring a collection of integers
        Collection<int> iColl = new Collection<int>();

        // adding elements to the collection
        iColl.Add(100);
        iColl.Add(200);
        iColl.Add(300);
        iColl.Add(400);

        // displaying total number of elements
        Console.WriteLine("Total number of elements: " + iColl.Count);

        // displaying all elements 
        foreach (int ele in iColl)
        {
            Console.WriteLine(ele);
        }

        // now inserting 900 at -1 index
        // that will generate exception
        iColl.Insert(-1, 900);

        // displaying total number of elements
        Console.WriteLine("Total number of elements: " + iColl.Count);

        // displaying all elements 
        foreach (int ele in iColl)
        {
            Console.WriteLine(ele);
        }
    } 
}

Output

Total number of elements: 4
100
200
300
400

Unhandled Exception:
System.ArgumentOutOfRangeException: Index must be within the bounds of the List.
Parameter name: index
  at System.ThrowHelper.ThrowArgumentOutOfRangeException 
  (System.ExceptionArgument argument, System.ExceptionResource resource) [0x00029] 
  in <65984520577646ec9044386ec4a7b3dd>:0 
  at System.Collections.ObjectModel.Collection`1[T].Insert (System.Int32 index, T item) 
  [0x00026] in <65984520577646ec9044386ec4a7b3dd>:0 
  at IncludeHelp.Main () [0x0007f] in <d658237a48934373b441d64c47cad823>:0
[ERROR] FATAL UNHANDLED EXCEPTION: System.ArgumentOutOfRangeException: 
    Index must be within the bounds of the List.
Parameter name: index
  at System.ThrowHelper.ThrowArgumentOutOfRangeException 
  (System.ExceptionArgument argument, System.ExceptionResource resource) [0x00029] 
  in <65984520577646ec9044386ec4a7b3dd>:0
  at System.Collections.ObjectModel.Collection`1[T].Insert (System.Int32 index, T item)
   [0x00026] 
  in <65984520577646ec9044386ec4a7b3dd>:0
  at IncludeHelp.Main () [0x0007f] in <d658237a48934373b441d64c47cad823>:0


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.