Lambda Expressions



Lambda Expressions in C#

We can use a lambda expression to create an anonymous function. Lambda expression is an anonymous function that we can use to create delegates.

Any lambda expression can be converted to a delegate type. The delegate type to which a lambda expression can be converted is defined by the types of its parameters and return value. We can use the lambda expression to create local functions that can be passed as an argument.

It is also helpful to write LINQ queries.

We use the lambda declaration operator (=>) to separate the lambda's parameter list from its body.

Syntax:
(input-parameters) => expression  

Example: 

using System;  
namespace LambdaExpressions  
{  
    class LambdaExpressionClass
    {  
        delegate int Cube(int n);  
        static void Main(string[] args)  
        {  
            Cube GetCube = x => x * x * x;  
            int result = GetCube(3);    
            Console.WriteLine("Cube: "+result);  
        }  
    }  
}  

Output:

Cube: 27

0 Comment's

Comment Form

Submit Comment