Guest User

Untitled

a guest
Nov 21st, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.21 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace EgoDelegate
  5. {
  6. public static class HashSetExtension
  7. {
  8. public static void DisplaySet(this HashSet<int> set)
  9. {
  10. Console.Write("{");
  11. foreach( int i in set)
  12. {
  13. Console.Write(" {0}", i);
  14. }
  15. Console.WriteLine(" }");
  16. }
  17.  
  18. public static void ForEach<T>(this IEnumerable<T> source, Action<T> action)
  19. {
  20. foreach (T element in source)
  21. {
  22. action(element);
  23. }
  24. }
  25. }
  26.  
  27.  
  28. class Damn
  29. {
  30. static void Main()
  31. {
  32. HashSet<int> evenNumbers = new HashSet<int>();
  33. HashSet<int> oddNumbers = new HashSet<int>();
  34.  
  35. for (int i = 0; i < 5; i++)
  36. {
  37. evenNumbers.Add(i * 2);
  38. oddNumbers.Add((i * 2) + 1);
  39. }
  40.  
  41. evenNumbers.DisplaySet();
  42. oddNumbers.DisplaySet();
  43.  
  44. // union of two sets
  45. HashSet<int> uniformedSet = new HashSet<int>(evenNumbers);
  46. uniformedSet.UnionWith(oddNumbers);
  47. uniformedSet.ForEach(x => Console.Write(x + " "));
  48.  
  49. }
  50. }
  51. }
Add Comment
Please, Sign In to add comment