1.

UnionWith() method in C#

Answer»

The UnionWith() method is used to get the union of two collections. This MEANS that all the elements in both the collections are displayed without any duplicates using the UnionWith() method.

A program that demonstrates the UnionWith() method using two DICTIONARIES is given as follows:

using System; using System.Collections.Generic; public class Program {   public static void Main()   {      Dictionary < string, int > D1 = new Dictionary < string, int > ();      d1.Add("Apple", 1);      d1.Add("MANGO", 2);      d1.Add("Guava", 3);      Dictionary < string, int > d2 = new Dictionary < string, int > ();      d2.Add("Peach", 4);      d2.Add("Apple", 5);      HashSet < string > h = new HashSet  <string > (d1.Keys);      h.UnionWith(d2.Keys);      Console.WriteLine("The union of two Dictionaries is:");      foreach(string i in h)      {         Console.WriteLine(i);      }   } }

The output of the above program is as follows:

The union of two Dictionaries is: Apple Mango Guava Peach


Discussion

No Comment Found