Why can not I replace a private member in a nested class?

advertisements

In C#, nested classes can access private members of the containing class.

Why is it I can't override such members? And why does the compiler get the error wrong?

    private abstract class InheritanceTest
    {
        public virtual object Property
        {
            get { return null; }
            private set { }
        }

        public class Child : InheritanceTest
        {
            public override object Property
            {
                get { return null; }
                private set { base.Property = null; } // the base.Property = null statement here is just to show that there isn't an error message for accesing a parent private member.
            }
        }
    }

The only error message I get is:

'Program.InheritanceTest.Child.Property.set': cannot override inherited member 'Program.InheritanceTest.Property.set' because it is not marked virtual, abstract, or override

The compiler is clearly getting something wrong, because the whole property is marked as virtual. The get method can override the inherited member.

Is this part of the C# spec, and only the error message is wrong? Or should this be allowed?

What part of the spec am I missing? (Or is the compiler missing?)


Private virtual methods and properties cannot be overridden:

You cannot use the virtual modifier with the static, abstract, private, or override modifiers. virtual (C# Reference)

Also see Can you override private virtual methods?.