How to display list items on console window in C#

后端 未结 7 1344
执笔经年
执笔经年 2020-11-29 00:19

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()). Ho

7条回答
  •  被撕碎了的回忆
    2020-11-29 00:43

    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 and pick the most specific method from the group, in this case Console.WriteLine(int):

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

提交回复
热议问题