Lambda Expressions in C#

CsharpProgrammingServer Side Programming

A lambda expression in C# describes a pattern.

Lambda Expressions has the token => in an expression context. This is read as “goes to” operator and used when a lambda expression is declared.

Here, we are finding the first occurrence of the element greater than 50 from a list.

list.FindIndex(x => x > 50);

Above the token => is used. The same is shown below −

Example

 Live Demo

using System;
using System.Collections.Generic;

class Demo {
   static void Main() {
      List<int> list = new List<int> { 44, 6, 34, 23, 78 };

      int res = list.FindIndex(x => x > 50);
      Console.WriteLine("Index: "+res);
   }
}

Output

Index: 4


raja
Published on 20-Aug-2018 13:34:47
Advertisements