fork download
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. class HashSet
  5. {
  6. static void Main()
  7. {
  8. HashSet<String> cars = new HashSet<String>();
  9. HashSet<String> trucks = new HashSet<String>();
  10.  
  11. cars.Add ("Tesla M3");
  12. cars.Add ("Ford Mustang");
  13. cars.Add ("Chevy Camaro");
  14.  
  15. cars.Add ("Tesla M3"); // error, can't add Musta again
  16.  
  17. trucks.Add ("Ford F150");
  18. trucks.Add ("Chevy Siverado");
  19. trucks.Add ("Dodge Ram");
  20.  
  21. Console.Write ("cars contains {0} elements: ", cars.Count);
  22. DisplaySet (cars);
  23.  
  24. Console.Write("trucks contains {0} elements: ", trucks.Count);
  25. DisplaySet (trucks);
  26.  
  27. // Create a new HashSet with both cars and trucks
  28. HashSet<String> allClassMembers = new HashSet<String>(cars);
  29. Console.WriteLine ("allClassMembers UnionWith trucks ...");
  30. allClassMembers.UnionWith (trucks);
  31.  
  32. Console.Write ("allClassMembers contains {0} elements: ", allClassMembers.Count);
  33. DisplaySet (allClassMembers);
  34.  
  35. }
  36.  
  37. private static void DisplaySet (HashSet<String> set)
  38. {
  39. Console.Write ("{");
  40. foreach (String i in set)
  41. {
  42. Console.Write (" {0}", i);
  43. }
  44. Console.WriteLine (" }");
  45. }
  46. } // HashSet
Success #stdin #stdout 0.03s 26660KB
stdin
Standard input is empty
stdout
cars contains 3 elements: { Tesla M3 Ford Mustang Chevy Camaro }
trucks contains 3 elements: { Ford F150 Chevy Siverado Dodge Ram }
allClassMembers UnionWith trucks ...
allClassMembers contains 6 elements: { Tesla M3 Ford Mustang Chevy Camaro Ford F150 Chevy Siverado Dodge Ram }