c#之集学习笔记

来源:互联网 发布:福冈攻略软件 编辑:程序博客网 时间:2024/05/10 20:27

(1) 包含不重复元素的集合称为“集(set)”.NET4包含两个集(HashSet和SortedSet),他们都实现ISet街口。HashSet集合包含不重复元素的无序列表,SortedSet集合包含不重复元素的有序列表。
ISet接口提供的方法可以创建合集,交集,或者给出一个集是另一个集的超集或子集的信息。
其中HashSet集实现ICollection接口。

using System;using System.Collections.Generic;public class Test{    static void Main()    {        var companyTeams = new HashSet<string>() { "Ferrari", "McLaren", "Toyota", "BMW", "Renault" };        var traditionalTeams = new HashSet<string>() { "Ferrari", "McLaren" };        var privateTeams = new HashSet<string>() { "Red Bull", "Toro Rosso", "Force India", "Brawn GP" };        //Addd(0方法返回一个bool值,说明是否添加了元素。如果该元素已经在集中就不添加它,并返回false;        if (privateTeams.Add("Williams"))            Console.WriteLine("Williams added");        if (!companyTeams.Add("McLaren"))            Console.WriteLine("McLaren was already in this set");        //IsSubsetOf()和IsSupersetOf()方法比较集合实现了IEnumerable<T>接口的集合,并返回一个布尔结果。        //其中,IsSubsetOf()方法验证traditionalTeams集合中的元素是否都包含在companyTeams集合方法中,IsSupersetOf()方法验证        //traditionTeams集合中的每个元素是否都包含在companyTeams集合方法中,IsSupersetOf()方法验证traditionalTeams集合是否有与        //companyTeams集合比较的额外元素(traditionalTeams是否是companyTeams集合的超集)。        if(traditionalTeams.IsSubsetOf(companyTeams))        {            Console.WriteLine("traditionalTeams is subset of companyTeams");        }        if(companyTeams.IsSupersetOf(traditionalTeams))        {            Console.WriteLine("companyTeams is a superset of traditionalTeams");        }        traditionalTeams.Add("Williams");        //Overlaps()方法验证privateTeams是否与traditionTeams有重叠的元素。        if(privateTeams.Overlaps(traditionalTeams))        {            Console.WriteLine("At least one team is the same with the " + "traditional and private teams");        }        var allTeams = new SortedSet<string>(companyTeams);        //调用UnionWith()方法,把引用新SortedSet<string>的变量allTeams填充为companyTeams,privateTeams和traditionalTeams的合集。        allTeams.UnionWith(privateTeams);        allTeams.UnionWith(traditionalTeams);        Console.WriteLine();        Console.WriteLine("all teams");        foreach(var team in allTeams)        {            Console.WriteLine(team);        }        //ExceptWith()方法从allTeams集中删除所有私有队。        allTeams.ExceptWith(privateTeams);        Console.WriteLine();        Console.WriteLine("no private team left");        foreach(var team in allTeams)        {            Console.WriteLine(team);        }    }}

(2)如果需要集合中的元素何时删除或添加的信息,就可以使用ObservableCollection类。这个类是为WPF定义的,这样UI就可以得知集合的变化,因此这个类在程序集WindowsBase中定义,同时用户需要引用这个程序集。这个类的名称空间是System.Collections.ObjectModel.
ObservableCollection类派生自Collection基类,该基类可用于创建自定义集合,并在内部使用List类。重写基类中的虚方法SetItem()和RemoveItem(),以触发CollectionChanged事件。这个类的用户可以使用INotifyCollectionChanged接口注册这个事件。

using System;using System.Collections.Generic;using System.Collections.ObjectModel;using System.Collections.Specialized;public class Test{    static void Main()    {        var data = new ObservableCollection<string>();        data.CollectionChanged += Data_CollectionChanged;        data.Add("One");        data.Add("Two");        data.Insert(1, "Three");        data.Remove("One");    }    static void Data_CollectionChanged(object sender,NotifyCollectionChangedEventArgs e)    {        Console.WriteLine("action;{0}", e.Action.ToString());        if(e.OldItems != null)        {            Console.WriteLine("starting index for old item(s):{0}", e.OldStartingIndex);            Console.WriteLine("old item(s):");            foreach(var item in e.OldItems)            {                Console.WriteLine(item);            }        }        if(e.NewItems != null)        {            Console.WriteLine("starting index for new item(s):{0}", e.NewStartingIndex);            Console.WriteLine("new item(s): ");            foreach(var item in e.NewItems)            {                Console.WriteLine(item);            }        }        Console.WriteLine();    }}

Data_CollectionChanged()方法接收NotifyCollectionChangedEventArgs,其中包含了集合的变化信息。Action属性给出了是否添加或删除一项的信息。对于删除的项,会设置OldItems属性,列出删除的项。对于添加的项,则设置NewItem属性,列出新增的项。

0 0
原创粉丝点击