IList <T>, IEnumerable <T> and ObservableCollection <T>
I have two classes defined as
public class PostleitzahlList : ObservableCollection<Postleitzahl> {
}
public class Postleitzahl : IPostleitzahl {
}
Now I have a Service-Class that contains
PostleitzahlList _postleitzahlList;
This Serviceclass must also implement the Service-Interface Property, which returns _postleitzahlList, but this interface only knows IPostleitzahl- it does not know PostleitzahlListor Postleitzahl. this property should be used for binding in WPF.
I am trying to declare and implement this property. I tried
public ObservableCollection<IPostleitzahl> PostleitzahlList {
get { return this._postleitzahlList; }
}
and
public IList<IPostleitzahl> PostleitzahlList {
get { return this._postleitzahlList; }
}
But both do not work. It looks like the work looks like:
public IEnumerable<IPostleitzahl> PostleitzahlList {
get { return this._postleitzahlList; }
}
I ask myself now 1. Why are the first and second attempts not working? 2. What is the best solution to solve this problem?
. ObservableCollection<Postleitzahl> ObservableCollection<IPostleitzahl>, . , :
ObservableCollection<string> strings = new ObservableCollection<string>();
// This is invalid, but it what you're trying to do, effectively.
ObservableCollection<object> objects = strings;
// This would have to work... it fine...
objects.Add(new object());
// And this should be fine too...
string x = strings[0];
... , , , , . -... , .
IEnumerable<T> T, - - :
// There nothing you can do to violate type safety here...
Observable<string> strings = new ObservableCollection<string>();
IEnumerable<object> objects = strings;
IPostleitzahl? ObservableCollection<Postleitzahl> IList<Postleitzahl>, . , ObservableCollection<IPostleitzahl> , Postleitzahl.