Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is this delegate method doing?

Tags:

c#

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?

like image 480
johnbakers Avatar asked Jul 09 '13 03:07

johnbakers


People also ask

How do you define a delegate?

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.

What is a delegate and how do you create a delegate?

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.

What is delegate in Java?

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.

How do you add a delegate method?

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#.


2 Answers

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
}
like image 115
Michael Bray Avatar answered Sep 22 '22 03:09

Michael Bray


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

like image 30
Y.Yanavichus Avatar answered Sep 23 '22 03:09

Y.Yanavichus