-2

How can I solve this? How can I point to the exact overload that I would like to use?

    int A()
    {
        if (Environment.TickCount == 666)
            return 0;
        else
            return 1;
    }

    bool A()
    {
        if (A() == 0) //here's an error!
            return false;
        else
            return true;
    }
  • 1
    Overloads that differ only from the result type if not possible in C#. – Cédric Bignon Feb 03 '13 at 15:09
  • What exactly are you trying to do? – Shadow The GPT Wizard Feb 03 '13 at 15:15
  • The error is **The call is ambiguous between the following methods or properties**. The only error I got! Declaring duplicate functions with different return values caused no error! So that's why I was confused, and belived that there's a way to point to exact function. – user2037342 Feb 03 '13 at 15:25
  • If only you searched for "c# member overloading", you'd have found the answer: `Changing the return type of a method does not make the method unique as stated in the common language runtime specification. You cannot define overloads that vary only by return type.` – Nolonar Feb 03 '13 at 15:38

2 Answers2

2

When you provide multiple methods with the same name, the method is said to be overloaded. You cannot overload methods on return type in C#: you need to give them different names or different parameter types. In your case, changing the name of the bool A to bool B will fix the problem:

int A()
{
    if (Environment.TickCount == 666)
        return 0;
    else
        return 1;
}

bool B()
{
    if (A() == 0) //No error here
        return false;
    else
        return true;
}
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
0

@dasblinkenlight is right. It is also good practice to name your functions descriptive names.

example: bool IsEnvironmentTickCount666(). The Is in the function name makes it easy for someone reading your code to see you are returning a bool

SneakyPeet
  • 607
  • 5
  • 14