IList<T> to ObservableCollection<T>

You could write a quick and dirty extension method to make it easy

public static ObservableCollection<T> ToObservableCollection<T>(this IEnumerable<T> enumerable) {
  var col = new ObservableCollection<T>();
  foreach ( var cur in enumerable ) {
    col.Add(cur);
  }
  return col;
}

Now you can just write

return GetIlist().ToObservableCollection();

Er...

ObservableCollection does have a constructor that will take an IEnumerable<T>, and IList<T> derives from IEnumerable<T>.

So you can "just new one up"


The extension method that JaredPar has given you is your best option in Silverlight. It gives you the ability to turn any IEnumerable into observable collection automatically simply by refering to the namespace, and reduces code duplication. There is nothing built in, unlike WPF, which offers the constructor option.

ib.

Tags:

C#

Silverlight