How to detect if virtual method is overridden in c#

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...


Reflection is a good answer to this question.

using System.Reflection;

Type classType = typeof(ADerived);
MethodInfo method = classType.GetMethod("DoSomethingExtra");
if (method.DeclaringType == typeof(ABase))
    Console.WriteLine("DoSomethingExtra not overridden.");
else
    Console.WriteLine("DoSomethingExtra is overridden by " + method.DeclaringType.Name);

I hope that you will find this usefull.

Once I were implementing a special object viewer, when the object was unknown I would use ToString() unless it was not overridden.


There are a few options:

If derived classes must implement DoSomethingExtra(), then declare the method and the class as abstract. That will force a concrete derived class to have an implementation. You can then just call DoSomethingExtra() from base class code, knowing an implementation will exist.

abstract class ABase {

  public void DoSomething(object p)
  {
    p.Process();
    DoSomethingExtra(p);
  }

  public abstract void DoSomethingExtra(object p);
}

If derived class may implement the method, then just include a default implementation in the base class, which will be called if no derived implementation is available.

Another option is to have a flag that derived classes can set, indicating if they want something extra to be done:

class ABase {

  public virtual bool ShouldDoSomethingExtra { get { return false; } }

  public void DoSomething(object p)
  {
    p.Process();
    if(ShouldDoSomethingExtra)
      DoSomethingExtra(p);
  }
  public virtual void DoSomethingExtra(object p) { // Empty in base }
}

class ADerived {
  public override void DoSomethingExtra(object p)
  {
    p.ProcessMore();
  }

  public override bool ShouldDoSomethingExtra { get { return true; } }
}

This solution is a little brittle, though, since a derived class could forget to override the property when overriding the method. I think having a do-nothing base implementation is the easiest way to go.