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???