C# | Array.AsReadOnly(T[]) Method

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to demonstrate
// AsReadOnly() method
using System;
using System.Collections.Generic;
  
public class GFG {
  
    // Main Method
    public static void Main()
    {
  
        // Creating and intializing 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]);
        }
    }
}

chevron_right


Output:

Initial Array:
Sun
Mon
Tue
Thu

Read-only Array: 
Sun
Mon
Tue
Thu

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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]);
        }
    }
}

chevron_right


Output:

Exception Thrown: System.ArgumentNullException

Reference:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.