tags:

views:

183

answers:

3

I think you can and my colleage thinks you cannot!

+23  A: 

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

Jon Skeet
A: 

I cannot even imagine why one should be allowed to create a private virtual method. By definition, a derived class could not see it.

Thomas
A derived class could access it if the derived class were in the accessibility domain of the private member.
Eric Lippert
+2  A: 

Your colleague is right. You can't declare private virtual methods because there's no point (since there'd be no way to override them)...

But you can override protected virtual methods.

Justin Niessner
Sure you could override them; see Jon's example.
Eric Lippert
But even in Jon's example, the compiler won't allow it.
Justin Niessner
My point is simply that your claim that "there would be no way to override them" is incorrect. There would be a way to override them. The reason private virtual methods are illegal is because the language design committee doesn't like them, not because they are logically inconsistent.
Eric Lippert
Ah...that makes sense. I see what you'te saying and totally agree.
Justin Niessner