reddy reddy - 1 year ago 51
C# Question

Cannot hide inherited member compiler error

I'm tring to do :

public abstract class Base
public abstract Task Execute();

public abstract class Concrete<T> : Base where T : class
new public abstract Task<T> Execute();

But for some reason I am getting the compiler error :

CS0533 'Concrete.Execute()' hides inherited abstract member 'Program.Base.Execute()

I've hidden plenty of members in the past but never met this side case and I'm quite puzzled here. Spent a long time on MSDN and the web but couldn't find anything about this behaviour.

I would really appreciate any insight on the issue.

Here's the fiddle.

Answer Source

The problem is that the base method is abstract. A class inheriting from Concrete<T> would have to override Base.Execute(), but it could not override it, because it is hidden by Derived<T>.Execute(). So, Concrete<T> would be an abstract class that can't possibly have any implementation (at least not in C#), at thus it would be useless. So, the C# compiler does not let you write it.

If Base was an interface, you could work around this by using an explicit interface implementation. But there is nothing like explicit base class implementation, so I don't think there is any way to have this kind of code, at least not without renaming one of the two methods.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download