2015-11-23 67 views
0

如何将hashset的值写入控制台?我不想使用foreach循环。如何将Hashset值写入控制台?

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

输出

System.Linq.Enumerable 

预计输出

员工,Employee1

回答

5

您可以使用String.Join

Console.WriteLine(String.Join(",", hashSet)); 
0

试试这个:

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

这将遍历HashSet的,并在每个项目

+0

为什么你需要'停止console'打电话Console.WriteLine? –

+0

当控制台运行时,您可以停止执行并查看输出,可以使用“Console.ReadLine();”当你按下一个按钮执行重新启动。但是,如果这个话题。解决你的问题没有必要。 – daniele3004