Array.AsReadOnly ジェネリック メソッドとは? わかりやすく解説

Weblio 辞書 > コンピュータ > .NET Framework クラス ライブラリ リファレンス > Array.AsReadOnly ジェネリック メソッドの意味・解説 

Array.AsReadOnly ジェネリック メソッド

メモ : このメソッドは、.NET Framework version 2.0新しく追加されたものです。

指定した配列ラップする読み取り専用ラッパー作成します

名前空間: System
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

Public Shared Function AsReadOnly(Of
 T) ( _
    array As T() _
) As ReadOnlyCollection(Of T)
Dim array As T()
Dim returnValue As ReadOnlyCollection(Of
 T)

returnValue = Array.AsReadOnly(array)
public static ReadOnlyCollection<T> AsReadOnly<T>
 (
    T[] array
)
public:
generic<typename T>
static ReadOnlyCollection<T>^ AsReadOnly (
    array<T>^ array
)
J# では、ジェネリックな型およびメソッド使用できますが、新規に宣言することはできません。
JScript では、ジェネリックな型およびメソッド使用できません。

型パラメータ

T

配列要素の型。

パラメータ

array

読み取り専用の ReadOnlyCollection ラッパーラップする、インデックス番号が 0 から始まる 1 次元配列

戻り値
指定した配列ラップする読み取り専用ReadOnlyCollection ラッパー

例外例外
例外種類条件

ArgumentNullException

arraynull 参照 (Visual Basic では Nothing) です。

解説解説
使用例使用例

次の例では、配列読み取り専用ReadOnlyCollectionラップしています。

Imports System
Imports System.Collections.Generic

Public Class SamplesArray

    Public Shared Sub Main()

        ' Create and initialize a new string array.
        Dim myArr As String()
 =  {"The", "quick",
 "brown", "fox"}

        ' Display the values of the array.
        Console.WriteLine("The string array initially contains
 the following values:")
        PrintIndexAndValues(myArr)

        ' Create a read-only IList wrapper around the array.
        Dim myList As IList(Of
 String) = Array.AsReadOnly(myArr) '

        ' Display the values of the read-only IList.
        Console.WriteLine("The read-only IList contains the following
 values:")
        PrintIndexAndValues(myList)

        ' Attempt to change a value through the wrapper.
        Try
            myList(3) = "CAT"
        Catch e As NotSupportedException
            Console.WriteLine("{0} - {1}", e.GetType(),
 e.Message)
            Console.WriteLine()
        End Try

        ' Change a value in the original array.
        myArr(2) = "RED"

        ' Display the values of the array.
        Console.WriteLine("After changing the third element, the
 string array contains the following values:")
        PrintIndexAndValues(myArr)

        ' Display the values of the read-only IList.
        Console.WriteLine("After changing the third element, the
 read-only IList contains the following values:")
        PrintIndexAndValues(myList)

    End Sub 'Main

    Overloads Public Shared
 Sub PrintIndexAndValues(myArr() As String)
        Dim i As Integer
        For i = 0 To myArr.Length - 1
            Console.WriteLine("   [{0}] : {1}", i,
 myArr(i))
        Next i
        Console.WriteLine()
    End Sub 'PrintIndexAndValues

    Overloads Public Shared
 Sub PrintIndexAndValues(myList As IList(Of
 String))
        Dim i As Integer
        For i = 0 To myList.Count - 1
            Console.WriteLine("   [{0}] : {1}", i,
 myList(i))
        Next i
        Console.WriteLine()
    End Sub 'PrintIndexAndValues
 

End Class 'SamplesArray


'This code produces the following output.
'
'The string array initially contains the following values:
'   [0] : The
'   [1] : quick
'   [2] : brown
'   [3] : fox
'
'The read-only IList contains the following values:
'   [0] : The
'   [1] : quick
'   [2] : brown
'   [3] : fox
'
'System.NotSupportedException - Collection is read-only.
'
'After changing the third element, the string array contains the following
 values:
'   [0] : The
'   [1] : quick
'   [2] : RED
'   [3] : fox
'
'After changing the third element, the read-only IList contains the
 following values:
'   [0] : The
'   [1] : quick
'   [2] : RED
'   [3] : fox

using System;
using System.Collections.Generic;

public class SamplesArray  {
 
   public static void Main()
  {
 
      // Create and initialize a new string array.
      String[] myArr = { "The", "quick", "brown", "fox"
 };
 
      // Display the values of the array.
      Console.WriteLine( "The string array initially contains
 the following values:" );
      PrintIndexAndValues( myArr );

      // Create a read-only IList wrapper around the array.
      IList<String> myList = Array.AsReadOnly( myArr );
      
      // Display the values of the read-only IList.
      Console.WriteLine( "The read-only IList contains the following values:"
 );
      PrintIndexAndValues( myList );

      // Attempt to change a value through the wrapper.
      try  {
         myList[3] = "CAT";
      }
      catch ( NotSupportedException e )  {
         Console.WriteLine( "{0} - {1}", e.GetType(), e.Message );
         Console.WriteLine();
      }

      // Change a value in the original array.
      myArr[2] = "RED";

      // Display the values of the array.
      Console.WriteLine( "After changing the third element, the string
 array contains the following values:" );
      PrintIndexAndValues( myArr );

      // Display the values of the read-only IList.
      Console.WriteLine( "After changing the third element, the read-only IList
 contains the following values:" );
      PrintIndexAndValues( myList );

   }
 
   public static void PrintIndexAndValues(
 String[] myArr )  {
      for ( int i = 0; i < myArr.Length;
 i++ )  {
         Console.WriteLine( "   [{0}] : {1}", i, myArr[i] );
      }
      Console.WriteLine();
   }

   public static void PrintIndexAndValues(
 IList<String> myList )  {
      for ( int i = 0; i < myList.Count;
 i++ )  {
         Console.WriteLine( "   [{0}] : {1}", i, myList[i] );
      }
      Console.WriteLine();
   }

}


/* 
This code produces the following output.

The string array initially contains the following values:
   [0] : The
   [1] : quick
   [2] : brown
   [3] : fox

The read-only IList contains the following values:
   [0] : The
   [1] : quick
   [2] : brown
   [3] : fox

System.NotSupportedException - Collection is read-only.

After changing the third element, the string array contains the
 following values:
   [0] : The
   [1] : quick
   [2] : RED
   [3] : fox

After changing the third element, the read-only IList contains the following values:
   [0] : The
   [1] : quick
   [2] : RED
   [3] : fox

*/

using namespace System;
using namespace System::Collections::Generic;

namespace Samples
{
    public ref class SamplesArray
    {
    public:
        static void Work()
        {

            // Create and initialize a new string array.
            array <String^>^ textArray = 
                {"The", "quick", "brown", "fox"};

            // Display the values of the array.
            Console::WriteLine("The string array initially
 contains "
                "the following values:");
            PrintIndexAndValues(textArray);

            // Create a read-only IList wrapper around the array.
            IList <String^>^ textList = Array::AsReadOnly(textArray);

            // Display the values of the read-only IList.
            Console::WriteLine("The read-only IList contains " 
                "the following values:");
            PrintIndexAndValues(textList);

            // Attempt to change a value through the wrapper.
            try
            {
                textList[3] = "CAT";
            }
            catch (NotSupportedException^ ex) 
            {
                Console::WriteLine("{0} - {1}", ex->GetType(), 
                    ex->Message);
                Console::WriteLine();
            }


            // Change a value in the original array.
            textArray[2] = "RED";

            // Display the values of the array.
            Console::WriteLine("After changing the third element," 
                "the string array contains the following
 values:");
            PrintIndexAndValues(textArray);

            // Display the values of the read-only IList.
            Console::WriteLine("After changing the third element, the"
 
                " read-only IList contains the following values:");
            PrintIndexAndValues(textList);
        }

        static void PrintIndexAndValues(array<String^>^
 textArray)
        {
            for (int i = 0; i < textArray->Length;
 i++)
            {
                Console::WriteLine("   [{0}] : {1}", i, textArray[i]);
            }
            Console::WriteLine();
        }

        static void PrintIndexAndValues(IList<String^>^
 textList)
        {
            for (int i = 0; i < textList->Count;
 i++)
            {
                Console::WriteLine("   [{0}] : {1}", i, textList[i]);
            }
            Console::WriteLine();
        }
    };
}

int main()
{
    Samples::SamplesArray::Work();

}

/* 
This code produces the following output.

The string array initially contains the following values:
[0] : The
[1] : quick
[2] : brown
[3] : fox

The read-only IList contains the following values:
[0] : The
[1] : quick
[2] : brown
[3] : fox

System.NotSupportedException - Collection is read-only.

After changing the third element, the string array contains the
 following values:
[0] : The
[1] : quick
[2] : RED
[3] : fox

After changing the third element, the read-only IList contains the following values:
[0] : The
[1] : quick
[2] : RED
[3] : fox

*/
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照



英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

Array.AsReadOnly ジェネリック メソッドのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



Array.AsReadOnly ジェネリック メソッドのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2025 Microsoft.All rights reserved.

©2025 GRAS Group, Inc.RSS