C# Array AsReadOnly

Description

Array AsReadOnlyreturns a read-only wrapper for the specified array.

Syntax

Array.AsReadOnly<T> has the following syntax.


public static ReadOnlyCollection<T> AsReadOnly<T>(
  T[] array
)

Parameters

Array.AsReadOnly<T> has the following parameters.

  • T - The type of the elements of the array.
  • array - The one-dimensional, zero-based array to wrap in a read-only ReadOnlyCollection<t> wrapper.

Returns

Array.AsReadOnly<T> method returns <

Example

The following example wraps an array in a read-only ReadOnlyCollection<T>.


/* w w w  .j a v  a 2 s. c om*/
using System;
using System.Collections.Generic;

public class MainClass {

   public static void Main()  {
      String[] myArr = { "The", "quick", "brown", "fox" };

      IList<String> myList = Array.AsReadOnly( myArr );

      try  {
         myList[3] = "CAT";
      }
      catch ( NotSupportedException e )  {
         Console.WriteLine( "{0} - {1}", e.GetType(), e.Message );
         Console.WriteLine();
      }
   }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System »




Array
BitConverter
Boolean
Byte
Char
Console
ConsoleKeyInfo
Convert
DateTime
DateTimeOffset
Decimal
Double
Enum
Environment
Exception
Guid
Int16
Int32
Int64
Math
OperatingSystem
Random
SByte
Single
String
StringComparer
TimeSpan
TimeZone
TimeZoneInfo
Tuple
Tuple
Tuple
Type
UInt16
UInt32
UInt64
Uri
Version