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).
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…