Home > DeveloperSection > Blogs > Lambda Expression in c#

Lambda Expression in c#


.NET C#  .NET 
Ratings:
0 Comment(s)
 792  View(s)
Rate this:

Lambda Expression in c#

 

In this blog, I’m explaining about the lambda expression in C#.

 

Lambda expressions are anonymous methods, aimed at mainly addressing the "vertical problem" by replacing the machinery of anonymous inner classes with a lighter-weight mechanism in application cases.


 All lambda expressions use the lambda operator =>, which is read as “goes to”. The left side of the lambda operator specifies the input parameters (if any) and the right side holds the expression or statement block. The lambda expression x => x * x is read “x goes to x times x.

(The => operator has the same precedence as assignment (=))

A lambda expression is written in the following form

parameters => expression/{statements}

Lambda expressions in C#

Example

 Namespace LamdaExpression

{

      Class Program

             {

                        Static void Main(String[] args)

                              {

                                   int input;

                                       input=Convert.toInt32(System.ReadLine());

                                             Func<int,int,bool>  prime=null;

                                               Prime=(x,y)=>(x=1 || y=0)?true :(x%y==0?false:prime(x,y-1);

                                             Func<int,bool> isprime=x=>prime(x,x/2);

                                         Console.Write(“{0} is a {1} prime number”, input,  isprime(input)?””:”not”)

                              }

                     }

         }

                 


Don't want to miss updates? Please click the below button!

Follow MindStick