
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Arithmetic Operators
- C# - Assignment Operators
- C# - Relational Operators
- C# - Logical Operators
- C# - Bitwise Operators
- C# - Miscellaneous Operators
- C# - Operators Precedence
- C# Conditional Statements
- C# - Decision Making
- C# - If
- C# - If Else
- C# - Nested If
- C# - Switch
- C# - Nested Switch
- C# Control Statements
- C# - Loops
- C# - For Loop
- C# - While Loop
- C# - Do While Loop
- C# - Nested Loops
- C# - Break
- C# - Continue
- C# OOP & Data Handling
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
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.