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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T07:18:54+00:00 2026-05-21T07:18:54+00:00

Probably a simple question. I have an interface (MyInterface) that defines a property like

  • 0

Probably a simple question.

I have an interface (MyInterface) that defines a property like so:

IList<IMenuItem> MenuCollection { get; }

And the class implementing MyInterface

public class MyClass : MyInterface
{
    public ObservableCollection<MenuItemBase> MenuCollection 
    {
       get
       {
           ...
       }
    }
    ....
}

From what I read, ObservableCollection inherits from a Collection which is a IList, and I have MenuItemBase class that imlements IMenuItem – wouldn’t that satisfy the interface?

I suppose interfaces must be implemented explicitly?

I also tried this:

public class MyClass : MyInterface
{
    public IList<IMenuItem> MenuCollection MenuCollection 
    {
       get
       {
           if(_menuCollection == null)
              _menuCollection = new ObservableCollection<MenuItemBase>();
           return _menuCollection as IList<IMenuItem>;
       }
    }
    private ObservableCollection<MenuItemBase> _menuCollection;
}

Seems like a workaround hack (and I did run into a few issues saying that MenuCollection was not instantiated) to get the interface to be satisfied…. is there a better way to implement IInterface1<IInterface2> objects?

The reason why I need this kind of abstraction is because I’m building a prism / unity app and want to decouple the menu viewmodel as much as possible from the ribbon ui that displays the menu.

  • 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-21T07:18:54+00:00Added an answer on May 21, 2026 at 7:18 am

    This has nothing to do with generic variance

    There are multiple answers talking about generic variance. This has nothing to do with the example. If you try to implement a property defined as IList with an ArrayList (no generics), you will see that it’s still not possible to do so.

    The correct answer

    This behavior is because if you could implement MenuCollection as a property of some type implementing IList<T> (or deriving from it, if it were not specified as an interface), then this would be possible:

    public interface MyInterface
    {
        IList<IMenuItem> MenuCollection { get; set }
    }
    
    public class MyClass : MyInterface
    {
        // WARNING: Does not count as implementing the interface -- with good reason
        public ObservableCollection<MenuItemBase> MenuCollection { get; set; }
    }
    
    var myClass = new MyClass();
    var classAsInterface = (MyInterface) myClass; // This is OK of course
    
    classAsInterface.MenuCollection = new List<MenuItemBase>(); // OOPS!!
    

    In this last line, you have assigned a List<MenuItemBase> (which is OK as far as MyInterface can tell, since MyInterface.MenuCollection is of type IList<MenuItemBase>) to a property that is of type ObservableCollection<MenuItemBase>.

    Of course this is not legal to do, as List<MenuItemBase> clearly is not derived from ObservableCollection<MenuItemBase>. But it would be a possibility if you could implement the interface like that.

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

Sidebar

Related Questions

This is probably a simple question that I am just missing but I have
I have a language-agnostic question about an algorithm. This comes from a (probably simple)
Probably a long question for a simple solution, but here goes... I have a
This is probably a really simple question but... I would like to do a
Probably a simple question but I haven't found a solution. I have several view
I guess this is a really simple question and, probably, one that has been
Probably a simple question but I've developed an app that has always been using
This is probably a simple question but I am not an ASP.NET developer and
This is probably a simple question, and I'm slightly embarrassed to ask it, but
This is probably a simple question but I can't seem to find the solution.

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.