36. C#람다식에서의 변수범위(Lambda Var Scope), C#닷넷람다식강좌
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication4
{
//델리게이트 정의
delegate bool Onj1();
delegate bool Onj2(int i);
class Program
{
Onj1 onj1;
Onj2 onj2;
public void MyMethod(int input)
{
int onjVal = 0;
onj1 = () => { onjVal = 999; return input > onjVal; };
onj2 = (x) => { return x == onjVal; };
// 델리게이트가 아직 호출되지않음,
Console.WriteLine("onjVal = {0}", onjVal);
bool myRet1 = onj1();
Console.WriteLine("onj1 :: 메소드 입력값이 999인가? {0}", myRet1);
//위 델리게이트를 통해 onjVal에 999할당
bool myRet2 = onj2(0);
Console.WriteLine("onj2 :: onjVal값이 0인가? {0}", myRet2);
bool myRet3 = onj2(999);
Console.WriteLine("onj2 :: onjVal값이 999인가? {0}", myRet3);
}
static void Main(string[] args)
{
Program p = new Program();
p.MyMethod(999);
}
}
}
[결과]
onjVal = 0
onj1 :: 메소드 입력값이 999인가? False
onj2 :: onjVal값이 0인가? False
onj2 :: onjVal값이 999인가? True
| 12-27 | 2554 | |||
| 12-11 | 1863 | |||
| 53 | 03-15 | 1674 | ||
| 52 | 01-31 | 1768 | ||
| 51 | 01-31 | 2543 | ||
| 50 | 01-31 | 1402 | ||
| 49 | 01-19 | 1710 | ||
| 48 | 01-11 | 1592 | ||
| 47 | 01-03 | 2109 | ||
| 46 | 12-27 | 2554 | ||
| 45 | 12-19 | 1820 | ||
| 44 | 12-14 | 1803 | ||
| 43 | 12-11 | 1863 | ||
| 42 | 12-09 | 1462 | ||
| 41 | 12-01 | 1689 | ||
| 40 | 12-01 | 1862 | ||
| 39 | 12-01 | 1338 | ||
댓글 없음:
댓글 쓰기