c# - convert IList<Derived> to IReadOnlyCollection<Base> -


i have private field ilist<derivedclass> _elements , want create property returning ireadonlycollection<baseclass>.

public ireadonlycollection<baseclass> baseclasses {     { return _elements; } // compile time error } 

how without running compile errors?

(afaik should work, t in ireadonlycollection<t> covariant)

use list<t>.asreadonly() method:

public ireadonlycollection<baseclass> baseclasses {     { return _elements.cast<baseclass>().tolist().asreadonly(); } // compile time error } 

or since ireadonlycollection<t> covariant can skip cast<baseclass>:

public ireadonlycollection<baseclass> baseclasses {     { return _elements.tolist().asreadonly(); } // compile time error } 

you skip tolist() if field list<t>, not ilist<t>.


Comments

Popular posts from this blog

How to mention the localhost in android -

php - Calling a template part from a post -

c# - String.format() DateTime With Arabic culture -