HashSetset1 = new HashSet (); set1.Add(1); set1.Add(2); set1.Add(3); HashSet set2 = new HashSet (); set2.AddRange(set1); foreach (int i in set2) Console.WriteLine(i);
using System.Collections.Generic; ListIn this example, we have created a List of strings and added three elements (C#, Java, and Python) to it. We have then created a HashSet of strings and added all the elements of the list to it using the AddRange method. Finally, we have printed all the elements of the HashSet using a foreach loop. Package Library: System.Core.dll.list = new List (); list.Add("C#"); list.Add("Java"); list.Add("Python"); HashSet set = new HashSet (); set.AddRange(list); foreach (string s in set) Console.WriteLine(s);