Anonymous Methods and Delegates

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

lượt xem

Anonymous Methods and Delegates

Mô tả tài liệu
  Download Vui lòng tải xuống để xem tài liệu đầy đủ

Chưa xác định phương pháp và đại biểu Tất cả các ví dụ về cách thêm một phương pháp để một đại biểu mà bạn đã thấy cho đến nay sử dụng tên của phương pháp. Ví dụ, trở về kịch bản nhà máy tự động hiển thị trước đó

Chủ đề:

Nội dung Text: Anonymous Methods and Delegates

  1. Anonymous Methods and Delegates All the examples of adding a method to a delegate that you have seen so far use the method's name. For example, returning to the automated factory scenario shown earlier, to add the StopFolding method of the folder object to the stopMachinery delegate, we did this: this.stopMachinery += folder.StopFolding; This approach is very useful if there is a convenient method that matches the signature of the delegate, but what if this is not the case? Suppose that the StopFolding method actually had the following signature: void StopFolding(int shutDownTime); // Shut down within the specified number of seconds This is now different from the FinishWelding and PaintOff methods, therefore we cannot use the same delegate to handle all three methods. Creating a Method Adapter The way around this problem is to create another method that calls StopFolding, but that takes no parameters itself, like this: void FinishFolding() { folder.StopFolding(0); // Shutdown immediately } NOTE The FinishFolding method is a classic example of an Adapter; a method that converts (or adapts) a method to give it a different signature. This pattern is very common, and is one of the set of patterns documented in the book Design Patterns: Elements of Reusable Object-Oriented Architecture by Gamma, Helm, Johnson, and Vlissides (Addison- Wesley Professional; 1994). In many cases, adapter methods such as this are small, and it is easy to lose them in a sea of methods, especially in a large class. Furthermore, apart from using it to adapt the StopFolding method for use by the delegate, it is unlikely to be called elsewhere. C# provides anonymous methods for situations such as this. Using an Anonymous Method as an Adapter
  2. An anonymous method is a method that does not have a name. This sounds very strange, but anonymous methods are actually quite useful. There might well be occassions when you have a block of code that you are never going to call directly, but you would like to be able to invoke using a delegate. The FinishFolding method discussed just now is an example; its sole purpose is to provide an adapter for the StopFolding method. Thinking up a name for the method is really just an overhead for the developer (albeit a small one), but consider what happens if there are a number of these adapter methods in an application—they start to clutter up the classes. You can use an anonymous method anywhere that you can use a delegate. You simply provide the code, enclosed in curly braces, and prefixed with the delegate keyword. To use an anonymous method as an adapter for the StopFolding method and add it to the stopMachinery delegate, you can write this: this.stopMachinery += delegate { folder.StopFolding(0); }; You no longer need to create the FinishFolding method. You can also pass an anonymous method as a parameter in place of a delegate, like this: control.Add(delegate { folder.StopFolding(0); } ); Features of Anonymous Methods Anonymous methods have several idiosyncracies that you should be aware of. They include the following: • Any parameters needed are specified in braces following the delegate keyword. For example: • control.Add(delegate(int param1, string param2) { /* code that uses param1 and param2 */ ... }); • Anonymous methods can return values, but the return type must match that of the delegate they are being added to. • You can use the –= operator to remove a method from a delegate variable. However, it does not make much sense to do so as it will not remove anything. Anonymous methods that happen to contain the same code are actually different instances of the code. • The code in an anonymous method is ordinary C# code. It can comprise multiple statements, method calls, variable definitions, and so on. • Variables defined in an anonymous method go out of scope when the method finishes.
  3. • An anonymous method can access and modify variables in scope when the anonymous method is defined. Variables manipulated in this way are known as captured outer variables. Be very careful with this feature!
Đồng bộ tài khoản