using System.Collections.Generic; HashSetset = new HashSet (); set.Add(5); set.Add(3); set.Add(7); set.Add(5); // This will not be added since 5 already exists in the set // The set now contains 5, 3, and 7
using System.Collections.Generic; HashSetThis code also uses the HashSet class from the System.Collections.Generic package/library. Both examples demonstrate how the Set Add method can be used to add elements to a set in C#. The package/library used is System.Collections.Generic, which provides several collection classes for managing groups of objects.set = new HashSet (); set.Add("apple"); set.Add("banana"); set.Add("orange"); set.Add("apple"); // This will not be added since "apple" already exists in the set // The set now contains "apple", "banana", and "orange"