This is a simplified example to explain my problem. I'm trying to improve the readability of the code to explicitly implemented a common interface, moving a common interface to interface with a particular type, like this: interface ITyped : IGeneric<string>. The idea is to not use IGeneric<string>in all implemented explicit method names.
So the code will read:
ITyped.M1(string p){}
ITyped.M2(string p){}
instead
IGeneric<string>.M1(string p){}
IGeneric<string>.M2(string q){}
Introduce an interface with several generic types to understand why I'm trying to do this.
Now the fact is that this will not work! The compiler throws an error, so my declaration is not a member of the interface. I have to explicitly use the base interface with a generic declaration. Wouldn't that make sense if it worked? Further attempts showed that not even a generic is required - a simple derived interface also does not allow explicitly implementing the inherited method of the base interface in the derived interface. I would like to understand the theory why it was forbidden.
Is there an alternative to improve readability for this scenario?
Example code:
interface IGeneric<T>
{
void M(T t);
}
interface ITyped: IGeneric<string> { }
class C : ITyped
{
void IGeneric<string>.M(string t) {}
public void M(string t){ }
void ITyped.M(string t) {}
}
source
share