@PersyJack When you pass a delegate as a parameter then your method trusts that a suitable delegate is passed and your method has no understanding of the delegate's value/logic it is decoupled
Following example demonstrates declaration instantiation and use of a delegate that can be used to reference methods that take an integer parameter and returns an integer value
Dettagli su
delegate c#
Can Be Used
The preceding delegate can be used to reference any method that has a single string parameter and returns an int type variable
Following example demonstrates declaration instantiation and use of a delegate that can be used to reference methods that take an integer parameter and returns an integer value
The delegate printString can be used to reference method that takes a string as input and returns nothing
The Print delegate shown above can be used to point to any method that has same return type & parameters declared with Print
Be Invoked
An instantiated delegate can be invoked as if it were the wrapped method itself
You don't want to evaluate the code now but only when needed so you register a delegate that can be invoked when the event occurs
The delegate can be invoked like a method because it is a reference to a method
The delegate can be invoked by two ways
Be Used
The Event
The source calls the delegate when the event occurs
The delegate then calls the event handling method on the recipient delivering the event data
Whichever is appropriate you assign the delegate to the event which effectively registers the method that will be called when the event fires
String Message
public delegate void TestDelegate string message
public delegate void Del string message
} } public delegate void NotifyDelegateWithMessage string message
That Can
The following example declares a delegate named that can encapsulate a method that takes a string as an argument and returns void
To The
All the OnEventX delegate to the methods the user defines
The Method
" people IsChild is what passing and finally within if the delegate will call the method
Here delegate object encapsulates the method functionalities and returns the result as we specified in the method
Method That
The following method from Listing 14 1 shows a delegate handler method that conforms to the signature of theComparer delegate
All you really need to do is define your callback method delegate handler method that is invoked when someone presses the clickMe button
And The
For use with anonymous methods the delegate and the code to be associated with it are declared together
The "+" operator adds a function to the delegate object and the " " operator removes an existing function from a delegate object
Is A Reference
A delegate is a reference type variable that holds the reference to a method
A delegate is a reference to a method
Reference Type
introdurre Reference Type
Today I’m going to introduce a new reference type called adelegate
As A Parameter
This allows a method to accept a delegate as a parameter and call the delegate at some later time
@PersyJack When you pass a delegate as a parameter then your method trusts that a suitable delegate is passed and your method has no understanding of the delegate's value/logic it is decoupled
Type And
A method can have a parameter of a delegate type and can invoke the delegate parameter
It is declared with the eventkeyword a delegate type and an event name
You Can
The OnClickMeClicked method shown below conforms to the signature of the EventHandler delegate which you can look up in the
The Same
The delegate and method have the same signature and parameter type
To A Method
A delegate can refer to a method which has the same signature as that of the delegate
Static Void
private static void NotifyIfComplete { Console
Create
To use a delegate you must create an instance of it
The Code
Passed To The
When creating a delegate the argument passed to the new expression is written similar to a method call but without the arguments to the method
When The
Derived From
Because delegate types are derived from the methods and properties defined by that class can be called on the delegate
The Answer
Will you please elaborate the answer of "Why"
The Methods
The Reference
It is a delegate which holds the reference of more than one method
An Object
Because the instantiated delegate is an object it can be passed as a parameter or assigned to a property
To Call
We use this delegate to call two methods the first prints the string to the console and the second one prints it to a file
Methods The
With The
Identify the class as a delegate with the "delegate" keyword
Reference To A Method
delegare Reference To A Method