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

  • Home
  • SEARCH
  • 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 1016397
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T10:32:35+00:00 2026-05-16T10:32:35+00:00

I have specified a couple of interfaces, which I am implementing as entities using

  • 0

I have specified a couple of interfaces, which I am implementing as entities using Entity Framework 4. The simplest demonstration code I can come up with is:

public class ConcreteContainer : IContainer
{
    public EntityCollection<ConcreteChild> Children { get; set; }           
}
public class ConcreteChild : IChild
{
}
public interface IContainer
{
    IEnumerable<IChild> Children { get; set; }
}
public interface IChild
{        
}

I receive the following compiler error from the above:

‘Demo.ConcreteContainer’ does
not implement interface member
‘Demo.IContainer.Children’.
‘Demo.ConcreteContainer.Children’
cannot implement
‘Demo.IContainer.Children’
because it does not have the matching
return type of
‘System.Collections.Generic.IEnumerable’

My current understanding is that this is because IEnumerable (which is implemented by EntityCollection) is covariant but presumably not contravariant:

This type parameter is covariant. That is, you can use
either the type you specified or any type that is more
derived. For more information about covariance and contravariance,
see Covariance and Contravariance in Generics.

Am I correct, & if so, is there any way I can achieve my goal of specifying the IContainer interface purely in terms of other interfaces rather than using concrete classes?

Or, am I misunderstanding something more fundamental?

  • 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-16T10:32:36+00:00Added an answer on May 16, 2026 at 10:32 am

    The generic variance in .NET 4 is irrelevant here. The implementation of an interface has to match the interface signature exactly in terms of types.

    For example, take ICloneable, which looks like this:

    public interface ICloneable
    {
        object Clone();
    }
    

    It would be nice to be able to implement it like this:

    public class Banana : ICloneable
    {
        public Banana Clone() // Fails: this doesn't implement the interface
        {
            ...
        }
    }
    

    … but .NET doesn’t allow this. You can sometimes use explicit interface implementation work around this, like so:

    public class Banana : ICloneable
    {
        public Banana Clone()
        {
            ...
        }
    
        object ICloneable.Clone()
        {
            return Clone(); // Delegate to the more strongly-typed method
        }
    }
    

    However, in your case you can’t ever do that. Consider the following code, which would be valid if ConcreteContainer was considered to implement IContainer:

    IContainer foo = new ConcreteContainer();
    foo.Children = new List<IChild>();
    

    Now your property setter is actually only declared to work with EntityCollection<ConcreteChild>, so it clearly can’t work with any IEnumerable<IChild> – in violation of the interface.

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

Sidebar

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.