I do this:
- Create a new
ObservableCollection & lt; T & GT;
- Rubbit
List & lt; T & GT;
inForeach
and at each iteration add toObservableCollection
list
element
It seems to me that it is not “chic.” How to make this task “chic”?
Will there be a good version of writing extension for this case?
Answer 1, Authority 100%
In essence, everything depends on how you will handle these objects.
-
If you just need to do from
List & lt; T & GT;
ObservableCollection & lt; T & GT;
, we can see the documentation (LIST & LT; T & GT;
OBSERVACOLLECTION & LT; T & GT;
) and immediately it will be seen that we can simply pass through the desiredIenumerable & lt; T & GT;
).var list = new list & lt; string & gt; (); var Observable = New ObservableCollection & lt; String & GT; (List);
-
If you have a certain transformation logic, then you can already make a
extension
method that will return the specified type. For example, create an extension method that returnsObservableCollection & LT; T & GT;
and as logic, we will make it so that it returned only N elements taken from the input list:static class extensions { Public Static ObservableCollection & LT; T & GT; TakeToobServable & lt; T & GT; (This ienumerable & lt; t & gt; enumerable, int count) = & gt; New ObservableCollection & LT; T & GT; (Enumerable.Take (Count)); }
Usage:
var observable = new list & lt; string & gt; (). TakeToobServable (10);
In general, see yourself, as you need to implement.
Good luck in learning C #!