[C#] delegate ????
Delegate(대리자)
-> Delegate 는 함수의 기능을 대신해주는 대리자 역할을 한다. 함수를 보다 효율적으로 사용하기 위해 함수 자체를 캡슐화하는 기능을 가지고 있다네..
------------------------------------------------------------------------------------------------------------------------
delegate void Dele1();
delegate void Dele2();
class Test
{
public void Test1()
{
System.Console.WriteLine("쏼라쏼라~ ");
}
public void Test2(int x)
{
System.Console.WriteLine("머라고?" + x);
}
}
public class test2
{
public static void Main()
{
Test a = new Test();
Dele1 t1 = new Dele1(a.Test1);
Dele2 t2 = new Dele2(a.Test2);
t1();
t2(18);
}
}
----------------------------------------------------------------------------------------------------------
the result is
쏼라쏼라~
머라고? 18
-----------------------------------------------------------------------------------------------------------
재밋어~ 재밋어~ 주문을 외우면 정말 재밋어져요~
이거 꼭 봐야되... 제발 !!!!
namespace Delegate1
{
delegate int MeDelegate();
class Program
{
static void Main()
{
MeDelegate d = ReturnFive;
d += ReturnTen;
d += Return22;
List<int> ints = new List<int>();
foreach (MeDelegate del in d.GetInvocationList())
ints.Add(del());
foreach (int i in ints)
Console.WriteLine(i);
}
static int ReturnFive() { return 5; }
static int ReturnTen() { return 10; }
static int Return22() { return 22; }
}
}
-> Delegate 는 함수의 기능을 대신해주는 대리자 역할을 한다. 함수를 보다 효율적으로 사용하기 위해 함수 자체를 캡슐화하는 기능을 가지고 있다네..
------------------------------------------------------------------------------------------------------------------------
delegate void Dele1();
delegate void Dele2();
class Test
{
public void Test1()
{
System.Console.WriteLine("쏼라쏼라~ ");
}
public void Test2(int x)
{
System.Console.WriteLine("머라고?" + x);
}
}
public class test2
{
public static void Main()
{
Test a = new Test();
Dele1 t1 = new Dele1(a.Test1);
Dele2 t2 = new Dele2(a.Test2);
t1();
t2(18);
}
}
----------------------------------------------------------------------------------------------------------
the result is
쏼라쏼라~
머라고? 18
-----------------------------------------------------------------------------------------------------------
재밋어~ 재밋어~ 주문을 외우면 정말 재밋어져요~
이거 꼭 봐야되... 제발 !!!!
namespace Delegate1
{
delegate int MeDelegate();
class Program
{
static void Main()
{
MeDelegate d = ReturnFive;
d += ReturnTen;
d += Return22;
List<int> ints = new List<int>();
foreach (MeDelegate del in d.GetInvocationList())
ints.Add(del());
foreach (int i in ints)
Console.WriteLine(i);
}
static int ReturnFive() { return 5; }
static int ReturnTen() { return 10; }
static int Return22() { return 22; }
}
}
----------------------
using System;
class Observable
{
public event EventHandler SomethingHappened;
public void DoSomething()
{
EventHandler handler = SomethingHappened;
if (handler != null)
{
handler(this, EventArgs.Empty);
}
}
}
class Observer
{
public void HandleEvent(object sender, EventArgs args)
{
Console.WriteLine("Something happened to " + sender);
}
}
class Test
{
static void Main()
{
Observable observable = new Observable();
Observer observer = new Observer();
observable.SomethingHappened += observer.HandleEvent;
observable.DoSomething();
}
}
댓글
댓글 쓰기