I am trying learn the use of lambda expressions and hence still struggling to implement after even reading the documentation and other various related articles on it.
So if I want to convert this following loop into a lambda expression then how would I go about doing it, I just need an approach to look how lambda expressions work.
Code:
var pc = Enumerable.Range(2, 100).ToList();
var j = 0;
while (j < pc.Count)
{
Console.WriteLine(pc[j]);
j++;
}
Step by step explanation
Enumerable.Range makes a
IEnumarable<int>starting from 2 having length 100. ie. items will be 2,3,4…..101..ToList()converts thatIEnumerabletoList. Why converting? so that we can useForEachmethod ofList.Most Important for you
ForEach(). As the name suggests it performs action on each item in list. In here each element of list is taken and put in a runtime variable p which ‘goes to’=> Console.WriteLine(p)to write on console.