locked
Overloading A Generic Method RRS feed

  • Question

  • User1231829591 posted

    Hi all, currently I have the following generic method which takes two parameters of the same type:

    public T AddNumbers<T>(T a, T b)
            {
                dynamic num1 = a;
                dynamic num2 = b;
                return num1 + num2;
            }

    I would like to overload the method above so that it will take two parameters of differing types. I have tried the following but it does not work.

    public T AddNumbers<S,T>(S a, T b)
            {
                dynamic num1 = a;
                dynamic num2 = b;
                return num1 + num2;
            }

    I get the following errors

    The call is ambiguous between the following methods or properties: 'AddNumbers<int>(int, int)' and 'AddNumbers<int,int>(int, int)'
    
    The call is ambiguous between the following methods or properties: 'AddNumbers<double>(double, double)' and 'AddNumbers<double,double>(double, double)'
    

    Thanks in advance.

    Thursday, December 3, 2015 11:06 PM

Answers

  • User614698185 posted

    Hi ManyTitles,

    This is about overloaded methods pass value to delegates:

    static void Main(string[] args)
    {
        D1 d = new D1(M1);
        d();
        Console.Read();
    }
    public delegate void D1();
    
    public static void M1()
    {
        Console.WriteLine("M1");
    }
    
    public static void M1(int p1)
    {
        Console.WriteLine("M1 with p1");
    }

    This is about overloaded Generic methods pass value to Generic delegates:

    public static void Main()
    {
        D1<int> d = new D1<int>(M1<int>);
        d();
        Console.Read();
    }
    
    public delegate void D1<T>();
    
    public static void M1<T>()
    {
        Console.WriteLine("M1");
    }
    
    public static void M1<T>(T p1)
    {
        Console.WriteLine("M1 with p1");
    } 

    The above codes are right usage. Incorrect usage usually violate this principle ambiguity. If you write a program even if the compiler does not know how to execute it, it will  certainly be an error.

    Best Regards,

    Candice Zhou

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, December 4, 2015 7:54 AM

All replies

  • User2103319870 posted

    ManyTitles

    The call is ambiguous between the following methods or properties: 'AddNumbers<int>(int, int)' and 'AddNumbers<int,int>(int, int)' The call is ambiguous between the following methods or properties: 'AddNumbers<double>(double, double)' and 'AddNumbers<double,double>(double, double)'

    Try calling the method by passing the datatype value like below

     Test objtext = new Test();
                    int t1 = objtext.AddNumbers<int>(1, 2);
                    double t2 = objtext.AddNumbers<double,double>(1, 2);
    Thursday, December 3, 2015 11:55 PM
  • User1574164112 posted

    These all worked for me without any errors. 

            protected void Button1_Click(object sender, EventArgs e)
            {
                var x = AddNumbers<int>(2, 9);
    
                var a = AddNumbers<int,double>(2, 9.0);
    
                var b = AddNumbers<int, int>(2, 2);
    
                var c = AddNumbers<double, double>(9.0, 9.0);
    
            }

    The only thing I had to make sure was that the the return type was compatible with the calculation.  e,g this would not work because adding a double and an int returns a double but the second parameter is the type (int) that is returned.

    var a = AddNumbers<double, int >(9.0, 2);

    I was using .Net 4.5, what version are you using?

    Friday, December 4, 2015 12:02 AM
  • User1574164112 posted

    As a2h says, make sure you specify the data types

    This will give a ambiguous error

    var a = AddNumbers(2, 2);

    Friday, December 4, 2015 12:09 AM
  • User1231829591 posted

    Hi all, I thought that by overloading a generic function I can overload a delegate simply by passing the overloaded method's name to it (the delegate) but I get the errors I posted above. So my question is can I even use overloaded methods with delegates.

    Friday, December 4, 2015 12:22 AM
  • User614698185 posted

    Hi ManyTitles,

    This is about overloaded methods pass value to delegates:

    static void Main(string[] args)
    {
        D1 d = new D1(M1);
        d();
        Console.Read();
    }
    public delegate void D1();
    
    public static void M1()
    {
        Console.WriteLine("M1");
    }
    
    public static void M1(int p1)
    {
        Console.WriteLine("M1 with p1");
    }

    This is about overloaded Generic methods pass value to Generic delegates:

    public static void Main()
    {
        D1<int> d = new D1<int>(M1<int>);
        d();
        Console.Read();
    }
    
    public delegate void D1<T>();
    
    public static void M1<T>()
    {
        Console.WriteLine("M1");
    }
    
    public static void M1<T>(T p1)
    {
        Console.WriteLine("M1 with p1");
    } 

    The above codes are right usage. Incorrect usage usually violate this principle ambiguity. If you write a program even if the compiler does not know how to execute it, it will  certainly be an error.

    Best Regards,

    Candice Zhou

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, December 4, 2015 7:54 AM