I am trying to print numbers from 1 to 1000 (inclusive of 1000).
for (int i = 1; i <= 1000; i=i+1)
{
Console.WriteLine(i);
}
But, I do remember a single line of code that I myself used before. Like below:
Enumerable.TheMethodGives1To1000(Console.WriteLine);
Any ideas ?
What you need is Enumerable.Range
method which generates a sequence of integral numbers within a specified range. It returns IEnumerable<int>
object.
And for printing the elements in this collection we can use List<T>.ForEach
method. It performs the specified action on each element of the List<T>
. And in case of single argument you can pass function by itself.
So, the result is:
Enumerable.Range(1, 1000)
.ToList()
.ForEach(Console.WriteLine);
foreach(var i in Enumerable.Range(1, 999))
Console.WriteLine(i);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With