How can I access and change elements in this private readonly property?

Posted by CrimsonX on Stack Overflow See other posts from Stack Overflow or by CrimsonX
Published on 2011-01-04T17:50:45Z Indexed on 2011/01/04 17:53 UTC
Read the original article Hit count: 257

Filed under:
|

I'm trying to figure out how I am able to successfully change a "readonly" array. I have this:

namespace ConsoleApplication1
{
  class Program
  {
    static void Main(string[] args)
    {
      MyClass myClass = new MyClass();
      myClass.Time[5] = 5; // How is this legal?
    }
  }

  public class MyClass
  {
    private readonly uint[] time;
    public IList<uint> Time
    {
      get { return time; }
    }

    public MyClass()
    {
      time = new uint[7];
    }
  }
}

As I Note above, I would expect that Time[5] would be illegal due to the fact that public IList Time does not have a setter.

Additionally, how can I create an array in the constructor which is read-only and unchangeable outside of this class?

© Stack Overflow or respective owner

Related posts about c#

Related posts about readonly