C# |数组。AsReadOnly(T[])方法

原文:https://www . geesforgeks . org/c-sharp-array-as readonly t-method/

此方法用于返回指定数组的只读包装。

语法:

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

这里,T 是数组元素的类型。

返回值:该方法返回只读的readonly collection包装器。

异常:如果数组为空,这个方法抛出 ArgumentNullException

下面是举例说明数组。AsReadOnly(T[])方法:

例 1:

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

输出:

Initial Array:
Sun
Mon
Tue
Thu

Read-only Array: 
Sun
Mon
Tue
Thu

例 2:

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

输出:

Exception Thrown: System.ArgumentNullException

参考: