Advertisement
n4wn4w

C# SETS

Apr 28th, 2015
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.56 KB | None | 0 0
  1. static void Main()
  2.     {
  3.         Console.WriteLine("HashSet<T>");
  4.         Console.WriteLine("----------");
  5.         ISet<string> firstSet = new HashSet<string>() { "Perl", "Java", "C#", "SQL", "PHP" };
  6.         ISet<string> secondSet = new HashSet<string>() { "Oracle", "SQL", "MySQL" ,"Za" ,"AZ" };
  7.         DisplayUnionIntersect(secondSet ,firstSet);
  8.  
  9.         Console.WriteLine();
  10.         Console.WriteLine("SortedSet<T>");
  11.         Console.WriteLine("------------");
  12.         firstSet = new SortedSet<string>() { "Perl", "Java", "C#", "PHP", "SQL" };
  13.         secondSet = new SortedSet<string>() { "Oracle", "SQL", "MySQL","Z","ZaZaZaZaZa" };
  14.         DisplayUnionIntersect(firstSet, secondSet);
  15.     }
  16.  
  17.     private static void DisplayUnionIntersect(ISet<string> firstSet, ISet<string> secondSet)
  18.     {
  19.         Console.Write("First set: ");
  20.         PrintSet(firstSet);
  21.  
  22.         Console.Write("Second set: ");
  23.         PrintSet(secondSet);
  24.  
  25.         ISet<string> union = new HashSet<string>(firstSet);
  26.         union.UnionWith(secondSet);
  27.         Console.Write("Union: ");
  28.         PrintSet(union);
  29.  
  30.         ISet<string> intersect = new HashSet<string>(firstSet);
  31.         intersect.IntersectWith(secondSet);
  32.         Console.Write("Intersect: ");
  33.         PrintSet(intersect);
  34.     }
  35.  
  36.     private static void PrintSet<T>(ISet<T> set)
  37.     {
  38.         foreach (var element in set)
  39.         {
  40.             Console.Write("{0} ", element);
  41.         }
  42.         Console.WriteLine();
  43.     }
  44.  
  45.  
  46.  
  47. //////////////////////////////////////////////////////////////////////////////////
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement