Hashset<t> class

Source: Internet
Author: User

The Hashset<t> class is designed to perform high-performance set operations such as intersection, set, and difference set for two sets. The collection contains a set of elements that do not recur and have no attribute order.

Some of the features of hashset<t> are as follows:

1. Values in hashset<t> cannot be duplicated and have no order.

2, hashset<t> capacity will be automatically added on demand.

Construction Method:

The HashSet () default equality comparer creates an empty new instance.

HashSet (ienumerable<t> Collection) copies the data from the collection in the specified collection to the set

HashSet (iequalitycomparer<t> comparer) creates an empty new instance with the specified equality comparer

HashSet (ienumerable<t> collection,iequalitycomparer<t> comparer) instantiates the data using the specified comparer and copies the elements from the specified collection into the collection.

Because hashset<t> is specifically designed to do set operations, many of the methods it provides are related to set operations.

Here are some common ways to introduce it

Member Type description

The Add method adds the specified element to the collection

Clear method empties all elements in the collection

Contains method determines whether an element is in hashset<t>

The Exists method determines whether the hashset<t> contains elements that match the specified criteria

Exceptwith method removes all elements from the specified collection from the current hashset<t>

The Intersectwith method modifies the current Hashset<t> object to include only that object and the elements that exist in the specified collection

The Ispropersubsetof method determines whether the Hashset<t> object is a true subset of the specified collection

The IsProperSupersetOf method determines whether the Hashset<t> object is a true superset of the specified collection

The Issunsetof method determines whether the Hashset<t> object is a subset of the specified collection

The Issupersetof method determines whether the Hashset<t> object is a superset of the specified collection

The Remove method removes the specified element from the Hashset<t> object

The Removewhere method removes all elements that match the conditions defined by the specified predicate from the Hashset<t> collection

The Setequals method determines whether the Hashset<t> object contains the same elements as the specified collection

The Synmmetricexceptwith method modifies the current Hashset<t> object to contain only the elements that exist in the object or in the specified collection

The TrimExcess method sets the capacity of the Hashset<t> object to the actual number of elements it contains, rounded up to the close attribute and the implemented value.

The Unionwith method modifies the current Hashset<t> object to contain the object itself and all elements that exist in the specified collection

Give a simple example, can not finish, in short, remember the main role of hashset<t> is used to carry out, intersection, and set the operation of the OK.

        Static voidMain (string[] args) {HashSet<string> HS =Newhashset<string>(); Hs. ADD ("You're"); Hs. ADD ("Good"); Hs. ADD ("?"); HashSet<string> HS1 =Newhashset<string>(); HS1. ADD ("You're"); HS1. ADD ("Good"); BOOLb = HS1. Ispropersubsetof (HS);//determine if HS1 is a true subset of HSConsole.WriteLine (b);//Output TrueHashSet<string> HS2 =Newhashset<string>(); HS2. ADD ("Love You"); IEnumerable<string> list = hs. Union (HS2);//returns the Set            foreach(stringStrinchlist)     {Console.WriteLine (str); //output Hello, love you.} console.readkey (); }

Output:

Hashset<t> class

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.