C# -- event vs delegate



환경 :


참고 : http://www.csharpstudy.com/CSharp/CSharp-event.aspx

          http://www.csharpstudy.com/CSharp/CSharp-delegate3.aspx

          http://www.java2s.com/Tutorial/CSharp/0460__GUI-Windows-Forms/UsethebuiltinEventHandlerdelegate.htm


          https://msdn.microsoft.com/en-us/library/aa645739(v=vs.71).aspx
          http://csharpindepth.com/Articles/Chapter2/Events.aspx   ==> Delegates and Events

          http://www.c-sharpcorner.com/UploadFile/puranindia/C-Sharp-net-delegates-and-events/

          http://stackoverflow.com/a/803485

          http://cafe.naver.com/cstudyjava/2595

          http://cafe.naver.com/cstudyjava/2463

          http://www.akadia.com/services/dotnet_delegates_and_events.html

          

          https://msdn.microsoft.com/en-us/library/ms173171.aspx    ==> Delegates (C# Programming Guide)

          https://msdn.microsoft.com/en-us/library/ms173176.aspx   ==> How to: Declare, Instantiate, and Use a Delegate (C# Programming Guide)

          https://msdn.microsoft.com/en-us/library/awbftdfh.aspx    ===> Events (C# Programming Guide)
          https://msdn.microsoft.com/ko-kr/library/edzehd2t(v=vs.110).aspx   ==> 이벤트 처리 및 발생

          https://msdn.microsoft.com/ko-kr/library/orm-9780596521066-01-17.aspx   ===> Delegates and Events


          C# 6.0 in a Nutshell, p.127 참고하기

          

          http://www.cshandler.com/2011/07/delegates-and-async-programming-part-i.html  ==> Delegates and Async Programming Part I 

          http://www.cshandler.com/2011/07/delegate-and-async-programming-part-ii.html   ==> Delegate and Async Programming Part II - C# (AsyncCallback and Object State)



** delegate


1) delegate type declaration  -- delegate definition


delegate int Transformer (int x);



2) create a delegate instance


Transformer t = Square;          // create delegate instance


or


Transformer t = new Transformer(Square);



3) delegate instance 실행하기


int result = t(3);               // invoke delegate


or


t.Invoke(3)


// 비동기로 BeginInvoke/EndInvoke  사용 가능...




<< 참고 >>

= , += , -=  3가지 모두 사용가능.






** event


1) delegate definition 


예)   public delegate void PriceChangedHandler (decimal oldPrice, decimal newPrice);



2) event declaration  ; 발행자(Publisher)에서 선언함.

     -- delegate 와 event 동시 선언하는 경우.

     -- EventHandler  ; .NET Framework 에서 제공하는 사전 정의된 delegate


public event EventHandler PriceChanged;



==> compiler 에 의해 아래와 같이 private delegate 선언event 선언으로 변환됨.


private EventHandler priceChanged;  // Declare a private delegate


public event EventHandler PriceChanged   // event accessor functions

{

    add { priceChanged += value; }

    remove { priceChanged -= value; }

}


3) delegate instance 생성 ; 구독자에서 생성후 event 에 등록함.




4) 실행하기 ; 발행자 내부에서 event 에 등록된 delegate instance 를 순서대로 실행함.




<< 참고 >>

 += , -=  2가지만 사용가능.





발행자 class ; event를 발행한다. 구독자가 등록한 method (event handler)를 event 발생시, 순서대로 실행시킨다.


구독자 class ; event 를 구독하고, event 발생시 실행할 method (event handeler)를 등록한다.



evnet 는 add, remove 를 가진 property 라고 생각하라!!!
















 

Posted by 자유프로그램
TAG , ,