摘要:(200-07-23) LINQ Lambda Expression
Lambda 使用方式
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace linqwinpart2
{
public partial class TestLambda : Form
{
public delegate String MyDelegate<T>(T T1, T T2); //1.定義泛型委派 回傳字串
public delegate Boolean MyDelegate1<T>(T T1, T T2); //1.定義泛型委派 回傳布林
public TestLambda()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//3.使用Lambda方式呼叫
MessageBox.Show(doit((String s1,String s2)=>{return s1+s2;})); //正常寫法
MessageBox.Show(doit((s1, s2) => s1 + s2)); //省略寫法
//3.使用Lambda方式呼叫
MessageBox.Show(doitE((String s1, String s2) => { return s1.Equals(s2); }).ToString()); //正常寫法
MessageBox.Show(doitE((s1, s2) => s1.Equals(s2)).ToString()); //省略寫法
}
//2.建立使用窗口 回傳字串
private String doit(MyDelegate<String> MD)
{
return MD.Invoke("泛型參數1", "泛型參數2");
}
//2.建立使用窗口 回傳布林
private Boolean doitE(MyDelegate1<String> MD1)
{
return MD1.Invoke("泛型參數1", "泛型參數2");
}
}
}