Attemping to add a value to a HashSet does not change the number of values โ€‹โ€‹in it

I have one HashSet, and when I use the Addcollection method nothing is added. The output is still 2, 3, 5, 7, 11, 13, and the output from .Countis 6.

Is this a mistake or am I doing something wrong here?

namespace AllerDiz
{
    class MainClass
        {
            public static void Main (string[] args)
            {
                HashSet<int> smallPrimeNumbers = new HashSet<int> { 2, 3, 5, 7, 11, 13 };
                smallPrimeNumbers.Add (3);
                smallPrimeNumbers.Add (5);
                smallPrimeNumbers.Add (7);
                Console.WriteLine ("{0}", smallPrimeNumbers.Count);
                foreach(int val in smallPrimeNumbers)
                {
                    Console.WriteLine ("HashSet Value= {0}", val);
                }
            }
      }
}
+4
source share
3 answers

No, this is not a mistake. That is what is supposed to be HashSet.

The class HashSet<T>provides high-performance operations with many. A set is a set that does not contain repeating elements and whose elements do not have a special order.

, , , , .

, , List<T>.

+18

HashSet<T> Class

HashSet . , .

HashSet<T>.Add Method

true, HashSet; false, .

HashSet - . .

:

string[] array = new string[] {"one", "one", "two", "two", "three", "three"};
HashSet<string> hash = new HashSet<string>(array);
string[] array2 = hash.ToArray();

array2 {"one", "two", "three"}

, List<int>, .

+4

HashSet

0

All Articles