Can you override private virtual methods?

前端 未结 3 1463
失恋的感觉
失恋的感觉 2021-02-15 00:43

I think you can and my colleage thinks you cannot!

3条回答
  •  借酒劲吻你
    2021-02-15 01:05

    You can't even declare private virtual methods. The only time it would make any sense at all would be if you had:

    public class Outer
    {
        private virtual void Foo() {}
    
        public class Nested : Outer
        {
            private override void Foo() {}
        }
    }
    

    ... that's the only scenario in which a type has access to its parent's private members. However, this is still prohibited:

    Test.cs(7,31): error CS0621: 'Outer.Nested.Foo()': virtual or abstract members cannot be private
    Test.cs(3,26): error CS0621: 'Outer.Foo()': virtual or abstract members cannot be private

提交回复
热议问题