728x90
반응형
[예제1]
대리자
1. 동일한 함수 원형을 정의하고 앞에 delegate를 붙이고 이름정의
2. delegate 객체 생성(MyDelegate를 클래스라고 생각한다)
3. 객체 생성시 생성자에 인수(함수)를 전달한다.
[1] 사용하는 방법
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;
namespace ConsoleApp1
{
delegate void MyDelegate();
class Program
{
static void f1() { WriteLine(1); }
static void f2() { WriteLine(2); }
static void Main(string[] args)
{
MyDelegate md = new MyDelegate(f1);
md();
md = new MyDelegate(f2);
md();
md = new MyDelegate(f1);
md();
}
}
}
[2] 사용하는 방법
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;
namespace ConsoleApp1
{
delegate void F3(int a, string b);
class Program
{
static void f1(int a, string b) { WriteLine(1); }
static void f2(int a, string b) { WriteLine(2); }
static void Main(string[] args)
{
F3 ttt = new F3(f1);
ttt(2, "tiger");
ttt = new F3(f2);
ttt(2, "tiger");
}
}
}
[3] 사용하는 방법
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;
namespace ConsoleApp1
{
class A
{
public void f1(int a) { WriteLine("한식요리" + a); }
public void f2(int a) { WriteLine("일식요리" + a); }
}
delegate void F3(int a);
class Program
{
static void Main(string[] args)
{
A a = new A();
F3 f3 = new F3(a.f1);
f3(10);
f3 = new F3(a.f2);
f3(20);
}
}
}
[4] 사용하는 방법
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;
namespace ConsoleApp1
{
class A
{
public void f1(int a) { WriteLine("한식요리" + a); }
public void f2(int a) { WriteLine("일식요리" + a); }
}
delegate void F3(int a);
class Program
{
static void Main(string[] args)
{
A a = new A();
F3 f3 = new F3(a.f1);
f3(10);
f3 = new F3(a.f2);
f3(20);
f3 = new F3(new A().f1);
f3(30);
f3 = new F3(new A().f2);
f3(40);
}
}
}
[예제2]
quiz
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5 - i; j++)
{
Write("[{0} {1}]", j, j + 1);
}
WriteLine();
}
}
}
}
[예제3]
순차 정렬
역순 정렬
using System;
using static System.Console;
namespace ConsoleApp1
{
class Program
{
delegate int Compare(int a, int b);
static int AscendCompare(int a, int b)
{
if (a > b)
return 1;
else if (a == b)
return 0;
else
return -1;
}
static int DescendCompare(int a, int b)
{
if (a < b)
return 1;
else if (a == b)
return 0;
else
return -1;
}
static void BubbleSort(int[] DataSet, Compare compare)
{
for (int i = 0; i < DataSet.Length - 1; i++)
for (int j = 0; j < DataSet.Length - 1 - i; j++)
if (compare(DataSet[j], DataSet[j + 1]) > 0)
{
int t = DataSet[j + 1];
DataSet[j + 1] = DataSet[j];
DataSet[j] = t;
}
}
static void Main(string[] args)
{
int[] array = { 3, 7, 4, 2, 10 };
// 순차 정렬
Compare compare = new Compare(AscendCompare);
BubbleSort(array, compare);
foreach (var item in array)
{
Write(item + " ");
}
WriteLine();
// 역순 정렬
compare = new Compare(DescendCompare);
BubbleSort(array, compare);
foreach (var item in array)
{
Write(item + " ");
}
WriteLine();
}
}
}
728x90
반응형
'Education > Edu | .net' 카테고리의 다른 글
# 30) [C#] 문법13 (thread) (0) | 2021.02.26 |
---|---|
# 29.2) [C#] 문법12 (0) | 2021.02.24 |
# 28.2) [C#] 문법11 (Generic) (0) | 2021.02.23 |
# 28.1) [C#] 문법10 (0) | 2021.02.23 |
[C# 언어 3강] 데이터형 (5/5) 값 형식과 참조 형식, 정리 (0) | 2021.02.23 |