Domenic
Domenic

Reputation: 112857

Is there a way to make derived classes override ToString()?

(I'm working in .NET 4.0 beta, C#.)

I have an interface, and all classes derived from this interface should implement custom ToString() logic. Is that enforceable? If so, how?

Upvotes: 8

Views: 2696

Answers (1)

Noon Silk
Noon Silk

Reputation: 55132

Not through an interface.

You'll need to use an abstract class for this.

-- Edit

You can just re-declare 'ToString' as abstract:

abstract class Foo
{
    public override abstract string ToString ();
}

Upvotes: 12

Related Questions