C # set ),

Source: Internet
Author: User

C # set ),

A set that contains non-repeating elements is called "set )".. NET Framework contains two sets: HashSet <T> and SortedSet <T>. Both of them implement the ISet <T> interface. The HashSet <T> set contains an unordered list of non-repeating elements. The SortedSet <T> set contains an ordered list of non-repeating elements.
The ISet <T> interface provides methods for creating a collection or intersection, or providing information about a superset or a subset of another set.
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 verify that every element in traditionalTeams is included in companyTeams
If (traditionalTeams. IsSubsetOf (companyTeams ))
{
Console. WriteLine ("traditionalTeams is subset of companyTeams ");
}
IsSupersetOf verifies that traditionalTeams contains elements not included in companyTeams
If (companyTeams. IsSupersetOf (traditionalTeams ))
{
Console. WriteLine ("companyTeams is a superset of traditionalTeams ");
}
Overlaps verify whether 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 fill the new SortedSet <string> variable with a collection of companyTeams, privateTeams, and 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 );
}

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 deletes all private elements from ExceptWith.
AllTeams. receivtwith (privateTeams );
Console. WriteLine ();
Console. WriteLine ("no private team left ");
Foreach (var team in allTeams)
{
Console. WriteLine (team );
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.