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 −


 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);


Index: 4

Updated on: 22-Jun-2020


Kickstart Your Career

Get certified by completing the course

Get Started