[WinForm]람다 델리게잇 익명메소드

2012. 4. 16. 08:38C#/WinForm

// 대리자 정의

public delegate string aDelagateFunc(string name);

// 대리자와 같은 인자를 같은 함수

string otherfunc(string n)

{

    return "hello " + n;

}

private void button1_Click(object sender, EventArgs e)

{

    // 대리자를 통하여 함수를 호출

    aDelagateFunc del = new aDelagateFunc(otherfunc);

    MessageBox.Show(del("World"));

}

 

//C# 2.0 : Inline function "anonymous Methods"

public delegate string aDelagateFunc2(string name);

private void button2_Click(object sender, EventArgs e)

{

    // 대리자와 익명메소드를 결합한다.

    aDelagateFunc2 del2 = delegate(string s) { return "hello " + s; };

    // 익명 메소드를 호출.

    MessageBox.Show(del2("anonymous method"));

}

 

//.Net 3.0 : lambda expression

public delegate string LambdaDelegate(string name);

private void button3_Click(object sender, EventArgs e)

{

    LambdaDelegate _lamdadel = strname => "hello " + strname;

    string strResult = _lamdadel("Lamda Expression Example");

    MessageBox.Show(strResult);

}


출처: http://hihanguk.pe.kr/100066953728

'C# > WinForm' 카테고리의 다른 글

[WinForm]MID 폼 일때 Child 모두 닫기  (0) 2012.08.28
[WinForm] 배열선언안하고 컨트롤 찾기  (0) 2012.06.04
[WinForm] 익명변수  (0) 2012.04.06
[WinForm] Mouse Api  (0) 2012.02.17
[WinForm] 크로스쓰레딩 발동잠금  (0) 2012.02.17