如何显示在C#在控制台窗口列表项 [英] How to display list items on console window in C#
问题描述
我有一个列表
它包含了所有的数据库名称。我有(使用 Console.WriteLine()
),以dispaly包含在控制台的列表中的项目。我怎样才能做到这一点?
I have a List
which contains all databases names. I have to dispaly the items contained in that list in the Console (using Console.WriteLine()
). How can I achieve this?
推荐答案
其实你可以做pretty简单,因为列表中有一个的ForEach
方法和自您可以在 Console.WriteLine
作为传递的方法组。然后该编译器将使用的隐式转换的方法组转换,在这种情况下,一个动作及所述; INT>
和选自挑最具体的方法,在这种情况下`Console.WriteLine(INT):
Actually you can do it pretty simple, since the list have a ForEach
method and since you can pass in Console.WriteLine
as a method group. The compiler will then use an implicit conversion to convert the method group to, in this case, an Action<int>
and pick the most specific method from the group, in this case `Console.WriteLine(int):
var list = new List<int>(Enumerable.Range(0, 50));
list.ForEach(Console.WriteLine);
用绳子作品太=)
Works with strings too =)
要能尽迂腐(我不建议更改你的答案 - 只是评论感兴趣的缘故)Console.WriteLine是方法组。那么编译器使用从方法组到行动的隐式转换,采摘最具体的方法(Console.WriteLine(INT)在这种情况下)。
To be utterly pedantic (and I'm not suggesting a change to your answer - just commenting for the sake of interest) "Console.WriteLine" is a method group. The compiler then uses an implicit conversion from the method group to Action, picking the most specific method (Console.WriteLine(int) in this case).
这篇关于如何显示在C#在控制台窗口列表项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!