1

How do I write the values of a hashset to the console? I do not want to use a foreach loop.

HashSet<string> hashSet=new HashSet<string>();
hashSet.Add("Employee");
hashSet.Add("Employee1");
Console.WriteLine(hashSet.Select(x=>x));

Output

System.Linq.Enumerable

Expected output

Employee,Employee1

Chawin
  • 1,396
  • 1
  • 20
  • 32
khan
  • 117
  • 1
  • 4
  • 11

2 Answers2

10

You can use String.Join:

Console.WriteLine(String.Join(",", hashSet));
Tim Schmelter
  • 429,027
  • 67
  • 649
  • 891
1

Try this:

hashSet.ToList<String>().ForEach(x => Console.WriteLine(x));

This will iterate over the hashSet and call Console.WriteLine on each item

Alex
  • 35,969
  • 49
  • 197
  • 322
daniele3004
  • 11,984
  • 10
  • 61
  • 68
  • Why do you need to `stop console`? – Thomas Ayoub Nov 23 '15 at 10:39
  • When console run you can stop the execution and view the output can use "Console.ReadLine();" and when you press a button execute restart. But this if of topic. Is not necessary for solve your problem. – daniele3004 Nov 23 '15 at 10:42