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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T14:34:09+00:00 2026-06-04T14:34:09+00:00

I have two classes defined like public class PostleitzahlList : ObservableCollection<Postleitzahl> { } public

  • 0

I have two classes defined like

public class PostleitzahlList : ObservableCollection<Postleitzahl> {
}

public class Postleitzahl : IPostleitzahl {
}

Now I have a Service-Class which contains

PostleitzahlList _postleitzahlList;

This Serviceclass has also to implement a Property of a Service-Interface which returns _postleitzahlList – but this Interface only knows IPostleitzahl – it doesn’t know PostleitzahlList or Postleitzahl. this Property should be used for Binding in WPF.

I am trying now to declare and implement this Property. I have tried

    public ObservableCollection<IPostleitzahl> PostleitzahlList {
        get { return this._postleitzahlList; }
    }

and

    public IList<IPostleitzahl> PostleitzahlList {
        get { return this._postleitzahlList; }
    }

But both does not work.
The fallowing seems to work:

    public IEnumerable<IPostleitzahl> PostleitzahlList {
        get { return this._postleitzahlList; }
    }

I ask me now
1. why does the first and second try not working?
2. what is the best solution to solve this?

  • 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-04T14:34:11+00:00Added an answer on June 4, 2026 at 2:34 pm

    The problem is with covariance. An ObservableCollection<Postleitzahl> isn’t an ObservableCollection<IPostleitzahl>, and ditto with lists. Here’s an example of why not:

    ObservableCollection<string> strings = new ObservableCollection<string>();
    
    // This is invalid, but it's what you're trying to do, effectively.
    ObservableCollection<object> objects = strings;
    
    // This would have to work... it's fine...
    objects.Add(new object());
    
    // And this should be fine too...
    string x = strings[0];
    

    … but as you can see, you’re now trying to fetch a non-string reference and store it in a string variable. The only viable outcome would be an execution-time failure… and half the point of generics is to push error detection to compile-time.

    Now IEnumerable<T> is covariant in T because you can’t add any items via it – that makes it safe to apply that sort of conversion:

    // There's nothing you can do to violate type safety here...
    Observable<string> strings = new ObservableCollection<string>();
    IEnumerable<object> objects = strings;
    

    For more information, read up on covariance and contravariance in generics in MSDN.

    Do you need the IPostleitzahl interface? If you just exposed the properties via ObservableCollection<Postleitzahl> or IList<Postleitzahl> it would be fine. Alternatively, you could change your variable to be an ObservableCollection<IPostleitzahl> and just happen to populate it by creating Postleitzahl instances.

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

Sidebar

Related Questions

I have two connected classes defined like this: [ActiveRecord] public class Store : ActiveRecordBase<Store>
I have two classes, user and role, defined as: public class User : Entity
I have two classes defined as such: public class Questionnaire { public int QuestionnaireID
I have a method and two classes defined like this: template<template<class X> class T>
I defined two classes: class Token_ { public: virtual char operator*()const = 0;//this fnc
Say I have two classes, in two different headers, called: class TestA { public:
if I have two classes, and have defined an explicit type conversion between them,
I have two classes that I would like to merge into a composite. These
I have two classes in a PHP application, B extending A for example. Class
I have two classes one of which inherits from the other. Im trying to

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.