Delegate why use




















The method can be either static or an instance method. This flexibility means you can programmatically change method calls, or plug new code into existing classes. In the context of method overloading, the signature of a method does not include the return value. But in the context of delegates, the signature does include the return value. In other words, a method must have the same return type as the delegate.

This ability to refer to a method as a parameter makes delegates ideal for defining callback methods. You can write a method that compares two objects in your application. That method can be used in a delegate for a sort algorithm.

Because the comparison code is separate from the library, the sort method can be more general. This is known as an asynchronous callback, and is a common method of notifying a caller when a long process has completed. When a delegate is used in this fashion, the code using the delegate does not need any knowledge of the implementation of the method being used.

The functionality is similar to the encapsulation interfaces provide. Another common use of callbacks is defining a custom comparison method and passing that delegate to a sort method. It allows the caller's code to become part of the sort algorithm. The following example method uses the Del type as a parameter:. Using the delegate as an abstraction, MethodWithCallback does not need to call the console directly—it does not have to be designed with a console in mind.

What MethodWithCallback does is simply prepare a string and pass the string to another method. This is especially powerful since a delegated method can use any number of parameters. When a delegate is constructed to wrap an instance method, the delegate references both the instance and the method. A delegate has no knowledge of the instance type aside from the method it wraps, so a delegate can refer to any type of object as long as there is a method on that object that matches the delegate signature.

When a delegate is constructed to wrap a static method, it only references the method. Consider the following declarations:. Along with the static DelegateMethod shown previously, we now have three methods that can be wrapped by a Del instance. A delegate can call more than one method when invoked. For example, consider the following generic delegate that is used for int and string parameters. Delegate is also used to declare an Event and an Anonymous Method. Learn more about delegate here.

C ASP. Skill Tests ASP. Delegate Syntax. Example: Declare a Delegate. Example: Set Delegate Target. Example: Invoke a Delegate. Invoke "Hello World! WriteLine "Called ClassA. WriteLine "Called ClassB. Try it. Net Action delegate's return type is void.

Remember that the invoke method accepts delegate as argument and delegate encapsulates methods, so here inside Invoke method I am passing an anonymous method which will get invoked when action is called which executes the anonymous method and calls the Process method of FrequentCustomer class. Purpose and Use of Delegate. Ishoo Anyal Updated date Aug 11,



0コメント

  • 1000 / 1000