Force subclasses of an interface to implement ToString
I don't believe you can do it with an interface. You can use an abstract base class though:
public abstract class Base
{
public abstract override string ToString();
}
abstract class Foo
{
public override abstract string ToString();
}
class Bar : Foo
{
// need to override ToString()
}
Jon & Andrew: That abstract trick is really useful; I had no idea you could end the chain by declaring it as abstract. Cheers :)
In the past when I've required that ToString() be overriden in derived classes, I've always used a pattern like the following:
public abstract class BaseClass
{
public abstract string ToStringImpl();
public override string ToString()
{
return ToStringImpl();
}
}