c# - ForEach extension method -


i'm bit confused on line of code:

enumerable.range(1,10).tolist().foreach(console.writeline); 

this line says does, prints out each number 1 10 line break inbetween each of them, 1 neat little line of code..

now i'm c# novice @ best, looks foreign me, how can call console.writeline() without providing arguments? how know it's going print? syntax it's not clear me we're calling on method (considering lack of paranthesis around writeline).

i'm assuming there's lot of stuff going on "behind scenes" here i'm not @ reading il code, i've gathered reading msil seems foreach calls generic delegate(system.action) on each element in collection, guessing passes element argument system.action delegate that's guess..

there implicit conversion method group (console.writeline in case) , compatible delegate type. code same as

action<int> f = console.writeline; enumerable.range(1,10).tolist().foreach(f); 

the delegate f compatible method void console.writeline(int)


Comments

Popular posts from this blog

c++ - How to add Crypto++ library to Qt project -

jQuery Mobile app not scrolling in Firefox -

How to use vim as editor in Matlab GUI -