Can I force subclasses to override a method without making it abstract?

.net, abstract-class, c#, visual-studio-2008

Solution

Well you could do some really messy code involving `#if` - i.e. in `DEBUG` it is virtual (for the designer), but in `RELEASE` it is abstract. A real pain to maintain, though.

But other than that: basically, no. If you want designer support it can't be abstract, so you are left with "virtual" (presumably with the base method throwing a `NotImplementedException`).

Of course, your unit tests will check that the methods have been implemented, yes? ;-p

Actually, it would probably be quite easy to test via generics - i.e. have a generic test method of the form:

[Test]
public void TestFoo() {
  ActualTest<Foo>();
}
[Test]
public void TestBar() {
  ActualTest<Bar>();
}

static void ActualTest<T>() where T : SomeBaseClass, new() {
  T obj = new T();
  Assert.blah something involving obj
}

Problem

I have a class with some abstract methods, but I want to be able to edit a subclass of that class in the designer. However, the designer can't edit the subclass unless it can create an instance of the parent class. So my plan is to replace the abstract methods with stubs and mark them as virtual - but then if I make another subclass, I won't get a compile-time error if I forget to implement them. Is there a way to mark the methods so that they have to be implemented by subclasses, without marking them as abstract?

Original source