Understanding Lambda Expressions in C#

Introduction

Today, in this article let's play around with one of the interesting and most useful concept in C#.

Question: What are lambda expressions?

In simple terms "It is an anonymous function which accepts some input parameters and performs an expected operation with lambda operator."

Step 1: Create a new webform project

pic1.png

Step 2: The complete code of webform1.aspx looks like this

  1.  <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="LamdaExpressionApp.WebForm1" %>  
  2. <!DOCTYPE html>  
  3. <html xmlns="http://www.w3.org/1999/xhtml">  
  4. <head id="Head2" runat="server">  
  5.     <title></title>  
  6. </head>  
  7. <body>  
  8.     <form id="form1" runat="server">  
  9.     <center>  
  10.         <div>  
  11.             <table>  
  12.                 <tr>  
  13.                     <td colspan="2" align="center">  
  14.                         <asp:Label ID="Label3" runat="server" Text="Arithmetic Operations with Lambda Expressions in C#"  
  15.                             Font-Bold="true" Font-Size="Large" Font-Names="Verdana" ForeColor="Maroon"></asp:Label>  
  16.                     </td>  
  17.                 </tr>  
  18.                 <tr>  
  19.                     <td>  
  20.                         <asp:Label ID="Label4" runat="server" Text="Please Enter FirstNumber" Font-Size="Large"  
  21.                             Font-Names="Verdana" Font-Italic="true"></asp:Label>  
  22.                     </td>  
  23.                     <td>  
  24.                         <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>  
  25.                     </td>  
  26.                 </tr>  
  27.                 <tr>  
  28.                     <td>  
  29.                         <asp:Label ID="Label7" runat="server" Text="Please Enter SecondNumber" Font-Size="Large"  
  30.                             Font-Names="Verdana" Font-Italic="true"></asp:Label>  
  31.                     </td>  
  32.                     <td>  
  33.                         <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>  
  34.                     </td>  
  35.                 </tr>  
  36.                 <tr>  
  37.                     <td colspan="2" align="center">  
  38.                         <asp:Button ID="Button5" runat="server" Text="Addition" Font-Names="Verdana" Width="213px"  
  39.                             BackColor="Orange" Font-Bold="True" OnClick="Button1_Click" />  
  40.                     </td>  
  41.                 </tr>  
  42.                 <tr>  
  43.                     <td colspan="2" align="center">  
  44.                         <asp:Button ID="Button6" runat="server" Text="Substraction" Font-Names="Verdana"  
  45.                             Width="213px" BackColor="Orange" Font-Bold="True" OnClick="Button2_Click" />  
  46.                     </td>  
  47.                 </tr>  
  48.                 <tr>  
  49.                     <td colspan="2" align="center">  
  50.                         <asp:Button ID="Button7" runat="server" Text="Multiplication" Font-Names="Verdana"  
  51.                             Width="213px" BackColor="Orange" Font-Bold="True" OnClick="Button3_Click" />  
  52.                     </td>  
  53.                 </tr>  
  54.                 <tr>  
  55.                     <td colspan="2" align="center">  
  56.                         <asp:Button ID="Button8" runat="server" Text="Division" Font-Names="Verdana" Width="213px"  
  57.                             BackColor="Orange" Font-Bold="True" OnClick="Button4_Click" />  
  58.                     </td>  
  59.                 </tr>  
  60.                 <tr>  
  61.                     <td colspan="2" align="center">  
  62.                         <asp:Label ID="Label8" runat="server" Font-Bold="true" Font-Names="Verdana"></asp:Label>  
  63.                     </td>  
  64.                 </tr>  
  65.             </table>  
  66.         </div>  
  67.     </center>  
  68.     </form>  
  69. </body>  
  70. </html>  

Step 3: The complete code of webform1.aspx.cs looks like this

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;namespace LamdaExpressionApp  
  7. {  
  8.     public partial class WebForm1 : System.Web.UI.Page  
  9.     {  
  10.         protected void Page_Load(object sender, EventArgs e)  
  11.         {  
  12.         }  
  13.         protected void Button1_Click(object sender, EventArgs e)  
  14.         {  
  15.             if (string.IsNullOrEmpty(TextBox4.Text) || string.IsNullOrEmpty(TextBox1.Text))  
  16.             {  
  17.                 Label5.Text = "Please Enter Some Values";Label5.ForeColor = System.Drawing.Color.Red;  
  18.             }  
  19.             else  
  20.             {  
  21.                 ArithmticDelegate objAdd = (x, y) => x + y;  
  22.                 Label5.Text = "Addition Result is: " + objAdd.Invoke(double.Parse(TextBox4.Text), double.Parse(TextBox1.Text));  
  23.                 Label5.ForeColor = System.Drawing.Color.Green;TextBox1.Text = string.Empty;  
  24.                 TextBox4.Text = string.Empty;  
  25.             }  
  26.         }  
  27.         protected void Button2_Click(object sender, EventArgs e)  
  28.         {  
  29.             if (string.IsNullOrEmpty(TextBox4.Text) || string.IsNullOrEmpty(TextBox1.Text))  
  30.             {  
  31.                 Label5.Text = "Please Enter Some Values";  
  32.                 Label5.ForeColor = System.Drawing.Color.Red;  
  33.             }  
  34.             else  
  35.             {  
  36.                 ArithmticDelegate objSub = (x, y) => x - y;  
  37.                 Label5.Text = "Substraction Result is: " + objSub.Invoke(double.Parse(TextBox4.Text), double.Parse(TextBox1.Text));  
  38.                 Label5.ForeColor = System.Drawing.Color.Green;TextBox1.Text = string.Empty;TextBox4.Text = string.Empty;  
  39.             }  
  40.         }  
  41.         protected void Button3_Click(object sender, EventArgs e)  
  42.         {  
  43.             if (string.IsNullOrEmpty(TextBox4.Text) || string.IsNullOrEmpty(TextBox1.Text))  
  44.             {  
  45.                 Label5.Text = "Please Enter Some Values";Label5.ForeColor = System.Drawing.Color.Red;  
  46.             }  
  47.             else  
  48.             {  
  49.                 ArithmticDelegate objMul = (x, y) => x * y;Label5.Text = "Multiplication Result is: " + objMul.Invoke(double.Parse(TextBox4.Text), double.Parse(TextBox1.Text));  
  50.                 Label5.ForeColor = System.Drawing.Color.Green;TextBox1.Text = string.Empty;TextBox4.Text = string.Empty;  
  51.             }  
  52.         }  
  53.         protected void Button4_Click(object sender, EventArgs e)  
  54.         {  
  55.             if (string.IsNullOrEmpty(TextBox4.Text) || string.IsNullOrEmpty(TextBox1.Text))  
  56.             {  
  57.                 Label5.Text = "Please Enter Some Values";  
  58.                 Label5.ForeColor = System.Drawing.Color.Red;  
  59.             }  
  60.             else  
  61.             {  
  62.                 ArithmticDelegate objDiv = (x, y) => x / y;  
  63.                 Label5.Text = "Division Result is: " + objDiv.Invoke(double.Parse(TextBox4.Text), double.Parse(TextBox1.Text));  
  64.                 Label5.ForeColor = System.Drawing.Color.Green;  
  65.                 TextBox1.Text = string.Empty;TextBox4.Text = string.Empty;  
  66.             }  
  67.         }  
  68.         #region Public Memberspublic delegate double ArithmticDelegate(double a, double b);  
  69.         #endregion  
  70.     }  
  71. }

Step 4: The output of the application looks like this

Output2.png
 

Step 5: The addition operation output of the application looks like this

  Output3.png

I hope this article is useful for you.

Next Recommended Reading Lambda Expression In C#

MVC Corporation
MVC Corporation is consulting and IT services based company.