4

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

Robert Seder
  • 1,390
  • 1
  • 9
  • 19

5 Answers5

5

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);
Lee
  • 142,018
  • 20
  • 234
  • 287
  • I didn't think of this! This does most-closely give me what I want. I do ISerialier s = new MyCrazySerializer().Seri... intellisense first shows my custom interface, but with casting, I can see the IDataResult too... Thanks! – Robert Seder Jul 21 '12 at 19:46
  • You'll get a +1 from me if you write the explicit version to call the other version: `IDataResult ISerializer.Serialize(T instance { return Serialize(instance); }` This is certainly the way to do it if you can't modify the `ISerializer` interface. – Daniel Earwicker Jul 21 '12 at 20:12
5

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.

Daniel Earwicker
  • 114,894
  • 38
  • 205
  • 284
  • It doesn't seem like I should have to do this - but this would work. I wanted to have my interface be a little cleaner, ideally. – Robert Seder Jul 21 '12 at 19:47
3

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.

Oded
  • 489,969
  • 99
  • 883
  • 1,009
3

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
    }
}
Femaref
  • 60,705
  • 7
  • 138
  • 176
  • I think the OP has demonstrated enough competence as to why he expects it to work – parapura rajkumar Jul 21 '12 at 19:32
  • Why does it need to match exactly, and not respect a derived type? Why such a brittle implementation? Why not allow OO principles? Why not allow a derived type - that is at the heart of OO!!?!? – Robert Seder Jul 21 '12 at 19:39
  • See [Eric Lippert's answer](http://stackoverflow.com/questions/4348760/c-sharp-covariant-return-types-utilizing-generics/4349584#4349584) (one of the developers on the C# compiler team) on that topic. – Femaref Jul 21 '12 at 20:23
1

Unlike C++, C# does not have covariant return type.

stakx - no longer contributing
  • 83,039
  • 20
  • 168
  • 268
parapura rajkumar
  • 24,045
  • 1
  • 55
  • 85