C# Array AsReadOnly
Description
Array AsReadOnly
returns 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.