这篇文章主要为大家展示了“c#中ObservableCollection

创新互联是一家集网站建设,高唐企业网站建设,高唐品牌网站建设,网站定制,高唐网站建设报价,网络营销,网络优化,高唐网站推广为一体的创新建站企业,帮助传统企业提升企业形象加强企业竞争力。可充分满足这一群体相比中小企业更为丰富、高端、多元的互联网需求。同时我们时刻保持专业、时尚、前沿,时刻以成就客户成长自我,坚持不断学习、思考、沉淀、净化自己,让我们为更多的企业打造出实用型网站。
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 oc = new ObservableCollection();
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());
}
}
}
} 以上是“c#中ObservableCollection