Skip to content

Instantly share code, notes, and snippets.

@MeirionHughes
Created January 20, 2016 14:04
Show Gist options
  • Save MeirionHughes/5223b6d4ba97b06672da to your computer and use it in GitHub Desktop.
Save MeirionHughes/5223b6d4ba97b06672da to your computer and use it in GitHub Desktop.
Read Only ObservableCollection as contract interface
public interface INotifyCollection<T>
: ICollection<T>,
INotifyCollectionChanged
{}
public interface IReadOnlyNotifyCollection<out T>
: IReadOnlyCollection<T>,
INotifyCollectionChanged
{}
public class NotifyCollection<T>
: ObservableCollection<T>,
INotifyCollection<T>,
IReadOnlyNotifyCollection<T>
{}
@MeirionHughes
Copy link
Author

Usage:

public class Program
{
    private static void Main(string[] args)
    {
        var full = new NotifyCollection<string>();
        var readOnlyAccess = (IReadOnlyCollection<string>) full;
        var readOnlyNotifyOfChange = (IReadOnlyNotifyCollection<string>) full;


        //Covarience
        var readOnlyListWithChanges = 
            new List<IReadOnlyNotifyCollection<object>>()
                {
                    new NotifyCollection<object>(),
                    new NotifyCollection<string>(),
                };
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment