Tuesday, October 20, 2009

IEnumerable Extension

This is an IEnumerable extension method, that create a generic ObservableCollection from a generic IEnumerable.

public static class CollectionExtensions
{
public static ObservableCollection<T> ToObservableCollection<T>(this IEnumerable<T> ienum)
{
ObservableCollection<T> oc = new ObservableCollection<T>();

foreach (T i in ienum)
{
oc.Add(i);
}

return oc;
}
}

Have the extension method residing in a namespace that can be common accessible by all.

Please let me know if this helps you.