Is there something comparable to explicit interface implementation but for classes instead in C#?
Consider the following situation:
Company X provides a library containing a class as follows:
public class LibraryClass
{
public virtual void A() { }
public void DoWork()
{
// does something
}
}
Company Y uses this library in one of its products and inherits from LibraryClass:
public class UserClass : LibraryClass
{
public virtual void B() { }
}
So far everything works fine. But at someday X releases a new library version and adds a virtual method B()to LibraryClass:
public class LibraryClass
{
public virtual void A() { }
public virtual void B() { }
public void DoWork()
{
// does something
// now uses B with certain semantic assumptions
}
}
Now Y updates to the new library version. While compiling with a reference to the new version, the compiler emits a warning saying that UserClass.B() is hiding the inherited method LibraryClass.B() and therefore should either specify the new keyword or override the method. Because there is a semantic gap between the existing method UserClass.B() and the newly introduced method LibraryClass.B() Y decides to introduce the new keyword because any existing override of UserClass.B() will probably not provide the semantics expected by DoWork() which would break the code. On the other hand Y wants to use a new feature of the library which would require an override of LibraryClass.B(). Now this is not possible: If the override would be done in a derived class of UserClass the override would refer to UserClass.B() due to the new keyword; an override of B in UserClass itself is not even allowed as it already defines a public method with that signature.
This situation could be solved if there was either a way in a derived class of UserClass to specify that the override refers to LibraryClass.B() which is not possible as far as I know -or- if B() could be explicitly overriden in UserClass:
public class UserClass : LibraryClass
{
...
// Override this method in order to change the behavior of LibraryClass.B()
public virtual void LibraryB() { }
private void override LibraryClass.B()
{
LibraryB();
}
...
}
Is there any way in the language to solve this situation other than renaming the original B() in UserClass (which might not even be possible if it was part of a library itself which is consumed by company Z)? If not, is this a C# limitation or a limitation of the CLR?
Sorry for the long post and thank you for reading up to this point.
Edit: This is not a CLR limitation. C++/CLI supports named overrides which solve the situation, so you could do something like virtual void LibraryB(void) = LibraryClass::B;. The C# design team probably just missed this issue.