Forced interface subclasses for implementing ToString

Say I have an IFoo interface, and I want all IFoo subclasses IFoo override the Object ToString method. Is it possible?

Just adding a method signature to IFoo as such does not work:

 interface IFoo { String ToString(); } 

since all subclasses extend Object and provide implementation in this way, so the compiler does not complain about it. Any suggestions?

+46
c # oop
Feb 04 '09 at 7:01
source share
6 answers

I do not believe that you can do this using an interface. You can use an abstract base class:

 public abstract class Base { public abstract override string ToString(); } 
+67
Feb 04 '09 at 7:08
source share
β€” -
 abstract class Foo { public override abstract string ToString(); } class Bar : Foo { // need to override ToString() } 
+21
Feb 04 '09 at 7:08
source share

John and Andrew: this abstract trick is really useful; I had no idea that you could end the chain by declaring it abstract. Greetings :)

In the past, when I required to override ToString () in derived classes, I always used a template similar to the following:

 public abstract class BaseClass { public abstract string ToStringImpl(); public override string ToString() { return ToStringImpl(); } } 
+6
Jun 06 '09 at 0:58
source share

Implementing an interface method implicitly seals the method (and also overrides it). So, unless you specify otherwise, the first interface implementation ends the C # redefinition chain.

Essential.NET

Abstract class = your friend

Mark this question

+1
Feb 04 '09 at 7:13
source share

I know this does not answer your question, but since you do not have the opportunity to do what you ask, I thought I would share my own approach so that others could see.

I use a hybrid of the offers offered by Mark and Andrew.

In my application, all domain objects are obtained from an abstract base class:

 public abstract class Entity { /// <summary> /// Returns a <see cref="System.String"/> that represents this instance. /// </summary> public override string ToString() { return this is IHasDescription ? ((IHasDescription) this).EntityDescription : base.ToString(); } } 

The interface itself defines a simple accessor:

 public interface IHasDescription : IEntity { /// <summary> /// Creates a description (in english) of the Entity. /// </summary> string EntityDescription { get; } } 

So, now the built-in return mechanism - or, in other words, Entity , which implements IHasDescription , should provide EntityDescription , but any Entity can still convert to string.

I know that this is not radically different from the other solutions proposed here, but I like the idea of ​​minimizing the responsibility of the basic Entity type, so the implementation of the description interface remains optional, but you are forced to actually implement the description method if you implement the interface.

IMHO, the interfaces implemented by the base class object should not be "counted" as implemented - it would be nice to have a compiler option for it, but, well, good ...

+1
Aug 04 2018-11-18T00:
source share

I do not think that you can force any subclass to override any of the virtual methods of the base class if these methods are not abstract.

0
Feb 04 '09 at 7:08
source share



All Articles