.net delegate what is
Back to: C. Please read our previous article where we discussed Single Cast Delegates in C with examples. As part of this article, we are going to discuss the following pointers in detail. A Multicast Delegate in C is a delegate that holds the references of more than one function. When we invoke the multicast delegate, then all the functions which are referenced by the delegate are going to be invoked. If you want to call multiple methods using a delegate then all the method signatures should be the same.
Let us see an example for understanding the Multicast Delegate in C. Please have a look at the following example which is without using delegates. In the below example, we created two methods, and then from the main method, we are creating the instance of the class and then invoke the two methods.
In the above example, we created an instance of the Rectangle class and then called the two methods. Now I want to create a single delegate that is going to invoke the above two methods i. GetArea and GetPerimeter. The two methods having the same signature with the different method names, so we can create a single delegate that holds the reference of the above two methods.
And when we invoke the delegate, it is going to invoke the above two methods. In most cases, when we call a function, we specify the function to be called directly. WriteLine "Process begin" ; Console. That works well in most situations. Sometimes, however, we don't want to call a function directly - we'd like to be able to pass it to somebody else so that they can call it. This is especially useful in an event-driven system such as a graphical user interface, when I want some code to be executed when the user clicks on a button, or when I want to log some information but can't specify how it is logged.
The very basic Delegate. An interesting and useful property of a delegate is that it does not know or care about the class of the object that it references. Any object will do; all that matters is that the method's argument types and return type match the delegate's. This makes delegates perfectly suited for "anonymous" invocation. This declaration defines a delegate named SimpleDelegate , which will encapsulate any method that takes no parameters and returns no value. This declaration defines a delegate named ButtonClickHandler , which will encapsulate any method that takes two objects as parameters and returns an int.
A delegate will allow us to specify what the function we'll be calling looks like without having to specify which function to call. The declaration for a delegate looks just like the declaration for a function, except that in this case, we're declaring the signature of functions that this delegate can reference. A very basic example SimpleDelegate1. WriteLine "I was called by delegate Calling Static Functions.
For our next, more advanced example SimpleDelegate2. LogHandler Logger ; myClass. Calling Member Functions. In the simple example above, the Logger function merely writes the string out. A different function might want to log the information to a file, but to do this, the function needs to know what file to write the information to SimpleDelegate3. IO; namespace Akadia. Close ; fileStream. A delegate is an object which refers to a method or you can say it is a reference type variable that can hold a reference to the methods.
It provides a way which tells which method is to be called when an event is triggered. For example, if you click on a Button on a form Windows Form application , the program would call a specific method.
In simple words, it is a type that represents references to methods with a particular parameter list and return type and then calls the method in a program for execution when it is needed. Important Points About Delegates: Provides a good way to encapsulate the methods. Delegates are the library class in System namespace. These are the type-safe pointer of any method.
Delegates are mainly used in implementing the call-back methods and events. Delegates can be chained together as two or more methods can be called on a single event. Anonymous Methods C 2. Sometimes, these features together are known as anonymous functions. Declaration of Delegates Delegate type can be declared using the delegate keyword. Once a delegate is declared, delegate instance will refer and call those methods whose return type and parameter-list matches with the delegate declaration.
It can be void. A method must have the same return type as the delegate. Once a delegate is instantiated, a method call made to the delegate is pass by the delegate to that method. The parameters passed to the delegate by the caller are passed to the method, and the return value, if any, from the method, is returned to the caller by the delegate.
0コメント