Assume you have this:
// General purpose
public interface ISerializer
{
IDataResult Serialize<T>(T instance);
}
// General purpose
public interface IDataResult
{
}
// Specific - and I implement IDataResult
public interface IMyCrazyDataResult : IDataResult
{
}
public class MyCrazySerializer : ISerializer
{
// COMPILE ERROR:
// error CS0738: 'MyCrazySerializer' does not implement interface member 'ISerializer.Serialize<T>(T)'.
// 'MyCrazySerializer.Serialize<T>(T)' cannot implement 'ISerializer.Serialize<T>(T)' because it does
// not have the matching return type of 'IDataResult'.
public IMyCrazyDataResult Serialize<T>(T instance)
{
throw new NotImplementedException();
}
}
Why in the world do I get this compile error? I am respecting the interface - I do, in-fact, return an IDataResult, albeit indirectly. Is it that the compiler can't figure that out or is there something fundamentally (at an OO level) wrong, here?
I thought the entire point of having an interface was that I could guarantee some implementation, but leave it open for me to add-on to it. That is what I am doing - yet I get a compile error.
In my real code, I want the return type to be a bit more specific because I have several additional methods that I have in my derived interface. If I make the return type of MyCrazySerializer.Serialize of-type IDataResult, then intellisense just shows me and the bare-bones common methods, where I want to show a more-specific interface.
How else could I accomplish this? What is wrong with this code???
C# does not support return type covariance so you'll need to implement the Serialize<T>
method exactly as it appears on the interface. You could implement it explicitly however, meaning any clients which know the real type of MyCrazySerializer
can access the more specific method:
public class MyCrazySerializer : ISerializer
{
public IMyCrazyDataResult Serialize<T>(T instance)
{
throw new NotImplementedException();
}
IDataResult ISerializer.Serialize<T>(T instance)
{
return this.Serialize(instance);
}
}
As the comment point out, you can simply call the more specific version in your explicit implementation.
Which you can use as:
IMyCrazyDataResult result = new MyCrazySerializer().Serialize<int>(1);
ISerializer serializer = (ISerializer)new MyCrazySerializer();
IDataResult = serializer.Serialize<int>(1);
You can build your own kind of return type covariance in C#:
// General purpose
public interface ISerializer<out TResult> where TResult : IDataResult
{
TResult Serialize<T>(T instance);
}
// General purpose
public interface IDataResult
{
}
// Specific - and I implement IDataResult
public interface IMyCrazyDataResult : IDataResult
{
}
public class MyCrazySerializer : ISerializer<IMyCrazyDataResult>
{
public IMyCrazyDataResult Serialize<T>(T instance)
{
throw new NotImplementedException();
}
}
The return type of Serialize
is explicitly stated to be something that derives from IDataResult
, instead of being precisely TResult
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With