1. 출력 결과가 다음과 같이 나오도록 아래의 코드에 익명 메소드를 추가하여 완성하세요.
7
2
풀이
using System;
namespace Ex13_1
{
delegate int MyDelegate(int a, int b);
class MainApp
{
static void Main(string[] args)
{
MyDelegate Callback;
Callback = delegate (int a, int b)
{
return a + b;
};
Console.WriteLine(Callback(3, 4));
Callback = delegate (int c, int d)
{
return c - d;
};
Console.WriteLine(Callback(7, 5));
}
}
}
============================================================================
2. 출력 결과가 다음과 같이 나오도록 다음 코드에 이벤트 처리기를 추가하세요.
축하합니다! 30번째 고객 이벤트에 당첨되셨습니다.
풀이
using System;
namespace Ex13_2
{
delegate void MyDelegate(int a);
class Market
{
public event MyDelegate CustomerEvent;
public void BuySomething(int CustomerNo)
{
if (CustomerNo == 30)
CustomerEvent(CustomerNo);
}
}
class MainApp
{
static public void MyHandler(int no)
{
Console.WriteLine($"축하합니다! {no}번째 고객 이벤트에 당첨되셨습니다.");
}
static void Main(string[] args)
{
Market market = new Market();
market.CustomerEvent += new MyDelegate(MyHandler);
for (int customerNo = 0; customerNo < 100; customerNo += 10)
market.BuySomething(customerNo);
}
}
}
============================================================================
[ References ]
'Book Review > 이것이 C# 이다' 카테고리의 다른 글
이것이 C#이다 연습문제 15장 (1) | 2021.06.13 |
---|---|
이것이 C#이다 연습문제 14장 (0) | 2021.06.02 |
이것이 C#이다 연습문제 12장 (0) | 2021.03.14 |
이것이 C#이다 연습문제 11장 (0) | 2021.01.17 |
이것이 C#이다 연습문제 10장 (0) | 2021.01.16 |