intTypePromotion=1
zunia.vn Tuyển sinh 2024 dành cho Gen-Z zunia.vn zunia.vn
ADSENSE

Chapter 16 Quick Reference

Chia sẻ: Nghia Tuan | Ngày: | Loại File: PDF | Số trang:3

82
lượt xem
8
download
 
  Download Vui lòng tải xuống để xem tài liệu đầy đủ

Chương 16 Quick tham khảo để làm điều này đại biểu từ khóa Viết, theo sau là kiểu trả về, tiếp theo là khai báo một tên thuộc loại đại biểu, theo sau bất kỳ loại tham số. Ví dụ: đại biểu loại. đại biểu void Tick ();

Chủ đề:
Lưu

Nội dung Text: Chapter 16 Quick Reference

  1. Chapter 16 Quick Reference To Do this Write the keyword delegate, followed by the return type, followed by the Declare a name of the delegate type, followed by any parameter types. For example: delegate type. delegate void Tick(); Use the same syntax as a method call. For example: Invoke a Tick m; delegate. ... m(); Use the same syntax you use for a class or struct: write the keyword new, followed by the name of the type (the name of the delegate), followed by the argument between parentheses. The argument must be a method whose signature exactly matches the signature of the delegate. For Create an example: instance of a delegate. delegate void Tick(); private void Method() { ... } ... Tick m = new Tick(this.Method); Write the keyword event, followed by the name of the type (the type must be a delegate type), followed by the name of the event. For example: delegate void TickHandler(); Declare an event. class Ticker { public event TickHandler Tick; } Create a delegate instance (of the same type as the event), and attach the delegate instance to the event by using the += operator. For example: class Clock Subscibe to an { event. ... public void Start() { ticker.Tick += new TickHandler (this.RefreshTime);
  2. To Do this } private void RefreshTime() { ... } private Ticker ticker = new Ticker(); } You can also get the compiler to automatically generate the new delegate by just specifying the subscribing method: public void Start() { ticker.Tick += this.RefreshTime; } Create a delegate instance (of the same type as the event), and detach the delegate instance from the event by using the –= operator. For example: class Clock { ... public void Stop() { ticker.Tick - = new TickHandler (this.RefreshTime); } Unsubscribe from an event. private void RefreshTime() { ... } private Ticker ticker = new Ticker(); } Or: public void Stop() { ticker.Tick -= this.RefreshTime; } Raise an Use parentheses exactly as if the event were a method. You must supply
  3. To Do this event. arguments to match the type of the event. Don't forget to check whether the event is null. For example: class Ticker { public event TickHandler Tick; ... private void Notify() { if (this.Tick != null) { this.Tick(); } } ... }
ADSENSE

CÓ THỂ BẠN MUỐN DOWNLOAD

 

Đồng bộ tài khoản
2=>2