0

Getting the ambiguous call as arrangement of parameters are different: short,int / int,short /byte,int / int,byte

As Function signature is:

1.Number of arguments/parameters

2.Type of arguments/parameters

3.Arrangement of arguments/parameters

Why the call is ambiguous ? should it belongs to the similar type ?...

code:

class Program
{
    static void Main(string[] args)
    {

        test abbb = new test();
        //abbb.add(2.2f,1);
        // abbb.add(2,2.2f);
        abbb.add(255,1);
        abbb.add(1,256);
        Console.ReadLine();
    }
}

class test
{
    public int add(byte i , int f) {
       return i + f;
    }
    public int add(int i, byte f)
    {
        return i + f;
    }
    public int add(short i, int f)
    {
        return i + f;
    }
    public int add(int i, short f)
    {
        return i + f;
    }
}
  • Can you step through the process of what you would think should be the selected overload? You can do this with the C# language specification if you want to get your answer as well. – Jeroen Vannevel Feb 16 '16 at 20:17
  • Look at what happens if you add a method that takes two ints. Now there is no error because the two integer constants have a perfect matching target. Missing that the compiler can only find the two methods that takes,as first paramater an integer, but then it cannot decide if it should convert the second parameter to a byte or to a short – Steve Feb 16 '16 at 20:17

3 Answers3

3

By default, any 'Magic Number' will be treated as an Integer, but sometimes for ease of use the compiler can convert to another number format implicitly if it has enough information to do so. In order to get around the ambiguous calls, you would be best explicitly defining typed variables for the numbers first, then passing them into the functions to remove any ambiguity.

Community
  • 1
  • 1
MikeDub
  • 5,143
  • 3
  • 27
  • 44
0
class Program
   {       
    static void Main(string[] args)
    {           
        test abbb = new test();

        Console.WriteLine(abbb.add(32767, 32770)); //short , int
        Console.WriteLine(abbb.add(32770, 32767)); //int ,short
        Console.WriteLine(abbb.add(255, 32770)); // byte,int
        Console.WriteLine(abbb.add(32770, 255)); // int,byte

        Console.ReadLine();
    }
}
class test
{       
    public int add(byte f, int i)
    {
        return i + f;
    }

    public int add(int i, byte f)
    {
        return i + f;
    }
    public int add(short i, int f)
    {
        return i + f;
    }
    public int add(int f, short i)
    {
        return i + f;
    }
}

No ambiguity due to mentioned specific range of types...

-3

It is ambiguous because C# does not know which of the two numbers is which of the types. Both 1 and 256 may a be a short and both may be an int. You may use explicit casting to "choose" one of the methods.

  • 3
    Incorrect -- C# knows very well that both literals are integers. – Jeroen Vannevel Feb 16 '16 at 20:12
  • @JeroenVannevel Yes, it knows both are INTs. BUT there is no function with a (int, int) signature so the compiler tries to implicity convert one of the values. Because both may be casted to a short C# does not know which to call. – user5055454 Feb 17 '16 at 06:20