C# Array AsReadOnly() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Array

Definition

In C#, Array.AsReadOnly<T>(array) returns a ReadOnlyCollection<T> that wraps your array. Callers can read and enumerate, but cannot add, remove, or set items through that collection.

It does not copy the elements. If you still hold the original T[], changes to the array are visible through the wrapper. For a true snapshot, copy first, then wrap.

This is a static member of System.Array. You call it with the class name, such as Array.AsReadOnly(...). Arrays in C# have a fixed length after creation; use List<T> when the count must grow.

Syntax

Pass the array to the static method and keep the read-only collection.

C#
var view = Array.AsReadOnly(nums);
Console.WriteLine(view[0]);
CallWhat it does
Array.AsReadOnly(nums)Returns a read-only view over nums.
view.CountSame as the array Length.

Needs using System.Collections.ObjectModel; only if you name the type explicitly; var is enough in many samples.

Library declarations
C#
public static ReadOnlyCollection<T> AsReadOnly<T>(T[] array);

Parameters

ParameterDescription
arrayRequired. The source T[] to wrap. Do not pass null.

Return Value

Return ValueDescription
ReadOnlyCollection<T>A wrapper that exposes read access. Mutations through the collection throw.

C# Examples

Example 1 — Wrap and Read

Create a read-only view and print items by index.

C#
using System;
using System.Collections.ObjectModel;

class Program {
    static void Main() {
        string[] names = { "Ann", "Bob", "Cara" };
        ReadOnlyCollection<string> view = Array.AsReadOnly(names);

        Console.WriteLine("Count: " + view.Count);
        Console.WriteLine("view[1] = " + view[1]);
        foreach (string n in view) {
            Console.Write(n + " ");
        }
        Console.WriteLine();
    }
}

How It Works

  1. Wrap. AsReadOnly returns a collection facade over the same storage.
  2. Count mirrors Length. Three names means Count is 3.
  3. Index read works. view[1] returns Bob.
  4. foreach works. Enumeration is allowed; mutating APIs are not.

Example 2 — Writes Through the View Throw

Trying to assign via the read-only collection fails at runtime.

C#
using System;
using System.Collections.ObjectModel;

class Program {
    static void Main() {
        int[] nums = { 1, 2, 3 };
        ReadOnlyCollection<int> view = Array.AsReadOnly(nums);

        try {
            // IList indexer set is not supported
            ((System.Collections.Generic.IList<int>)view)[0] = 9;
        } catch (NotSupportedException) {
            Console.WriteLine("Write blocked");
        }

        Console.WriteLine("nums[0] still " + nums[0]);
    }
}

How It Works

  1. Read-only contract. The collection rejects set operations.
  2. Exception path. NotSupportedException signals “cannot edit.”
  3. Underlying intact. Because the set failed, nums[0] stays 1.
  4. Teach callers. Hand them view when they should only read.

Example 3 — Underlying Array Still Mutable

Changing the original array updates what the wrapper shows.

C#
using System;
using System.Collections.ObjectModel;

class Program {
    static void Main() {
        int[] nums = { 10, 20 };
        ReadOnlyCollection<int> view = Array.AsReadOnly(nums);

        nums[0] = 99;
        Console.WriteLine("view[0] = " + view[0]);
    }
}

How It Works

  1. No copy. The wrapper points at the same slots as nums.
  2. Array write wins. Assigning nums[0] changes the shared storage.
  3. View reflects it. Reading view[0] shows 99.
  4. Snapshot tip. Clone or Copy first if you need a frozen picture.

Frequently Asked Questions

It wraps a T[] in a ReadOnlyCollection<T> so callers can read but not edit through that collection.
No. It wraps the same storage. Changes to the original array are visible on the wrapper.
Yes, if you keep the T[] reference. Only the read-only wrapper blocks edits.
When you expose data to other code and want a clear “read-only collection” API without copying.
Mutating through the read-only collection throws NotSupportedException.

Next: BinarySearch()

Fast search for a value in a sorted array; returns the index or a negative bitwise complement with System.Array.

BinarySearch() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

7 people found this page helpful