Reinventing The Wheel

Be careful when using readonly

When a reference type field is marked with readonly, only the pointer (memory address) is readonly, not the object the pointer refers to.

This means if you mark an array as readonly, you can still change the array contents.

Have a look at this code:

public class Program
{
    static void Main(string[] args)
    {
        ClassOne.SomeIntegers[0] = 4;
        ClassOne.SomeIntegers[1] = 5;
        ClassOne.SomeIntegers[2] = 6;
    }
}

public class ClassOne
{
    public static readonly int[] SomeIntegers = new int[] { 1, 2, 3 };
}

The above code compiles and runs fine.

If you want to have the array contents as readonly as well, you need to wrap the array in a ReadOnlyCollection<T>:

public class Program
{
    static void Main(string[] args)
    {
        //The next 3 lines provide the compile time error:
        //"Property or indexer 'System.Collections.ObjectModel.ReadOnlyCollection<int>.this[int]' cannot be assigned to -- it is read only"
        ClassOne.SomeIntegers[0] = 4;
        ClassOne.SomeIntegers[1] = 5;
        ClassOne.SomeIntegers[2] = 6;
    }
}

public class ClassOne
{        
    public static ReadOnlyCollection<int> SomeIntegers = new ReadOnlyCollection<int>(new int[] { 1, 2, 3 });
}