The method Array.AsReadOnly<T>(T[] array)
which is defined on the Array class itself is supposed to be used for this purpose.
The method takes an array as an argument (the one you want to make read-only) and returns a ReadOnlyCollection<T>
.
An example would be as follows:
// declaration of a normal example array
string[] myArray = new string[] { "StackOverflow", "SuperUser", "MetaStackOverflow" };
// declaration of a new ReadOnlyCollection whose elements are of type string
// the string array is passed through the constructor
// that's is where our array is passed into its new casing
// as a matter of fact, the ReadOnlyCollection is a wrapper for ordinary collection classes such as arrays
ReadOnlyCollection<string> myReadOnlyCollection = new ReadOnlyCollection<string>(maArray);
Console.WriteLine(myReadOnlyCollection[0]); // would work fine since the collection is read-only
Console.WriteLine(myReadOnlyCollection[1]); // would work fine since the collection is read-only
Console.WriteLine(myReadOnlyCollection[2]); // would work fine since the collection is read-only
myReadOnlyCollection[0] = "ServerFault"; // the [] accessor is neither defined nor would it be allowed since the collection is read-only.
Here you can find the according MSDN documentation.
Alternatively you might simply define a getter-method such as
public T getElement(int index) { return array[index]; }
in order to make your array read-only - at least from outside its class?
Regarding your use of Array.IsReadOnly the MSDN documentation says that
This property is always false for all arrays.
That means you will have to use IList<T>.IsReadOnly
instead of arr.IsReadOnly.
See here