So in C# it is possible to declare a virtual method without implementing it.
This is not possible. You can declare a method as abstract, but if the method is virtual, it will have some implementation (even if the implementation is effectively a null op).
Your code above reports the error Error 1 'ABase.DoSomethingExtra(object)' must declare a body because it is not marked abstract, extern, or partial
.
The typical way to handle this is to just declare the method with a null op implementation, and call it:
class ABase {
public void DoSomething(object p)
{
p.Process();
DoSomethingExtra(p); // Always call
}
public virtual void DoSomethingExtra(object p)
{
// Do nothing here
}
}
Edit: Now that your question has been edited, I'll give you more information related to your edit, in particular, this:
I realize that this example seems stupid (e.g. why don't you just call DoSomethingExtra() since it doesn't do anything). I assure you I have a legitimate case for this. Any ideas?
There is no direct way to determine whether the current instance has overriden your virtual method. This would likely require some pretty nasty, unmaintainable code, such as checking the method body declaring type via reflection to see what is there.
That being said, I would strongly question the design goal here. Your question is basically asking for a specific way to violate the Liskov Substitution Principle, which is one of the core principles in object oriented programming. This is going to have the effect of making your code less performant and much less maintainable...