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?
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 =)
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<int>
, picking the most specific method (Console.WriteLine(int)
in this case).