Inconsistency between abstract method and interface method return type checking (CS0738) [duplicate]

20 hours ago 1
ARTICLE AD BOX

Let's say I have this C# code. Why is it OK that I return a string from ClassA.DoThing(), but I cannot return a string from ClassB.DoThing()?

public abstract class MyBase { public abstract object DoThing(); } public interface MyInterface { public object DoThing(); } public class ClassA : MyBase { public override string DoThing() => ""; // ✅ Returning a string is allowed here. } public class ClassB : MyInterface { public string DoThing() => ""; // ❌ But I cannot return a string here (CS0738). Why? }

Why is returning a string allowed for the method override, but not for the interface implementation? I assume it should follow the same rules.

Trying to compile ClassB.DoThing() results in this error:

CS0738: 'ClassB' does not implement interface member 'MyInterface.DoThing()'. 'ClassB.DoThing()' cannot implement 'MyInterface.DoThing()' because it does not have the matching return type of 'object'.

enter image description here

Read Entire Article