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

c#, delegates

Solution

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

Problem

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() { } ```

Original source