class Program
{
static void Main(string[] args)
{
new Program().Test();
}
// ๋ธ๋ฆฌ๊ฒ์ดํธ ์ ์ -> ๊ทธ๋ฆ์ ๊ฐ๋
์ ์ ์
delegate int MyDelegate(string s);
void Test()
{
//๋ธ๋ฆฌ๊ฒ์ดํธ ๊ฐ์ฒด ์์ฑ -> ๊ทธ๋ฆ ์์ฑ -> ํจ์๋ฅผ ๋ด์
MyDelegate m = new MyDelegate(StringToInt);
//๋ธ๋ฆฌ๊ฒ์ดํธ ๊ฐ์ฒด(ํจ์๋ด์ ๊ทธ๋ฆ)๋ฅผ ๋ฉ์๋๋ก ์ ๋ฌ
Run(m);
}
// ๋ธ๋ฆฌ๊ฒ์ดํธ ๋์์ด ๋๋ ์ด๋ค ๋ฉ์๋
int StringToInt(string s)
{
return int.Parse(s);
}
// ๋ธ๋ฆฌ๊ฒ์ดํธ๋ฅผ ์ ๋ฌ ๋ฐ๋ ๋ฉ์๋
void Run(MyDelegate m)
{
// ๋ธ๋ฆฌ๊ฒ์ดํธ๋ก๋ถํฐ ๋ฉ์๋ ์คํ
// ๊ทธ๋ฆ์ ๋ด๊ธด ํจ์๋ฅผ ์คํ. -> StringToInt(string s)๋ฅผ ์คํํ๊ฑฐ์.
int i = m("123");
Console.WriteLine(i);
}
}
Delegate ์์ ํจ์ ๋ฐ๊ฟ๋ฒ๋ฆผ.
using System;
namespace MySystem
{
class MyClass
{
// 1. delegate ์ ์ธ
private delegate void RunDelegate(int i);
private void RunThis(int val)
{
// ์ฝ์์ถ๋ ฅ : 1024
Console.WriteLine("{0}", val);
}
private void RunThat(int value)
{
// ์ฝ์์ถ๋ ฅ : 0x400
Console.WriteLine("0x{0:X}", value);
}
public void Perform()
{
// 2. delegate ์ธ์คํด์ค ์์ฑ
RunDelegate run = new RunDelegate(RunThis);
// 3. delegate ์คํ
run(1024);
//run = new RunDelegate(RunThat); ์ ์ค์ฌ์
//์๋์ ๊ฐ์ด ์ธ ์ ์๋ค.
run = RunThat;
run(1024);
}
}
class Program
{
static void Main(string[] args)
{
MyClass mc = new MyClass();
mc.Perform();
}
}
}
Delegate ๊ฐ์ฒด ๋ด์ ์ฌ๋ฌ๊ฐ ํจ์ ๋ฑ๋ก
class Program
{
public delegate void testDelegate(string str);
static void Main(string[] args)
{
testDelegate tDelegate;
//tDelegate += aaa; //์ฒ์ํ ๋ ์๋จ ์ด๊ธฐํ ๋๊ฒ ์์ด์
tDelegate = aaa;
tDelegate("aaa");
tDelegate += bbb;
tDelegate("add bbb");
tDelegate -= aaa;
tDelegate("rm aaa");
}
static void aaa(string str)
{
Console.WriteLine("aaa str: {0}", str);
}
static void bbb(string str)
{
Console.WriteLine("bbb str: {0}", str);
}
}
class Program
{
static void Main(string[] args)
{
new Program().Test();
}
// ๋ธ๋ฆฌ๊ฒ์ดํธ ์ ์
delegate int MyDelegate(string s);
void Test()
{
//๋ธ๋ฆฌ๊ฒ์ดํธ ๊ฐ์ฒด ์์ฑ
MyDelegate m = new MyDelegate(StringToInt);
//๋ธ๋ฆฌ๊ฒ์ดํธ ๊ฐ์ฒด๋ฅผ ๋ฉ์๋๋ก ์ ๋ฌ
Run(m);
}
// ๋ธ๋ฆฌ๊ฒ์ดํธ ๋์์ด ๋๋ ์ด๋ค ๋ฉ์๋
int StringToInt(string s)
{
return int.Parse(s);
}
// ๋ธ๋ฆฌ๊ฒ์ดํธ๋ฅผ ์ ๋ฌ ๋ฐ๋ ๋ฉ์๋
void Run(MyDelegate m)
{
// ๋ธ๋ฆฌ๊ฒ์ดํธ๋ก๋ถํฐ ๋ฉ์๋ ์คํ
int i = m("123");
Console.WriteLine(i);
}
}