using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.ObjectModel;
namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            //可观察的集合
            ObservableCollection<string> oc = new ObservableCollection<string>();
            oc.CollectionChanged+=oc_CollectionChanged;
            oc.Add("a");
            oc.Add("b");
            oc.Insert(2, "c");
            oc.Remove("c");
            foreach (var item in oc)
            {
                Console.WriteLine(item);
            }
            Console.ReadKey();

            //输出:
            //操作:Add
            //被添加的索引:0
            //操作:Add
            //被添加的索引:1
            //操作:Add
            //被添加的索引:2
            //操作:Remove
            //被添加的索引:2
            //a
            //b

        }
        private static void oc_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            Console.WriteLine("操作:{0}", e.Action.ToString());//引起该事件的操作
            if (e.NewItems != null)//添加
            {
                Console.WriteLine("被添加的索引:{0}", e.NewStartingIndex.ToString());
            }
            if (e.OldItems != null)//删除
            {
                Console.WriteLine("被删除的索引:{0}", e.OldStartingIndex.ToString());
            }
        }
    }
}