Friday 28 April 2017

Can i have inner execution code inside lambda expression in C#?

 

  • Yes, we can have. The following code snippet will tell you how we can achieve the same easily.
  • This technique also known as action definition for lambda expression. 
  • n is actual item of collection, i is optional and is used for Index started with 0.

   

        public static void innerFun()
        {
            List<int> nums = new List<int>() { 1, 2, 3, 4, 10 };
            int multiply = 2;
            var newnums = nums.Select((n, i) => {
                var result = n * multiply;
                Console.WriteLine(i.ToString() + " position - " + result);
                return result;
            });
            Console.WriteLine("-----------------------------\nnewnum count: " + newnums.Count());
        }