C#: Delegate와 1회용 Delegate
/*
* made by so_Sal
*/
///////////////// 1회용 Delegate 사용
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
namespace test_console
{
class Program
{
delegate string Fire(string location);
static void Main(string[] args)
{
Fire fire;
fire = delegate(string location)
{
return location;
}; //1회용 델리게이트의 선언
Console.WriteLine("{0}",fire("here"));
}
}
}
///////////////////// 델리게이트 체인
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
namespace test_console
{
class Program
{
delegate void myFire(string location);
public static void call119(string location)
{
Console.WriteLine("불난곳: {0}",location);
}
public static void shout(string location)
{
Console.WriteLine("소리친곳: {0}", location);
}
public static void escape(string location)
{
Console.WriteLine("도망가는곳: {0}", location);
}
static void Main(string[] args)
{
myFire fire = new myFire(call119);
fire += new myFire(call119);
fire += new myFire(shout);
fire += new myFire(escape);
fire("인천에서"); //Delegate chain의 연속적 실행
}
}
}
실행 결과