C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
This is not possible with basic array types. But with the ReadOnlyCollection and the AsReadOnly method on the Array class, we add another layer of indirection to eliminate unwanted writes.
Example. Let's start by looking at this program. The System.Collections.ObjectModel namespace is included. This is because the ReadOnlyCollection is found there. We call Array.AsReadOnly and pass the int array as the argument.
Finally: We display the Count. If you try to change an element in the ReadOnlyCollection, your program won't compile.
C# program that uses Array.AsReadOnly using System; using System.Collections.ObjectModel; class Program { static void Main() { int[] array = { 1, 5, 3 }; ReadOnlyCollection<int> result = Array.AsReadOnly(array); Console.WriteLine("Count: " + result.Count); for (int i = 0; i < result.Count; i++) { // Can't touch this. Console.WriteLine(result[i]); } } } Output Count: 3 1 5 3
Internals. What does Array.AsReadOnly do internally? Does it copy the array? In my investigation, I found that AsReadOnly simply called into the ReadOnlyCollection constructor. In that constructor, the array is received as an IList instance.
Then: A reference to the original array is stored as a field in ReadOnlyCollection. Little extra memory is used when this method is called.
Summary. We demonstrated Array.AsReadOnly and also ventured into the System.Collections.ObjectModel namespace to see the ReadOnlyCollection type. Arrays cannot be made immutable in the C# language. Elements can always be assigned.
Thus: Array.AsReadOnly makes it possible to restrict access to arrays by client code.
But: This extra level of indirection has a performance cost. And it introduces a name change: the Length property becomes Count.