HashSet C# Help

.NET 3.5 includes a new collection class in the System. Collections. Generic namespace: HashSet<T>. This collection class contains an unordered list of distinct items. Such a collection is known by the term set. Because set is a reserved word, the class has a different name: HashSet<T>. The name was easily decided because this collection is based on hash values; inserting elements is fast. There’s no need to rearrange the collection as is necessary with the List<T> class.

The HashSet<T> class offers methods to create a union, an intersection of sets. The following table describes the methods that change the values of the set.

The next table lists the methods that just return information about the set without changing the elements.

With the sample code, three new sets of type string are created and filled with Formula-l cars. The HashSet<T> class implements the ICollection<T> interface. However, the Add () method is implemented explicitly and a different Add () method is offered by the class as you can see here. The Add () method differs by the return type; a Boolean value is returned to give the information if the
element was added. If the element was already in the set, it is not added, and false is returned.

The result of these two Add () methods is written to the console:

Williams added
McLaren. was already in this set

The methods IsSubsetOf () and IsSupersetOf () compare a set with a collection that implements the IEnurnerable<T> interface and returns a Boolean result. Here, IsSubsetOf () verifies if every element in tradi tional Teams is contained in companyTeams, which is the case; IsSupersetOf () verifies if tradi tional Teams does not have any additional element compared to companyTeams.

The output of this verification is shown here:

traditionalTeams is a subset of companyTeams
companyTeams is a superset of traditional Teams

Williams isa traditional team as well, and that’swhy this team isadded to the tradi tional Teams collection:

traditionalTeams .Add (·Williams·) ;
if (privateTeams.Overlaps(traditionalTeams))
Console.WriteLine{·At least one team i~ • +
·the same with the traditional • +
·and private teams·);


Because there’s an overlap, this is the result:

At least one team is the same with the traditional and private teams.

The variable all Teams is filled with a union of companyTeams, privateTeams, and traditional Teams by calling the UnionWith () method:’

HashSet<string> allTeams =
new HashSet<string>{companyTeams);
.~ Console.writeLine(·all teams·);
foreach (var team in all Teams)


Here all teams are returned, but every team is listed just once because the set contains only unique values:

Red Bull
Toro Rosso
Super Aguri

The method ExceptWith () removes all private teams from the all Teams set:

Console.WriteLine{·no private team left·);
~oreach (var team in all Teams)


The remaining elements in the collection do not contain any private team:


Posted on October 29, 2015 in Collections

Share the Story

Back to Top
Share This