Lambda Expressions in C#


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


karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 22-Jun-2020

377 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements