Tuesday, 13 March 2018

044 C# Anonymous Method

Anonymous Method 

Anonymous means which has no name or unknown name.. Net Framework in C# platform allow us to write method who has no name. Anonymous method has no name and no corresponding declaration no parameter passing no and no code block. Delegate is declared and pass code block to the delegate. Below is the example of a Anonymous method.

Example 1
 
using System;
public class Program
{
          public delegate void DemoAnonymous(int a,int b);
          public static void Main()
              {
                             DemoAnonymous obj = delegate (int x,int y)
                              {
                                  int c = x + y;
                                  Console.WriteLine("Thre Result is : {0}", c.ToString());
                               };
                             obj(100,200);
                             Console.ReadKey();
              }
}

Output :
 Thre Result is : 300

Advantage of anonymous method is 
  • Reduce code overhead. 
  • No need of separate area for method declaration. 
  • Simplified code for implementation. 

Here is some point about Anonymous Method 
  • Variable can be declared in Anonymous Method. 
  • Parameter can be passed to Anonymous Method from delegates. 
  • Anonymous Method can access variables outer scope. 
  • Anonymous Method cannot access ref or out parameter of the out of scope. 
  • Anonymous Method , if encounter jump , go , break statement to outer scope it will cause error. 
  • Anonymous Method can be worked with event Handler. 
  • Anonymous method cannot handle unsafe code. 

 Example 2

 Below is the example that Anonymous Method can access variables outer scope.
 
using System;
public class Program
          {
                      public delegate void DemoAnonymous(int a,int b);
                      public static int d = 200;

                      public static void Main()
                       {
                                   DemoAnonymous obj = delegate (int x,int y)
                                     {
                                        int c = x + y+d;
                                        Console.WriteLine("Thre Result is : {0}", c.ToString());
                                      };
                                     obj(100,200);
                                    Console.ReadKey();
                        }
        }

 Output :
 Thre Result is : 500



 Example 3

 Below is the example that Anonymous Method can be work with event handler.
------------------------------------------aspx Page------------------------------------------------------------------
 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
         <title></title>
</head>
<body>
            <form id="form1" runat="server">
             <asp:Button ID="Button1" runat="server" Text="Button" />
             </form>
</body>
</html>
 -----------------------------------C# Code------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
              protected void Page_Load(object sender, EventArgs e)
             {
                    Button1.Click += delegate
                    {
                             string script = "";
                             System.Type obj = this.GetType();
                             ClientScript.RegisterStartupScript(obj, "mySite", script);
                     };
            }
}

Output :

No comments:

Post a Comment

বাঙালির বেড়ানো সেরা চারটি ঠিকানা

  বাঙালি মানে ঘোড়া পাগল | দু একদিন ছুটি পেলো মানে বাঙালি চলল ঘুরতে | সে সমুদ্রই হোক , পাহাড়ি হোক বা নদী হোক। বাঙালির ...