Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delegates - does the delegate return type have to match the method it is delegating too?

Tags:

c#

delegates

I expect this to sound like an obvious question but, does the delegate return type have to match the return type of the method it is delegating too?

EG, like this:

public static void Save()
    {
        TS ts = new TS(SaveToDatabase);
    }

    public delegate void TS();

    private static void SaveToDatabase()
    { }

where this will never work

public static void Save()
    {
        TS ts = new TS(SaveToDatabase);
    }

    public delegate string TS();

    private static void SaveToDatabase()
    { }
like image 913
Dave Avatar asked Dec 10 '12 14:12

Dave


People also ask

Can delegates return type?

delegate: It is the keyword which is used to define the delegate. return_type: It is the type of value returned by the methods which the delegate will be going to call. It can be void. A method must have the same return type as the delegate.

Is a delegate that can hold the reference of a method that does not return any value?

The myDelegate is a user-defined name of the delegate that takes a single integer parameter and its return type is void that does not return any value.

What should be the return type of multicast delegate methods?

Multicast Delegates must have a return type of void Otherwise it will throw an exception.

What will happen if a delegate has a non void return type?

When the return type is not void as above in my case it is int. Methods with Int return types are added to the delegate instance and will be executed as per the addition sequence but the variable that is holding the return type value will have the value return from the method that is executed at the end.


3 Answers

Yes, It has to return the same type and have the same parameters. In other words, the function and the delegate declaration must have the same signature.

Example:

    //Declare delegate (return double with double param)
    public delegate double Squared(double x);

    public class Circle
    {
        private double _radius;


        public static double ValueTimesValue(double Value)
        {
            return Value * Value;
        }

        public double Area(Squared sqd)
        {
            return sqd(_radius) * Math.PI;
        }

        public void CircleCharacteristics()
        {
            Squared Sq = new Squared(ValueTimesValue);
        }
    }

EDIT: If you see the sample code, Squared Delegate and ValueTimesValue function have the same return type and parameters.

like image 83
Carlos Landeras Avatar answered Sep 22 '22 22:09

Carlos Landeras


From msdn:

A delegate lets you pass a function as a parameter. The type safety of delegates requires the function you pass as a delegate to have the same signature as the delegate declaration.

And another quote from C# specification:

A method and a delegate type are compatible if both of the following are true:

  • They have the same number or parameters, with the same types, in the same order, with the same parameter modifiers.
  • Their return types are the same.

I think it's very good description of compatibility conditions. And as you can see, your code violates second condition, which produces compiler error.

like image 29
Sergey Berezovskiy Avatar answered Sep 22 '22 22:09

Sergey Berezovskiy


In simple terms, a delegate is a template for a method (hope I don't get bashed too hard for the oversimplification). If you want a visualization, think of it like a lock, and the physical implementation is like a key. A key fits a certain lock and fails in a different lock. Just as a key won't fit in the wrong lock, a method that applies a different template (signature) fails.

So, yes, you need the right signature for the method you wish to "delegate work to". If you want to think more in software terms, a delegate is a contract for the physical implementation it represents, much like an interface is a contract for the actual methods it represents. They are very similar concepts.

like image 26
Gregory A Beamer Avatar answered Sep 22 '22 22:09

Gregory A Beamer