Check if an array is read-only or not in C#

In C#, arrays are typically mutable by default, meaning their elements can be modified after creation. However, you can check if an array is read-only using the IsReadOnly property from the ICollection interface.

The IsReadOnly property returns true if the array cannot be modified, and false if elements can be changed. Regular arrays in C# always return false for this property.

Syntax

Following is the syntax to check if an array is read-only −

bool isReadOnly = arrayName.IsReadOnly;

Using IsReadOnly Property

Example

The following example demonstrates how to check if an array is read-only −

using System;

public class Demo {
    public static void Main() {
        string[] products = new string[] { "Laptop", "Mobile", "Tablet" };
        
        Console.WriteLine("Products array:");
        foreach(string product in products) {
            Console.WriteLine("- " + product);
        }
        
        Console.WriteLine("\nArray Properties:");
        Console.WriteLine("Is the array having fixed size? = " + products.IsFixedSize);
        Console.WriteLine("Is the array read only? = " + products.IsReadOnly);
        
        // Demonstrating that we can modify the array
        products[0] = "Desktop";
        Console.WriteLine("After modification, first element: " + products[0]);
    }
}

The output of the above code is −

Products array:
- Laptop
- Mobile
- Tablet

Array Properties:
Is the array having fixed size? = True
Is the array read only? = False
After modification, first element: Desktop

Checking Empty Array

Example

Even empty arrays are not read-only by default −

using System;

public class Demo {
    public static void Main() {
        string[] emptyArray = new string[] { };
        int[] numbers = { 10, 20, 30, 40, 50 };
        
        Console.WriteLine("Empty array properties:");
        Console.WriteLine("Length: " + emptyArray.Length);
        Console.WriteLine("Is fixed size? = " + emptyArray.IsFixedSize);
        Console.WriteLine("Is read only? = " + emptyArray.IsReadOnly);
        
        Console.WriteLine("\nNumbers array properties:");
        Console.WriteLine("Length: " + numbers.Length);
        Console.WriteLine("Is fixed size? = " + numbers.IsFixedSize);
        Console.WriteLine("Is read only? = " + numbers.IsReadOnly);
        
        // Modifying numbers array
        numbers[2] = 99;
        Console.WriteLine("Modified element at index 2: " + numbers[2]);
    }
}

The output of the above code is −

Empty array properties:
Length: 0
Is fixed size? = True
Is read only? = False

Numbers array properties:
Length: 5
Is fixed size? = True
Is read only? = False
Modified element at index 2: 99

Array Properties Comparison

Property Regular Array Value Description
IsReadOnly False Elements can be modified after creation
IsFixedSize True Array size cannot be changed after creation
Length Number of elements Total capacity of the array

Understanding Read-Only vs Fixed Size

It's important to distinguish between read-only and fixed size:

  • IsReadOnly = False: You can modify existing elements

  • IsFixedSize = True: You cannot add or remove elements

Example

using System;

public class Demo {
    public static void Main() {
        char[] letters = { 'A', 'B', 'C', 'D' };
        
        Console.WriteLine("Original array:");
        Console.WriteLine(string.Join(", ", letters));
        
        Console.WriteLine("\nArray characteristics:");
        Console.WriteLine("Can modify elements (IsReadOnly): " + !letters.IsReadOnly);
        Console.WriteLine("Cannot resize (IsFixedSize): " + letters.IsFixedSize);
        
        // This works - modifying existing element
        letters[1] = 'X';
        Console.WriteLine("\nAfter modifying index 1:");
        Console.WriteLine(string.Join(", ", letters));
    }
}

The output of the above code is −

Original array:
A, B, C, D

Array characteristics:
Can modify elements (IsReadOnly): True
Cannot resize (IsFixedSize): True

After modifying index 1:
A, X, C, D

Conclusion

In C#, regular arrays are never read-only (IsReadOnly always returns false), meaning you can modify their elements. However, arrays are fixed-size (IsFixedSize returns true), so you cannot add or remove elements after creation. Use the IsReadOnly property to programmatically check mutability in generic collection scenarios.

Updated on: 2026-03-17T07:04:36+05:30

293 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements