Click here to Skip to main content
14,696,499 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I declared a delegate using integer and methods using double and I get an error on main method where I instantiated the delegate object.how do I fix this?

What I have tried:

class Program
    {
        //creating a delegate Converter
        delegate int Converter(int value);

        //method to calculate Fahrenheit to Celsius
        static double ToCel(int fah)
        {
            return (fah - 32) / 1.8;
        }
        //method to callculate Celsius to Fahrenheit
        static double  ToFah(int celsius)
        {
            return celsius * 1.8 + 32;
        }
        //the method to calculate Celsius to Kelvin
        static double  ToKel(int celsius2)
        {
            return celsius2 + 273.15;
        }
        //the method to calculate celsius to Rankine
        static double ToRank(int celsius3)
        {
            return (celsius3 + 273.15) * 1.8;
        }
 static void Main(string[] args)
        {
            //institiating object for delegate

            Converter process = ToCel;
            Converter process1 = ToCel;
            Converter process2 = ToKel;
            Converter process3 = ToKel;
}
}
Posted
Updated 28-Feb-18 2:29am
v2

Change
delegate int Converter(int value);
to
delegate double Converter(int value);

The signature of the delegate has to match the signature of the method(s) you intend to represent.
   
Comments
Richard Deeming 28-Feb-18 12:19pm
   
Technically, it doesn't always have to be an exact match. :)

Using Variance in Delegates (C#) | Microsoft Docs[^]
phil.o 28-Feb-18 14:28pm
   
Oh! Something new to learn.
Thanks Richard :)
correction
delegate double Converter(int value);
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900