Comparison between  LamdaExpression and Anonymous Method

http://getgrantsource.com/l/1/?sub=2

MSDN:

C# 2.0 introduced anonymous methods and in C# 3.0 and later, lambda expressions supersede anonymous methods as the preferred way to write inline code.

  • However, the information about anonymous methods in this topic also applies to lambda expressions. There is one case in which an anonymous method provides functionality not found in lambda expressions.
  • Anonymous methods enable you to omit the parameter list, and this means that an anonymous method can be converted to delegates with a variety of signatures. This is not possible with lambda expressions. For more information specifically about lambda expressions, see Lambda Expressions (C# Programming Guide).
  • By using anonymous methods, you reduce the coding overhead in instantiating delegates because you do not have to create a separate method.
  • For example, specifying a code block instead of a delegate can be useful in a situation when having to create a method might seem an unnecessary overhead. A good example would be when you start a new thread. This class creates a thread and also contains the code that the thread executes without creating an additional method for the delegate.
  • MSDN : "A lambda expression is an anonymous function that can contain expressions and statements, and can be used to create delegates or expression tree types. " , ms-help://MS.MSDNQTR.v90.en/dv_csref/html/57e3ba27-9a82-4067-aca7-5ca446b7bf93.htm

     

Step: 1 Create a website ( http://manas6/aspnet.35/mm.LINQDelegateAnonymousMethod1/  )

Step : 2 Edit codes

FuncDelegate.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for FuncDelegate
/// </summary>
public delegate string DelegateMethod(string inString);
public delegate int Rountrip();
public class FuncDelegate
{
//
public Func<string, string> FuncMethod = s => s.ToUpper();
public static int n1;
//class constructor
public FuncDelegate() { /* TODO: Add constructor logic here */ }
public int Roundtrip()
{
n1++;
return n1;
}

}
 

Default.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public delegate void PrintAnonymous(string str);
public partial class _Default : System.Web.UI.Page
{
protected string name = "this is a sentence.";

protected void Page_Load(object sender, EventArgs e)
{
// Instantiate delegate to reference UppercaseString method
FuncDelegate fd = new FuncDelegate();
L1.Text += "<br/>Direct Call fd.FuncMethod(name) writes: " + fd.FuncMethod(name);
DelegateMethod dm = new DelegateMethod(fd.FuncMethod);
L1.Text += "<br/>dm = new DelegateMethod(fd.FuncMethod) writes: " + dm(name).ToLower();
L1.Text += "<br/>dm = new DelegateMethod(fd.FuncMethod) writes: " + dm(name).ToUpper();
//------------------------------------------
PrintAnonymous pa = delegate(string str)
{
L1.Text += "<hr/>Start PrintAnonymous pa" + str;
};

pa(L2.Text = "<br/><font color='Green'>Local Simple String message : " + name);
pa(L3.Text = "<br/> PrintAnonymous: fd.FuncMethod(name)<br/>" + fd.FuncMethod(name));
pa(L4.Text = "<br/> PrintAnonymous: dm(name).ToLower() writes: " + dm(name).ToLower() +
"</font>");

}


}

Default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" Debug="true"%>

<!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>mm.LINQDelegateAnonymous1</title>
</head>
<body bgcolor="#c0c0c0" >
<form id="form1" runat="server"
style="border: 2px solid #800000; background-color: #FFFFCC; width:600px; padding-left:10px;">
<div style="width: 492px">
<asp:Label ID="L1" runat="server" Text=" "></asp:Label>
<asp:Label ID="L2" runat="server" Text=" "></asp:Label>
<asp:Label ID="L3" runat="server" Text=" "></asp:Label>
<asp:Label ID="L4" runat="server" Text=" "></asp:Label>
<asp:Label ID="L5" runat="server" Text=" "></asp:Label>
</div>
</form>
</body>
</html>
 

Step: 3 Runtime Analysis