Friday, April 19, 2013

C# extension method for a generic class

As an example, let's extend ObservableCollection class with AddRange method.

namespace Vurdalakov
{
    using System;
    using System.Collections.Generic;
    using System.Collections.ObjectModel;

    static public class ObservableCollectionExtensions
    {
        static public void AddRange<T>(this ObservableCollection<T> observableCollection, IEnumerable<T> items)
        {
            foreach (var item in items)
            {
                observableCollection.Add(item);
            }
        }
    }
}

No comments:

Post a Comment