Array::AsReadOnly<T> Method
Returns a read-only wrapper for the specified array.
Assembly: mscorlib (in mscorlib.dll)
Type Parameters
- T
The type of the elements of the array.
Parameters
- array
- Type: array<T>
The one-dimensional, zero-based array to wrap in a read-only ReadOnlyCollection<T> wrapper.
Return Value
Type: System.Collections.ObjectModel::ReadOnlyCollection<T>A read-only ReadOnlyCollection<T> wrapper for the specified array.
| Exception | Condition |
|---|---|
| ArgumentNullException | array is nullptr. |
To prevent any modifications to the array, expose the array only through this wrapper.
A collection that is read-only is simply a collection with a wrapper that prevents modifying the collection; therefore, if changes are made to the underlying collection, the read-only collection reflects those changes.
This method is an O(1) operation.
The following example wraps an array in a read-only ReadOnlyCollection<T>.
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 */
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.