I can’t do this
interface InterfaceA
{
void MethodA();
}
class ClassA : InterfaceA
{
virtual void InterfaceA.MethodA()
// Error: The modifier 'virtual' is not valid for this item
{
}
}
Where the following works
class ClassA : InterfaceA
{
public virtual void MethodA()
{
}
}
Why? How to circumvent this?
I think this is because when a member is explicitly implemented, it cannot be accessed through a class instance, but only through an instance of the interface.
So making something ‘virtual’ really does not make sense in this case, since virtual means that you intend to override it in an inherited class. Implementing an interface explicitly and making it virtual would be contradictory. Which also may be why the compiler disallows this.
To work around it I think csharptest.net’s or Philip’s answer sounds like it would do the trick