์ฐธ๊ณ ์์
๋๋ฆฌ์(Delegator)
- ์ฝ๋(๋ฉ์๋)๋ฅผ ๋์ ์คํํ๋ ๊ฐ์ฒด
- ๋ฉ์๋๋ฅผ ํธ์ถํ๋ฏ ์ฌ์ฉ(์ฆ, ์ธ์๋ฅผ ์ ๋ ฅํ๊ณ ๊ฒฐ๊ณผ๋ฅผ ๋ฐํ ๋ฐ์)
- ๋จ, ๋๋ฆฌ์๊ฐ ์คํํ ์ฝ๋๋ ์ปดํ์ผ ์์ ์ด ์๋ ์คํ ์์ ์ ๊ฒฐ์
๋๋ฆฌ์์ ์ ์ธ๊ณผ ์ฌ์ฉ
(1) delegate ํค์๋ ์ฌ์ฉ, ๋๋ฆฌ์ ์ ์ธ
delegate int MyDelegate(int a, int b); //์ธํฐํ์ด์ค ์ ์ธํ ๋ ์ฒ๋ผ ๊ตฌํ์ ์๋ค!
ํน์
ํ์ ์ - delegate - ๋ฐํํ - ๋ธ๋ฆฌ๊ฒ์ดํธ๋ช (๋งค๊ฐ๋ณ์ ๋ชฉ๋ก)
private delegate int MyDelegate(int a, int b);
(2) ์ฌ์ฉ ์
<์์ >
namespace Delegate // Note: actual namespace depends on the project name.
{
// 1. ๋๋ฆฌ์ ์ ์ธ
delegate int MyDelegate(int a, int b);
class Calculator
{
public int Plus(int a, int b)
{
return a + b;
}
public static int Minus(int a, int b)
{
return a - b;
}
}
class MainApp
{
static void Main(string[] args)
{
Calculator Calc = new Calculator();
MyDelegate Callback;
// 2. ๋๋ฆฌ์ ์ธ์คํด์ค ์์ฑ & ํธ์ถ (์ฐธ์กฐํ ๋ฉ์๋ -> ์ธ์)
Callback = new MyDelegate(Calc.Plus); // Plus ์ฐธ์กฐ
Console.WriteLine(Callback(3, 4));
// 2. ๋๋ฆฌ์ ์ธ์คํด์ค ์์ฑ & ํธ์ถ (์ฐธ์กฐํ ๋ฉ์๋ -> ์ธ์)
Callback = new MyDelegate(Calculator.Minus); // Minus ์ฐธ์กฐ
Console.WriteLine(Callback(7, 5));
}
}
}
<์ง์ >
// See https://aka.ms/new-console-template for more information
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using System;
namespace Delegate
{
public delegate int MyCalDelegate(int firstNum, int secoundNum);
class Calculator
{
public int Sum(int firstNum, int secoundNum)
{
return firstNum + secoundNum;
}
public int Minus(int firstNum, int secoundNum)
{
return firstNum - secoundNum;
}
}
class MainApp
{
static void Main(string[] args)
{
Calculator BasicCalculator = new Calculator();
//์ฆ, BasicCalculator.Sum๋ฅผ ๋๋ฆฌ ์คํํ๋๋ก ์์์ ๋ฐ์ ์ฝ๋ฐฑ์ธ plusCalDelegate๋ฅผ ์คํํจ
MyCalDelegate plusCalDelegate = new MyCalDelegate(BasicCalculator.Sum);
MyCalDelegate minusCalDelegate = new MyCalDelegate(BasicCalculator.Minus);
Console.WriteLine(plusCalDelegate(10, 23));
Console.WriteLine(minusCalDelegate(5, 1));
}
}
}
์ถ๋ ฅ ๊ฒฐ๊ณผ
๋ฌผ๋ก ์๋์ ๊ฐ์ด ์์ฑํ์ฌ๋ ๊ฒฐ๊ณผ๋ ๋๊ฐ์ด ๋์จ๋ค.
Calculator BasicCalculator = new Calculator();
MyCalDelegate plusCalDelegate = BasicCalculator.Sum;
MyCalDelegate minusCalDelegate = BasicCalculator.Minus;
Console.WriteLine(plusCalDelegate(10, 23));
Console.WriteLine(minusCalDelegate(5, 1));
์ต๋ช ๋ฉ์๋(Anonymous Method)
์ต๋ช ๋ฉ์๋๋ ๋ค๋ฅธ
์์ ์ฝ๋ ๋ธ๋ก์์ ์ฌ์ฌ์ฉ๋ ์ผ์ด ์๋ ์ด๋ฆ ์๋ ๋ฉ์๋์ด๋ค.
์ต๋ช ๋ฉ์๋ ๊ตฌํ ์ ์ฐจ
1. ์ฌ์ ์ ์๋ ๋๋ฆฌ์ ํ์(๋ฐํํ, ๋งค๊ฐ๋ณ์)์ ๋ง์ถฐ ์ต๋ช ๋ฉ์๋ ์์ฑ
2. ์ต๋ช ๋ฉ์๋ ์ ์์ delegate ํค์๋๋ก ์์, ์ ์๋ ์ฝ๋๋ ๋๋ฆฌ์ ์ฐธ์กฐ์ ํ ๋น
3. ์ต๋ช ๋ฉ์๋๋ฅผ ํ ๋น๋ฐ์ ๋๋ฆฌ์ ์ฐธ์กฐ ํธ์ถ
namespace Delegate
{
public delegate int MyCalDelegate(int firstNum, int secoundNum);
class Calculator
{
public int Sum(int firstNum, int secoundNum)
{
return firstNum + secoundNum;
}
public int Minus(int firstNum, int secoundNum)
{
return firstNum - secoundNum;
}
}
class MainApp
{
static void Main(string[] args)
{
Calculator BasicCalculator = new Calculator();
MyCalDelegate plusCalDelegate = BasicCalculator.Sum;
MyCalDelegate minusCalDelegate = BasicCalculator.Minus;
MyCalDelegate divideCalDelegate = delegate (int a, int b)
{
return a / b;
};
Console.WriteLine(plusCalDelegate(10, 23)); //33
Console.WriteLine(minusCalDelegate(5, 1)); //4
Console.WriteLine(divideCalDelegate(60, 10)); //6
}
}
}
<์ฐ์ต> : ๋ธ๋ฆฌ์ผ์ดํธ ์ด์ฉํ ๋ฒ๋ธ์ ๋ ฌ ๋ง๋ค์ด๋ณด๊ธฐ (์ค๋ฆ์ฐจ์ ๋ธ๋ฆฌ์ผ์ดํธ /๋ด๋ฆผ์ฐจ์ ๋ธ๋ฆฌ๊ฒ์ดํธ)
namespace UsingCallback
{
class MainApp
{
delegate bool Compare(int a, int b);
static bool AscendCompare(int a, int b)
{
if (a > b)
{
return true;
}
else
{
return false;
}
}
static bool DescendCompare(int a, int b)
{
if (a < b)
{
return true;
}
else
{
return false;
}
}
static void BubbleSort(int[] numbers, Compare compareNumbers)
{
for (int i = 0; i < numbers.Length - 1; i++)
{
for (int j = 0; j < numbers.Length - 1 - i; j++)
{
if (compareNumbers(numbers[j], numbers[j + 1]))
{
int temp = numbers[j];
numbers[j] = numbers[j + 1];
numbers[j + 1] = temp;
}
}
}
}
static void Main(string[] args)
{
int[] numbers = { 3, 4, 6, 1, 9, 2, 7, 8, 10, 5 };
Compare Ascend = AscendCompare;
BubbleSort(numbers, Ascend);
Console.WriteLine();
for (int i = 0; i < numbers.Length; i++)
{
Console.Write(numbers[i]);
}
Console.WriteLine();
Compare Desscend = DescendCompare;
BubbleSort(numbers, Desscend);
for (int i = 0; i < numbers.Length; i++)
{
Console.Write(numbers[i]);
}
}
}
}
์ถ๋ ฅ๊ฒฐ๊ณผ
์ฐธ๊ณ
[์ด๊ฒ์ด C#์ด๋ค] 13. ๋๋ฆฌ์์ ์ด๋ฒคํธ
Key point ๋๋ฆฌ์ ์ผ๋ฐํ ๋๋ฆฌ์ ์ต๋ช ๋ฉ์๋ ์ด๋ฒคํธ 13.1 ๋๋ฆฌ์(delegate) ์ฝ๋ฐฑ(Callback) ์ด๋ค ์ผ์ ์ํํ๋ ์ฝ๋ ์๋ ์ฝ๋๊ฐ ์๋ ๋ค๋ฅธ ์ฝ๋์๊ฒ ๋์ ์คํํ๊ฒ ํ๋ ๊ฒ ํ๋ก๊ทธ๋จ ์คํ ์ค์ ๊ฒฐ์ ๋จ(
velog.io
(2) ๋ฒ๋ธ์ํธ
(3) ๋ฒ๋ธ ์ํธ
'C# > ์ด๊ฒ์ด C#์ด๋ค' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
๋๋ฆฌ์์ ์ด๋ฒคํธ(2) (0) | 2022.11.30 |
---|---|
์์ธ์ฒ๋ฆฌํ๊ธฐ(4) try~catch์ finally (0) | 2022.04.18 |
์์ธ ์ฒ๋ฆฌํ๊ธฐ(3) ์์ธ ๋์ง๊ธฐ (0) | 2022.04.17 |
์์ธ ์ฒ๋ฆฌํ๊ธฐ(2) System.Exception ํด๋์ค (0) | 2022.04.17 |
60. ์์ธ ์ฒ๋ฆฌํ๊ธฐ(1) (0) | 2022.04.09 |