Reputation: 1420
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???
Upvotes: 4
Views: 904
Reputation: 144136
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);
Upvotes: 5
Reputation: 116674
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
.
Upvotes: 5
Reputation: 61437
You aren't fulfulling the interface contract, as you specify something different as the return type of the method, the signatures have to match completely. This doesn't mean you can't return the more specified interface, the following is perfectly fine:
// 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
{
public IDataResult Serialize<T>(T instance)
{
// return a IMyCrazyDataResult here
}
}
Upvotes: 3
Reputation: 499002
Why in the world do I get this compile error? I am respecting the interface
No, you are not. You are not returning a IDataResult
, but a IMyCrazyDataResult
. Yes, it inherites from IDataResult
, but it isn't identical to it.
When it comes to interfaces, you don't get variance - types must match exactly.
Upvotes: 3