
- 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# 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