Combining two collections with no duplicates - CSharp Collection

CSharp examples for Collection:List

Description

Combining two collections with no duplicates

Demo Code

using System;//w  w  w .  ja v  a2 s.c o  m
using System.Collections.Generic;
class Program
{
   static void Main(string[] args)
   {
      List<string> colors = new List<string> { "red", "orange", "yellow" };
      string[] moreColors = { "orange", "yellow", "green", "blue", "violet" };
      HashSet<string> combined = new HashSet<string>(colors);
      combined.UnionWith(moreColors);
      foreach (string color in combined)
      {
         Console.WriteLine(color);
      }
   }
}

Result


Related Tutorials