11.6. Using a HashSet Object
Problem
You need an object that contains a group of unique unordered objects. This object must be able to be compared to other objects containing unique unordered groups of similar objects. In addition, the two must be able to have the following actions performed on them:
Union of the items contained by the two container objects
Intersection of the items contained by the two container objects
Difference of the items contained by the two container objects
Solution
Use the built in HashSet<T>
object.
The methods defined in Table 11-8 are of particular interest to using a HashSet<T>
object.
Table 11-8. Members of the HashSet<T> class
Member | Description |
---|---|
| Add a new object to the current Add(T
where |
| Removes an existing object from the current Remove(T
where |
| Removes an existing object from the current RemoveWhere(Predicate<T>
where |
| Returns a Boolean indicating whether the object passed in exists within this Contains(T
where |
|
Get C# 3.0 Cookbook, 3rd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.