Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8266613
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T05:13:31+00:00 2026-06-08T05:13:31+00:00

Assume you have this: // General purpose public interface ISerializer { IDataResult Serialize<T>(T instance);

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T05:13:33+00:00Added an answer on June 8, 2026 at 5:13 am

    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);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Assume I have this domain object... public class SpansMultipleTables { public int CommonID {get;
Assume I have a C# class like this: [XmlRoot(floors)] public class FloorCollection { [XmlElement(floor)]
Assume I have a class that looks like this: class Sample { public string
Assume I have this model : class Task(models.Model): title = models.CharField() Now I would
Assume that I have this piece of code: @interface Foo : NSObject { Bar
This question is specific to Erlang, but may have general implications to other IO
What are my options for printing in Silverlight 3? Assume I have this awesome
Assume I have executed this js code: var container=function() { //do something } container.a=function
So I have this piece of code, and let's assume I'm filling out a
Assume I have a table like this: column A(int) column B(int) 1 2 2

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.