Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use set of C #

Shulou Source: shulou.com Published: 2022-05-31 23:37:13 10月02日 Update

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.

Tags: Elements clips articles methods validation ordering intersection content interface collection appropriateness two information variables subsets ideas new knowledge more steps knowledge Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn Xiaomi Shulou Information Apple Shulou Technology