C# ArrayList - FixedSize() Method



The C# ArrayList FixedSize() method is used to create a wrapper around an existing ArrayList, Fixing its size. That means we can modify (update) the ArrayList but cannot add or remove elements from it.

Syntax

Following is the syntax of the C# ArrayList FixedSize() method −

FixedSize(System.Collections.ArrayList list);

Parameters

This method accepts an ArrayList that needs to be wrapped with a fixed size.

Return value

This method returns an ArrayList wrapper with a fixed size.

Example 1: Wrap an ArrayList to a Fixed Size

Following is the basic example of the FixedSize() method to wrap an ArrayList to a fixed size −

using System;
using System.Collections;
class Program
{
   static void Main()
   {
      ArrayList list = new ArrayList() { 1, 2, 3, 4, 5 };

      // Creating a fixed-size wrapper
      ArrayList fixedList = ArrayList.FixedSize(list);

      Console.WriteLine("Original List:");
      foreach (var item in fixedList)
      {
         Console.Write(item + " ");
      }
      Console.WriteLine();

      // Modifying an existing element
      fixedList[2] = 10; 
      Console.WriteLine("After modifying an element:");
      foreach (var item in fixedList)
      {
         Console.Write(item + " ");
      }
      Console.WriteLine();
   }
}

Output

Following is the output −

Original List:
1 2 3 4 5 
After modifying an element:
1 2 10 4 5 

Example 2: Adding an Element to a Fixed-Size ArrayList

The following example tries to add an element in a fixed-size arraylist. Therefore, we get an exception "collection was a fixed size" because we cannot add the element in fixed size ArrayList −

using System;
using System.Collections;
class Program
{
   static void Main()
   {
      ArrayList list = new ArrayList() { 1, 2, 3, 4, 5 };

      // Creating a fixed-size wrapper
      ArrayList fixedList = ArrayList.FixedSize(list);

      Console.WriteLine("Original List:");
      foreach (var item in fixedList)
      {
         Console.Write(item + " ");
      }
      Console.WriteLine();

      // Attempting to add an element
      try
      {
         fixedList.Add(6);
      }
      catch (NotSupportedException e)
      {
         Console.WriteLine("Exception: " + e.Message);
      }
   }
}

Output

Following is the output −

Original List:
1 2 3 4 5 
Exception: Collection was of a fixed size.

Example 3: Removing an Element to a Fixed-Size ArrayList

The following example tries to remove an element in a fixed-size arraylist. Therefore, we get an exception "collection was a fixed size" because we cannot remove an element in fixed size ArrayList −

using System;
using System.Collections;

class Program
{
   static void Main()
   {
      ArrayList list = new ArrayList() { 1, 2, 3, 4, 5 };

      // Creating a fixed-size wrapper
      ArrayList fixedList = ArrayList.FixedSize(list);

      Console.WriteLine("Original List:");
      foreach (var item in fixedList)
      {
         Console.Write(item + " ");
      }
      Console.WriteLine();
      
      // attempting to remove an element
      try
      {
         fixedList.RemoveAt(2);
      }
      catch (NotSupportedException e)
      {
         Console.WriteLine("Exception: " + e.Message);
      }
   }
}

Output

Following is the output −

Original List:
1 2 3 4 5 
Exception: Collection was of a fixed size.
Advertisements