C# | Array.AsReadOnly(T[]) Method Last Updated : 11 Jul, 2025 Comments Improve Suggest changes Like Article Like Report This method is used to return a read-only wrapper for the specified array. Syntax: public static System.Collections.ObjectModel. ReadOnlyCollection<T> AsReadOnly<T> (T[] array); Here, T is the type of element of the array. Return Value: This method return the a read-only ReadOnlyCollection<T> wrapper . Exception: This method throws ArgumentNullException if the array is null. Below are the examples to illustrate the Array.AsReadOnly(T[]) Method: Example 1: CSharp // C# program to demonstrate // AsReadOnly() method using System; using System.Collections.Generic; public class GFG { // Main Method public static void Main() { // Creating and initializing new the String String[] myArr = {"Sun", "Mon", "Tue", "Thu"}; // Display the values of the myArr. Console.WriteLine("Initial Array:"); // calling the PrintIndexAndValues() // method to print PrintIndexAndValues(myArr); // Create a read-only IList // wrapper around the array. IList<String> myList = Array.AsReadOnly(myArr); // Display the values of the read-only myList. Console.WriteLine("Read-only Array: "); // calling the PrintIndexAndValues() // method to print PrintIndexAndValues(myList); } // Defining the method // PrintIndexAndValues public static void PrintIndexAndValues(String[] myArr) { for (int i = 0; i < myArr.Length; i++) { Console.WriteLine("{0}", myArr[i]); } Console.WriteLine(); } // Defining the method // PrintIndexAndValues public static void PrintIndexAndValues(IList<String> myList) { for (int i = 0; i < myList.Count; i++) { Console.WriteLine("{0}", myList[i]); } } } Output: Initial Array: Sun Mon Tue Thu Read-only Array: Sun Mon Tue Thu Example 2: CSharp // C# program to demonstrate // AsReadOnly() method // For ArgumentNullException using System; using System.Collections.Generic; public class GFG { // Main Method public static void Main() { try { // Creating and initializing new // the String with a null value String[] myArr = null; // Create a read-only IList // wrapper around the array. IList<String> myList = Array.AsReadOnly(myArr); // Display the values of // the read-only myList. Console.WriteLine("Read-only Array:"); // calling the PrintIndexAndValues() // method to print PrintIndexAndValues(myList); } catch (ArgumentNullException e) { Console.Write("Exception Thrown: "); Console.Write("{0}", e.GetType(), e.Message); } } // Defining the method PrintIndexAndValues public static void PrintIndexAndValues(String[] myArr) { for (int i = 0; i < myArr.Length; i++) { Console.WriteLine("{0}", myArr[i]); } Console.WriteLine(); } // Defining the method PrintIndexAndValues public static void PrintIndexAndValues(IList<String> myList) { for (int i = 0; i < myList.Count; i++) { Console.WriteLine("{0}", myList[i]); } } } Output: Exception Thrown: System.ArgumentNullException Reference: https://learn.microsoft.com/en-us/dotnet/api/system.array.asreadonly?view=netframework-4.7.2 Comment More infoAdvertise with us R rohitprasad3 Follow Improve Article Tags : C# CSharp-method CSharp-Arrays Explore IntroductionC# Tutorial 4 min read Introduction to .NET Framework 6 min read C# .NET Framework (Basic Architecture and Component Stack) 6 min read C# Hello World 2 min read Common Language Runtime (CLR) in C# 4 min read FundamentalsC# Identifiers 2 min read Data Types in C# 6 min read C# Variables 4 min read C# Literals 5 min read Operators in C# 7 min read C# Keywords 5 min read Control StatementsC# Decision Making (if, if-else, if-else-if ladder, nested if, switch, nested switch) 5 min read C# Switch Statement 4 min read Loops in C# 4 min read C# Jump Statements (Break, Continue, Goto, Return and Throw) 4 min read OOP ConceptsC# Class and Objects 5 min read C# Constructors 5 min read C# Inheritance 6 min read C# Encapsulation 4 min read C# Abstraction 4 min read MethodsMethods in C# 4 min read Method Overloading in C# 4 min read C# | Method Parameters 7 min read C# Method Overriding 9 min read Anonymous Method in C# 3 min read ArraysArrays in C# 6 min read Jagged Arrays in C# 4 min read Array Class in C# 5 min read How to Sort an Array in C# | Array.Sort() Method Set - 1 8 min read How to find the rank of an array in C# 2 min read ArrayListArrayList in C# 6 min read C# ArrayList Class 7 min read C# | Array vs ArrayList 2 min read StringStrings in C# 6 min read C# Verbatim String Literal - @ 5 min read C# String Class 9 min read C# StringBuilder 4 min read C# String vs StringBuilder 3 min read TupleC# Tuple 7 min read C# Tuple Class 3 min read C# ValueTuple 7 min read C# ValueTuple Struct 4 min read IndexersC# Indexers 4 min read C# Multidimensional Indexers 5 min read C# - Overloading of Indexers 3 min read Like