Inserting an element at the specified index in a Collection in C#

Here, we will figure out how to embed a component at a determined record in a Collection in C#?

Given a Collection of Integer and we need to embed a component at given file.

To embed a component in Collection, we use Insert() technique, it acknowledges two parameters list and thing, where the record is an integer type and the thing is of T type, for example, the kind of the Collection.

Syntax:

    public void Insert (int index, T item);

Note: Insert() technique may return special case (ArgumentOutOfRangeException) if the record is either under 0 or more prominent than the tally.

C# code to embed a component in the Collection

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

class JustTechReview
{
    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

Showing an Exception

Here, we will embed a component at – 1 record that will create “ArgumentOutOfRangeException” exemption.

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

class JustTechReview
{
    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 JustTechReview.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 JustTechReview.Main () [0x0007f] in <d658237a48934373b441d64c47cad823>:0

Leave a Comment

error: Alert: Content is protected!!