[CLR via C#]17#Delegates WORK-IN-PROGRESS

delegate 與 event 的關係



delegate僅僅是回調函數嗎?



event在。net中主要用在什麼地方?

  • Event handlers (for GUI and more)
  • Starting threads
  • Callbacks (e.g. for async APIs)
  • LINQ and similar (List.Find etc)
  • Anywhere else where I want to effectively apply "template" code with some specialized logic inside (where the delegate provides the specialization)






type-safe


參數的類型,返回值的類型


Demystifying

a constructor
Invoke
BeginInvoke
EndInvoke


_target
_methodPtr
_invocationList

-------------------------------


Chaining


+=

-=


Generic Delegates

public delegate void Action<T> (T obj);


public delegate TResult Func<TResult>();


delegate void EventHandler<TEventArgs> (Object sender, TEventArgs e) where TEventArgs : EventArgs;


Syntactical Sugar





發佈了36 篇原創文章 · 獲贊 0 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章