locked
Property Accessors (Asymmetric) RRS feed

  • Question

  • According to the lanuguage specification, a protected instance member can be accessible only through an instance of the derived class type in which the access occurs. So, the following should yield the same result, but it does not.

    public class A
    {
        private int _var1;
        private int _var2;

        protected int Var1
        {
            get { return _var1; }
            set { _var1 = value; }
        }

        public int Var2
        {
            get { return _var1; }
            protected set { _var1 = value; }
        }
    }

    public class B : A
    {
        public void DoSomeWork()
        {
            A a = new A();

            a.Var1 = 10; // Error, will not compile (as specified)
            a.Var2 = 10; // Will work, but according to the language spec
                         // should not work
        }
    }

    So, my question is, are property accessors excluded from the above rule, since the Var2 can be set through the instance of A, which is not the derived type B.

     

    Wednesday, March 21, 2007 8:52 PM

Answers

  • Hi,

    This was a bug but it's fixed in the new (Orcas) compiler.

     

    Thursday, March 22, 2007 1:35 AM

All replies

  • I would consider that a bug, it does honor the "private" access modifier, though.
    Wednesday, March 21, 2007 9:10 PM
  • Hi,

    This was a bug but it's fixed in the new (Orcas) compiler.

     

    Thursday, March 22, 2007 1:35 AM