Quantcast
Channel: .NET Framework Class Libraries forum
Viewing all articles
Browse latest Browse all 8156

Why is ObservableCollection Constructor (List) and not (IList)?

$
0
0

ObservableCollection<T> inherits from Collection<T> which has the constructor defined as (IList<T>).
Why not define the parameter as IList<T> or ICollection<T>?

I can only see that the Count property and the IEnumerable<T> interface is used.

public ObservableCollection(List<T> list)
    : base((list != null) ? new List<T>(list.Count) : list)
{
    CopyFrom(list);
}

private void CopyFrom(IEnumerable<T> collection)
{
    IList<T> items = Items;
    if (collection != null && items != null)
    {
        foreach (var item in collection)
        {
            items.Add(enumerator.Current);
        }
    }
}
The if statement ensures that either a new List<T> or null is passed down to the base class Collection<T>.

In the case of null an ArgumentNullException exception is  thrown::

public Collection(IList<T> list)
{
    if (list == null)
    {
        ThrowHelper.ThrowArgumentNullException(ExceptionArgument.list);
    }

    items = list;
}





Viewing all articles
Browse latest Browse all 8156

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>