I've been reading the MSDN page on delegates and they seem straightforward. Then I was looking at some code that uses them and I saw this:
public delegate void NoArguments();
public NoArguments Refresh = null;
Refresh = new NoArguments( Reset );
It's that third line that confuses me. How can you new
a delegate? It is not an object, it's a method, or rather a delegate to a method. According to the example on the MSDN page, creating an instance of a delegate is through simple assignment, not allocaiton. Furthermore, why is the new
for the delegate taking a parameter, Reset
, when the delegate declaration takes no parameters?
delegate | American Dictionarya person chosen or elected by a group to represent the group, esp. at a meeting: Each state chooses delegates to the national convention. delegate. verb.
A delegate is a reference type variable that holds the reference to a method. The reference can be changed at runtime. Delegates are especially used for implementing events and the call-back methods. All delegates are implicitly derived from the System.
Delegation is simply passing a duty off to someone/something else. Delegation can be an alternative to inheritance. Delegation means that you use an object of another class as an instance variable, and forward messages to the instance.
Multiple methods can be assigned to the delegate using "+" or "+=" operator and removed using "-" or "-=" operator. It is called multicast delegate. If a multicast delegate returns a value then it returns the value from the last assigned target method. Delegate is used to declare an event and anonymous methods in C#.
The delegate
keyword indicates that what follows is essentially a function signature, so therefore, Refresh
becomes kind of like a pointer to a function that takes no arguments. However, to assign something to the Refresh
pointer, you have to give it a function to point to. In this case, it's the Reset
function. And further, the Reset
function must take no arguments.
In addition, the syntax:
Refresh = Reset;
is also valid, and is just syntactic sugar for the more formal syntax:
Refresh = new NoArguments(Reset);
in both cases, you could then execute the Reset function by calling Refresh:
Refresh();
Note, however, that if you execute Refresh()
without it having been assigned, then you could generate an exception. The way to prevent this would be to check it against null:
if (Refresh != null) Refresh();
else {
// Refresh was never assigned
}
You may think delegate is like a function type:
Declare type, function returns void and have no arguments:
public delegate void NoArguments();
Declare variable of given type and initialize it:
public NoArguments Refresh = null;
Assign new object to your variable. Object is actually a function Reset
, which must have the same signature as your delegate:
Refresh = new NoArguments( Reset );
UPDATE:
You may review the following link for more details: C# Delegates
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With