當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


C# Array.AsReadOnly(T[])用法及代碼示例


此方法用於返回指定數組的隻讀包裝器。

用法:

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

在此,T是數組元素的類型。



返回值:此方法返回一個隻讀ReadOnlyCollection <T>包裝器。

異常:如果數組為null,則此方法引發ArgumentNullException。

下麵是說明Array.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

參考:




相關用法


注:本文由純淨天空篩選整理自RohitPrasad3大神的英文原創作品 C# | Array.AsReadOnly(T[]) Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。