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 269555
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T23:51:23+00:00 2026-05-11T23:51:23+00:00

The following code: public interface ISomeData { IEnumerable<string> Data { get; } } public

  • 0

The following code:

public interface ISomeData
{
    IEnumerable<string> Data { get; }
}

public class MyData : ISomeData
{
    private List<string> m_MyData = new List<string>();
    public List<string> Data { get { return m_MyData; } }
}

Produces the following error:

error CS0738: ‘InheritanceTest.MyData’
does not implement interface member
‘InheritanceTest.ISomeData.Data’.
‘InheritanceTest.MyData.Data’ cannot
implement
‘InheritanceTest.ISomeData.Data’
because it does not have the matching
return type of
‘System.Collections.Generic.IEnumerable’.

Since a List<T> implements IEnumerable<T>, one would think that my class would implement the interface. Can someone explain what the rationale is for this not compiling?

As I can see it, there are two possible solutions:

  1. Change the interface to be more specific and require IList be implemented.
  2. Change my class (MyData) to return IEnumerable and implement the original interface.

Now suppose I also have the following code:

public class ConsumerA
{
    static void IterateOverCollection(ISomeData data)
    {
        foreach (string prop in data.MyData)
        {
            /*do stuff*/
        }
    }
}

public class ConsumerB
{
    static void RandomAccess(MyData data)
    {

        data.Data[1] = "this line is invalid if MyPropList return an IEnumerable<string>";
    }
}

I could change my interface to require IList to be implemented (option 1), but that limits who can implement the interface and the number of classes that can be passed into ConsumerA. Or, I could change implementation (class MyData) so that it returns an IEnumerable instead of a List (option 2), but then ConsumerB would have to be rewritten.

This seems to be a shortcoming of C# unless someone can enlighten me.

  • 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-05-11T23:51:24+00:00Added an answer on May 11, 2026 at 11:51 pm

    Unfortunately, the return type must match. What you are looking for is called ‘return type covariance’ and C# doesn’t support that.

    http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=90909

    Eric Lippert, senior developer on C# Compiler team, mentions on his blog that they don’t plan to support return type covariance.

    “That kind of variance is called
    “return type covariance”. As I
    mentioned early on in this series, (a)
    this series is not about that kind of
    variance, and (b) we have no plans to
    implement that kind of variance in C#.
    “

    http://blogs.msdn.com/ericlippert/archive/2008/05/07/covariance-and-contravariance-part-twelve-to-infinity-but-not-beyond.aspx

    It’s worth reading Eric’s articles on covariance and contravariance.

    http://blogs.msdn.com/ericlippert/archive/tags/Covariance+and+Contravariance/default.aspx

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 147k
  • Answers 147k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer There is no such limit Since you mention "..." i… May 12, 2026 at 9:13 am
  • Editorial Team
    Editorial Team added an answer By default, sqlite3's .import expects tab-delimited data. You can change… May 12, 2026 at 9:13 am
  • Editorial Team
    Editorial Team added an answer Group.Where(x => x != null) .SelectMany(g => combination.Where(c => c… May 12, 2026 at 9:13 am

Related Questions

The general rule is that I want to say, T has a method with
Can someone explain to me why in .NET 2.0 if I have an interface,
I'm designing a client-server chat application (in fact I'm not, but let's pretend I
I am a beginner in design patterns. I am trying to use Abstract Factory

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.