I want to create an class that represents an integer set using a HashSet<int>
. I want it to keep track of which values are included in the set using that internal container. I've done this so far:
class SetInteger
{
HashSet<int> intTest= new HashSet<int>();
intTest.Add(1);
intTest.Add(2);
intTest.Add(3);
intTest.Add(4);
intTest.Add(5);
intTest.Add(6);
intTest.Add(7);
intTest.Add(8);
intTest.Add(9);
intTest.Add(10);
}
So, here I think I'm adding some values to the HashSet
, but I dont see how this can keep track of which values that are included in the set. Any ideas?
you can try this. you just take a one textbox and two button.
if you again problem faced just drop your code .....
Hmm...well, a
HashSet<T>
implementsIEnumerable<T>
, so you can always do this to figure out "What's already in there":There's also
bool Contains(T value)
which will tell you if a specific value is in the set,IEnumerable<T> Union(IEnumerable<T> other)
which will tell you the "OR" of two sets,IEnumerable<T> Intersect(IEnumerable<T> other)
which will tell you the overlap of two sets...pretty much anything in eitherIEnumerable<T>
orISet<T>
The hash set has a
Contains
method that allows you to check whether a value is in the set.In addition, the
HashSet<T>
implements theISet<T>
interface and therefore provides many methods for working with sets, such as union, intersection and determining if a set of values is a (proper) super- or subset of your set.By the way, did you know about the collection initializer syntax?
You can also
foreach
on the set to get each element it contains (in an unspecified order):Or convert it to an array or mutable list (also in an unspecified order):
You can use HashSet
Contains
method tell's if the value already exists!Example :
Us the Contains method: http://msdn.microsoft.com/en-us/library/bb356440.aspx
Hope this helps.