C#2.0 introduces a new feature-anonymous method that allows developers to declare their own function code online (inline) without using a delegate function (delegate function). C#3.0 provides a new feature--LAMBDA expression, which provides a more concise format for accomplishing the same goal. Let's take a closer look at anonymous methods before discussing lambda expressions.
anonymous method
Suppose you need to create a button that updates the contents of the ListBox when you click it. In c#1.0 and 1.1, you have to do this:
public MyForm()
{
listBox = new ListBox(...);
textBox = new TextBox(...);
addButton = new Button(...);
addButton.Click += new EventHandler(AddClick);
}
void AddClick(object sender, EventArgs e)
{
listBox.Items.Add(textBox.Text);
}
In c#2.0, you need to do this:
public MyForm()
{
listBox = new ListBox(...);
textBox = new TextBox(...);
addButton = new Button(...);
addButton.Click += delegate
{
listBox.Items.Add(textBox.Text);
};
As you can see, you don't have to specifically declare a new method to connect it to an event. You can use anonymous methods in c#2.0 to do the same job. C#3.0 introduces a simpler format, lambda expression, you can use "=>" to write your expression list, followed by an expression or statement block.
Parameters in a lambda expression
Parameters in a lambda expression can be explicit or implicitly typed. In an explicit type parameter list, the type of each expression is explicitly specified. In an implicit type parameter list, the type is inferred from the context:
(int x) => x + 1 // 显式类型参数
(y,z) => return y * z; // 隐式类型参数
Lambda calculus instance
The following example shows two different ways to print a string with an even length in a list. The first method Anonmethod uses the anonymous method, and the second lambdaexample is implemented through the lambda calculus:
//Program.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.query;
using System.Xml.XLinq;
using System.Data.DLinq;
namespace Lambdaexample
{
public delegate bool Keyvaluefilter<k, v> (K key, V value);
Static Class program
{
static void Main (string[] args)
{
list<string> List = new list<string> ();
list. ADD ("AA");
list. ADD ("ABC");
List. ADD ("DEFG");
list. ADD ("XYZ");
Console.WriteLine ("Through Anonymous method");
Anonmethod (list);
Console.WriteLine ("Through LAMBDA expression");
lambdaexample (list);
dictionary<string, int> varclothes= new dictionary<string,int> ();
varclothes.add ("Jeans", 20);
Varclothes.add ("shirts", 15);
Varclothes.add ("Pajamas", 9);
Varclothes.add ("Shoes", 9);
var clotheslistshortage = Varclothes.filterby (string name,
int count) => name = = "Shoes" && count < 10);
//Example of multiple parameters
if (Clotheslistshortage.count > 0)
Console.WriteLine ("We are short of shoes");
Console.ReadLine ();
}
static void Anonmethod (list<string> List)
{
list<string> evennumbers = List. FindAll (Delegate (string i)
{return (i.length% 2) = = 0;});
foreach (String evennumber in Evennumbers)
{
Console.WriteLine (Evennumber);
}
}
static void Lambdaexample (list<string> List)
{
var evennumbers = list. FindAll (i => (i.length% 2) = = 0); Example of single parameter
foreach (String i in Evennumbers)
{
Console.WriteLine (i);
}
}
}
public static class Extensions
{
public static dictionary<k, v> filterby<k, v>
(This dictionary<k, v> items, keyvaluefilter<k, v> filter)
{
var result = new dictionary<k, v> ();
foreach (keyvaluepair<k, v> element in items)
{
if filter (element. Key, Element. Value))
result. ADD (element. Key, Element. Value);
}
return result;
}
}
}