Point of Interest:

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.

  • MSDN "
  • The general rules for lambdas are as follows:
    • The lambda must contain the same number of parameters as the delegate type.
    • Each input parameter in the lambda must be implicitly convertible to its corresponding delegate parameter.
    • The return value of the lambda (if any) must be implicitly convertible to the delegate's return type.
    • Note that lambda expressions in themselves do not have a type because the common type system has no intrinsic concept of "lambda expression." However, it is sometimes convenient to speak informally of the "type" of a lambda expression. In these cases the type refers to the delegate type or Expression type to which the lambda expression is converted.
    • This Code Demonstrates an Anonymous Delegate
      • delegate void FunctionPointer(string str);
        static void Main(string[] args)
        {
        FunctionPointer fp = delegate(string s)
        {
        Console.WriteLine(s);
        Console.ReadLine();
        };
        fp("Hello World!");
         

In the example below, we are comparing Anonymous delegate with Lamda expression.

  •  using delegate Anonymously  : Func<string, string[]> funcRefofAMethod = delegate(string s) ....
  • using Func : Func<string,string[]> usingLamda = (s) => s.Split(spacer);

Step : 1 Create a web-site ( http://manas6/aspnet.35/mm.LINQFuncDelegate2/ )

Step: 2 Edit Codes

Class: FuncGeneric.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
//for IEnumerator and GetEnumerator();
using System.Collections;
/// <summary>
/// Summary description for FuncGeneric
/// </summary>
public class FuncGeneric
{
public string strProcess; public string str;
public FuncGeneric(){ str += "<br/>Constructor started"; }
public string StringProcessor(string title)
{
char [] spacer = new char[]{' '};
str += "<br/> Func<(Of <(T1, T2, TResult>)>)<font color='Teal'>";
str+="delegate with anonymous methods </font>";
// delegating a routine
Func<string, string[]> funcRefofAMethod = delegate(string s)
{
int i = s.Length;
char[] spacers = new char[] { ' ' };
return i > 0 ? s.Split(spacers, i) : s.Split(spacers);
};

str += "<br/>----->";
foreach (string word in funcRefofAMethod(title))
{
str += (word) + "&nbsp / ";
}
//alternate way
Func<string,string[]> usingLamda = (s) => s.Split(spacer);
//
str += "<br/> Using Lamda Expression <font color='red'>";
str+="(s) => s.Split(spacer);</font>";
str += "<br/>----->";
foreach(string word in usingLamda(title))
{
str += (word) + "&nbsp / ";
}
return str;
}


public static string[] UsingFunc(String param, int limit)
{
char[] spacers = new char[] { ' ' };
string[] str;
if (limit > 0)
{
str= param.Split(spacers, limit);
}
else
{
str = param.Split(spacers);
}
return str;
}
}
 

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 partial class _Default : System.Web.UI.Page
{
protected string str;
protected void Page_Load(object sender, EventArgs e)
{
FuncGeneric fg = new FuncGeneric();
L1.Text += "----- Calling a Method in an external Class";
L1.Text += fg.StringProcessor("<br/>If it be love indeed, tell me how much");

}
}
 

Default.aspx

<%@ 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>mm.LINQFuncDelegate2: Func Delegate and Lamda </title>
</head>
<body bgcolor="#cccccc">
<form id="form1" runat="server"
style="background-color: #FFFFCC; border: thick solid #800080; padding: 10px; margin: 10px; width: 500px">
<div>
<asp:Label ID="L1" runat="server" Text=""></asp:Label>
</div>
</form>
</body>
</html>
 

 

Step : 3 Runtime analysis

With debug

tying variable in watch window