C# Array - AsReadOnly() Method



The C# Array AsReadOnly() Method is used to return a read-only wrapper for the specified array. This method prevents modification of the array elements through the wrapper, while still allowing us to view the elements.

Syntax

Following is the syntax of the C# Array AsReadOnly() method −

public static System.Collections.ObjectModel.
Array.AsReadOnly<T> (T[] array);

Parameters

This function accepts a one-D array (T[] array) as a parameter to wrap in read-only (ReadOnlyCollection<T>) wrapper.

Return value

This function returns a read-only wrapper for the specified array.

Example 1

Let us crate a basic example of the AsReadOnly() method to display read-only collection −

using System;
using System.Collections.ObjectModel;

class Program {
   static void Main() {
      // Original array
      int[] numbers = { 1, 2, 3, 4, 5 };
   
      // Wrap the array as a read-only collection
      ReadOnlyCollection<int> readOnlyNumbers = Array.AsReadOnly(numbers);
   
      // Display the read-only collection
      Console.WriteLine("Read-only collection elements:");
      foreach (int num in readOnlyNumbers) {
         Console.WriteLine(num);
      }
	  
      // Original array can still be modified
      numbers[0] = 10;
   
      Console.WriteLine("\nAfter modifying the original array:");
      foreach (int num in readOnlyNumbers) {
         Console.WriteLine(num);
      }
   }
}

Output

Following is the output −

Read-only collection elements:
1
2
3
4
5

After modifying the original array:
10
2
3
4
5

Example 2: Read-only Collection of Custom Object

The following example uses the AsReadOnly() method to convert the custom object into read-only collection −

using System;
using System.Collections.ObjectModel;

class Person {
   public string Name { get; set; }
   public int Age { get; set; }

   public override string ToString() {
      return $"Name: {Name}, Age: {Age}";
   }
}

class Program {
   static void Main() {
      // Array of custom objects (Person)
      Person[] people = new Person[] {
         new Person { Name = "Aman", Age = 25 },
         new Person { Name = "Akash", Age = 30 },
         new Person { Name = "Vikash", Age = 35 }
      };

      // Wrap the array as a read-only collection
      ReadOnlyCollection<Person> readOnlyPeople = Array.AsReadOnly(people);

      Console.WriteLine("Read-only collection of people:");
      foreach (var person in readOnlyPeople) {
         Console.WriteLine(person);
      }
   }
}

Output

Following is the output −

Read-only collection of people:
Name: Aman, Age: 25
Name: Akash, Age: 30
Name: Vikash, Age: 35

Example 3: If You Modified Read-Only Collection

This is another example of theAsReadOnly()method. If you want to modify the Read-Only collection, you will get a compile-time error −

using System;
using System.Collections.ObjectModel;

class Person {
   public string Name { get; set; }
   public int Age { get; set; }

   public override string ToString() {
      return $"Name: {Name}, Age: {Age}";
   }
}
class Program {
   static void Main() {
      // Array of custom objects (Person)
      Person[] people = new Person[] {
          new Person { Name = "Aman", Age = 25 },
          new Person { Name = "Akash", Age = 30 },
          new Person { Name = "Vikash", Age = 35 }
      };

      // Wrap the array as a read-only collection
      ReadOnlyCollection<Person> readOnlyPeople = Array.AsReadOnly(people);
      
      // Attempting to modify the read-only collection throws an exception
      readOnlyPeople[0] = new Person { Name = "David", Age = 40 };
   }
}

Output

Following is the output −

ERROR!
Main.cs(31,9): error CS0200: Property or indexer 'ReadOnlyCollection<Person>.this[int]' cannot be assigned to -- it is read only
csharp_array_class.htm
Advertisements