예제 1
namespace Cshop_v6._0
{
delegate int GetResultDelegate();
class Target
{
public void Do(GetResultDelegate getResult)
{
Console.WriteLine(getResult()); // 콜백 메서드 호출
}
}
class Source
{
public int GetResult() // 콜백 용도로 전달된 메서드
{
return 10;
}
public void Test()
{
Target target = new Target();
target.Do(new GetResultDelegate(this.GetResult));
}
}
}
예제 2
namespace Cshop_v6._0
{
delegate int GetResultDelegate(int x, int y);
class Target
{
public void Do(GetResultDelegate getResult, int x, int y)
{
Console.WriteLine(getResult(x, y));
}
}
class Source
{
public static int GetResult(int x, int y)
{
return x * y;
}
public static void Main()
{
Target target = new Target();
GetResultDelegate gd = GetResult;
target.Do(gd, 5, 10);
}
}
}
댓글 없음:
댓글 쓰기