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,438
  • 1
  • 21
  • 33
khan
  • 127
  • 1
  • 4
  • 11

2 Answers2

11

You can use String.Join:

Console.WriteLine(String.Join(",", hashSet));
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
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
  • 37,502
  • 51
  • 204
  • 332
daniele3004
  • 13,072
  • 12
  • 67
  • 75
  • 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