How to use Lamda expression instead of If Condition in C#


Normal IF Condition
protected void Page_Load(object sender, EventArgs e)
    {
        int id = 1;
        int k=2;
        int j = 3;
      
         if ( id  > 2)
        {
            Response.Write(k);
        }
        else
        {
            Response.Write(j);
        }
    }

Lamda Expression
protected void Page_Load(object sender, EventArgs e)
    {
        int id = 1;
        int l;
        int k=2;
        int j = 3;
       
        // Here my if condition   (?)  it is true execute k , False execute j.  The value store into variable l.
        l = (id > 2) ? k : j;

        Response.Write(l);
    }

No comments:

Post a Comment