using System;
public class delegates
{
//대리자 선언은 클래스 밖에서 선언하여 상관없음
public delegate void GoHome(); //매개변수 없으며, 반환값이 없음.
public delegate void Gop(int a); //매개변수가 있는 매서드 선언
public delegate int Hap(int a, int b); //반환값이 있는 대리자
public static void Main()
{
//1. 다중 메서드 호출
Car car;
car = new Car();
car.Run();
car.Left();
car.Right();
//2. 대리자를 통해서 대신 호출
//값을 넘겨주는 매겨변수는 이름을 넘겨준다
GoHome go = new GoHome(car.Run); //메서드 등록
go+= new GoHome(car.Left); //add 추가
go += new GoHome(car.Right);
go -= new GoHome(car.Left); //reMove 삭제
go(); //[c]대신호출
//3.매개변수가 있는 메서드 대신 호출
Gop gop = new Gop(car.Test);
gop(2);
gop(4);
//3.반환값이 있는 대리자 호출(테스트)
Hap hap = new Hap(car.Sum);
Console.WriteLine(hap(3,5));
}
}
'.Net Project > .Net 3.5 Sp1' 카테고리의 다른 글
67장 제네릭 메서드 (0) | 2009.08.13 |
---|---|
66장 이벤트(Event) (0) | 2009.08.13 |
64장 알고리즘(병합) (0) | 2009.08.12 |
63장 알고리즘(이진검색) (0) | 2009.08.12 |
62장 알고리즘(순차검색) (0) | 2009.08.12 |