'Matching return types when returning an implementation of multiple interfaces
How can I match return types when returning an implementation of multiple interfaces if each of those interfaces is required by counterpart interface implementations in a single function?
That's my best shot at trying to put this into words, onto the code:
interface IA
{
IAA XYZ();
}
interface IAA
{
}
interface IB
{
IBB XYZ();
}
interface IBB
{
}
class IAABB : IAA, IBB
{
}
class T : IA, IB
{
public IAABB XYZ() { return null; }
}
The code above results in "T.XYZ() cannot implement IA.XYZ() because it does not have the matching return type of IAA". Same for IB.XYZ(), of course.
Solution 1:[1]
You can do this by implementing interfaces explicitly:
class T :IA,IB{
IAA IA.XYZ() { return XYZ(); }
IBB IB.XYZ() { return XYZ(); }
public IAABB XYZ() { return null; }
this will work only if IAA or IBB is an interface and not classes because you cannot inherit class IAABB from more than one base class.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Rafal |
