Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use set of C #

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article is a detailed introduction to "how to use C#set" for everyone. The content is detailed, the steps are clear, and the details are properly handled. I hope this article "how to use C#set" can help you solve your doubts. Let's go deeper and learn new knowledge together with the ideas of Xiaobian.

A collection containing non-repeating elements is called a "set." The. NET Framework contains two sets, HashSet and SortedSet, both of which implement the ISet interface. A HashSet set contains an unordered list of non-repeating elements, and a SortedSet contains an ordered list of non-repeating elements.

The ISet interface provides methods to create collections, intersections, or information that one is a superset or subset of another.

var companyTeams = new HashSet() { "Ferrari", "McLaren", "Mercedes" }; var traditionalTeams = new HashSet() { "Ferrari", "McLaren" }; var privateTeams = new HashSet() { "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 verifies that every element in traditionalTeams is included in companyTeams

if (traditionalTeams.IsSubsetOf(companyTeams)) { Console.WriteLine("traditionalTeams is subset of companyTeams"); }

IsSupersetOf verifies that there are elements in traditionalTeams that are not in companyTeams

if (companyTeams.IsSupersetOf(traditionalTeams)) { Console.WriteLine("companyTeams is a superset of traditionalTeams"); }

Overlaps verify if there is an intersection

traditionalTeams.Add("Williams"); if (privateTeams.Overlaps(traditionalTeams)) { Console.WriteLine("At least one team is the same with the traditional " + "and private teams"); }

Call the UnionWith method to populate the new SortedSet variable with companyTeams, privateTeams, traditionalTeams collections

var allTeams = new SortedSet(companyTeams); allTeams.UnionWith(privateTeams); allTeams.UnionWith(traditionalTeams); Console.WriteLine(); Console.WriteLine("all teams"); foreach (var team in allTeams) { Console.WriteLine(team); }

Output (ordered):

Ferrari Force India Lotus McLaren Mercedes Red Bull Sauber Toro Rosso Williams

Each element is listed only once because the set contains only unique values.

The ExceptWith method removes all private elements from ExceptWith

allTeams.ExceptWith(privateTeams); Console.WriteLine(); Console.WriteLine("no private team left"); foreach (var team in allTeams) { Console.WriteLine(team); } Read here, this article "C#set how to use" article has been introduced, want to master the knowledge points of this article also need to practice to understand, if you want to know more related content of the article, welcome to pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 265

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report