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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T15:10:34+00:00 2026-05-28T15:10:34+00:00

I’m exposing an API through a facade with methods returning interface types, and I

  • 0

I’m exposing an API through a facade with methods returning interface types, and I have run into an issue with generic lists and inheritance in C#/.NET 3.5.

I have two interfaces:
IMyList (implements IList<IMyItem>)
IMyItem

And three classes:
MyList (implements IMyList, extends List<MyItem>)
MyOtherList (implements IMyList, extends ObservableCollection<MyItem>)
MyItem (implements IMyItem)

But it doesn’t seem possible. How should I go about exposing only what is necessary, but still call the right implementations of the methods (that can vary for instance between MyList and MyOtherList)?

EDIT:
My facade is a factory looking something like this:

public static class Facade {
    public static IMyList<IMyItem> CreateList() {
        return new MyList<MyItem>();
    }

    public static IMyItem CreateItem() {
        return new MyItem();
    }

    public static IConfiguration CreateConfiguration() {
        return new Configuration();
    }
} 

Usage:

var list = Facade.CreateList();
list.DoSomethingOnChildren();

Now I’m expecting DoSomethingOnChildren() implemented in MyList to execute on a series of MyItem objects. If I was to change the method to return:

public static IMyList<IMyItem> CreateList() {
    return new MyOtherList<MyOtherItem>();
}

I would expect DoSomethingOnChildren() implemented in MyOtherList to execute on a series of MyOtherItem objects.

  • 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-28T15:10:35+00:00Added an answer on May 28, 2026 at 3:10 pm

    Just because MyItem implements IMyItem doesn’t mean MyList<MyItem> implements IMyList<IMyItem>. Put it this way, assume you had:

    IList<Shape> s = new List<Rectangle>();
    

    IF this were allowed, it would cause tons of problems because it would let you do:

    s.Add(new Circle());
    

    Since Circle is also a Shape. Some generic interfaces support co/contra-variance because the generic type parameter is used only in an in or out fashion, because in IList<T> however, the T argument is used both in in and out positions, this makes it difficult, and C# doesn’t support it.

    So, you can’t convert the references, but you could load the members of an MyList<MyItem> into an IMyList<IMyItem> through other means (LINQ, etc) and return a new instance of the list.

    If you wanted to support the two interface, but also allow a specific implementation, you could use explicit interface implementation.

    UPDATE:

    So, if you want to do something similar, you could do this instead. Have your interface return back the more generic implementation, then have your implementing classes have an explicit implementation to return only a generic implementation, and an overload for the specific implementation.

    So something like:

    // purely for illustrative purposes
    public interface IShape { }
    public class Rectangle : IShape { }
    
    // represents your more "generic" interface
    public interface ShapeMaker
    {
        List<IShape> GetShapes();
    }
    
    // Your specific implementation
    public class RectangleMaker : ShapeMaker
    {
        // the explicit implementation of the interface satisfies the 
        // original, and behaves like original when called from an ShapeMaker 
        // interface reference
        List<IShape> ShapeMaker.GetShapes()
        {
            return new List<IShape>();
        }
    
        // but, we also provide an overload that returns a more specific version
        // when used with a reference to our subclass.  This gives us more 
        // functionality.
        public List<Rectangle> GetShapes()
        {
            return new List<Rectangle>();
        }
    }
    

    You can only do this with explicit interface implementation, because you must still satisfy the original interface (must have same return type). But, it also allows us to say if used from the subclass reference, it can use the more specific method instead.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I am trying to loop through a bunch of documents I have to put
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
I have this code to decode numeric html entities to the UTF8 equivalent character.
I am currently running into a problem where an element is coming back from
I would like to run a str_replace or preg_replace which looks for certain words

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.