C#集合之集(set)的用法
包含不重復(fù)元素的集合稱(chēng)為“集(set)”。.NET Framework包含兩個(gè)集HashSet<T>和SortedSet<T>,它們都實(shí)現(xiàn)ISet<T>接口。HashSet<T>集包含不重復(fù)元素的無(wú)序列表,SortedSet<T>集包含不重復(fù)元素的有序列表。
ISet<T>接口提供的方法可以創(chuàng)建合集,交集,或者給出一個(gè)是另一個(gè)集的超集或子集的信息。
var companyTeams = new HashSet<string>() { "Ferrari", "McLaren", "Mercedes" }; var traditionalTeams = new HashSet<string>() { "Ferrari", "McLaren" }; var privateTeams = new HashSet<string>() { "Red Bull", "Lotus", "Toro Rosso", "Force India", "Sauber" }; if (privateTeams.Add("Williams")) Console.WriteLine("Williams added"); if (!companyTeams.Add("McLaren")) Console.WriteLine("McLaren was already in this set");
IsSubsetOf驗(yàn)證traditionalTeams中的每個(gè)元素是否都包含在companyTeams中
if (traditionalTeams.IsSubsetOf(companyTeams)) { Console.WriteLine("traditionalTeams is subset of companyTeams"); }
IsSupersetOf驗(yàn)證traditionalTeams中是否有companyTeams中沒(méi)有的元素
if (companyTeams.IsSupersetOf(traditionalTeams)) { Console.WriteLine("companyTeams is a superset of traditionalTeams"); }
Overlaps驗(yàn)證是否有交集
traditionalTeams.Add("Williams"); if (privateTeams.Overlaps(traditionalTeams)) { Console.WriteLine("At least one team is the same with the traditional " + "and private teams"); }
調(diào)用UnionWith方法把新的 SortedSet<string>變量填充為companyTeams,privateTeams,traditionalTeams的合集
var allTeams = new SortedSet<string>(companyTeams); allTeams.UnionWith(privateTeams); allTeams.UnionWith(traditionalTeams); Console.WriteLine(); Console.WriteLine("all teams"); foreach (var team in allTeams) { Console.WriteLine(team); }
輸出(有序的):
Ferrari Force India Lotus McLaren Mercedes Red Bull Sauber Toro Rosso Williams
每個(gè)元素只列出一次,因?yàn)榧话ㄒ恢怠?br />ExceptWith方法從ExceptWith中刪除所有私有元素
allTeams.ExceptWith(privateTeams); Console.WriteLine(); Console.WriteLine("no private team left"); foreach (var team in allTeams) { Console.WriteLine(team); }
到此這篇關(guān)于C#集合之集(set)的文章就介紹到這了。希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C# 實(shí)現(xiàn)Distinct將對(duì)象按條件去重
這篇文章主要介紹了C# 實(shí)現(xiàn)Distinct將對(duì)象按條件去重,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-12-12C#實(shí)現(xiàn)圖表中鼠標(biāo)移動(dòng)并顯示數(shù)據(jù)
這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)圖表中鼠標(biāo)移動(dòng)并顯示數(shù)據(jù),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02C# 使用鼠標(biāo)點(diǎn)擊對(duì)Chart控件實(shí)現(xiàn)數(shù)據(jù)提示效果
這篇文章主要介紹了C# 使用鼠標(biāo)點(diǎn)擊對(duì)Chart控件實(shí)現(xiàn)數(shù)據(jù)提示效果,文章給予上一篇的詳細(xì)內(nèi)容做延伸介紹,需要的小伙伴可任意參考一下2022-08-08asp.net core 使用 tensorflowjs實(shí)現(xiàn) face recognition的源代碼
tensorflowjs,在該項(xiàng)目中使用了ml5js這個(gè)封裝過(guò)的機(jī)器學(xué)習(xí)JavaScript類(lèi)庫(kù), 使用起來(lái)更簡(jiǎn)單,本文給大家分享asp.net core 使用 tensorflowjs實(shí)現(xiàn) face recognition的源代碼,需要的朋友參考下吧2021-06-06c# 網(wǎng)址壓縮簡(jiǎn)單實(shí)現(xiàn)短網(wǎng)址
短網(wǎng)址,忽然一下子就冒出來(lái)的東西,長(zhǎng)長(zhǎng)的一個(gè)URL,提交過(guò)去,出來(lái)就只有短短的一個(gè)URL了,看起來(lái)似乎挺神奇,其實(shí)簡(jiǎn)單分析一下,明白其中的原理,也是一件很簡(jiǎn)單的事情,需要的朋友可以了解下2012-12-12C# 使用 OleDbConnection 連接讀取Excel的方法
這篇文章主要介紹了C# 使用 OleDbConnection 連接讀取Excel的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12