I have a class with a static method, like so:
public class Calculator
{
public static decimal Calculate(decimal op1, decimal op2) => op1 * op2;
}
I add an instance method to that class which calls that static method:
public decimal Foo(decimal op1, decimal op2)
{
return Calculate(op1, op2);
}
This compiles just fine. Now I add another instance method which calls that static method inside a local function:
public decimal Bar((decimal op1, decimal op2) pair)
{
return Calculate();
decimal Calculate()
{
return Calculate(pair.op1, pair.op2);
}
}
This gives the compile error
CS1501 No overload for method 'Calculate' takes 2 arguments
If I change the call to return Calculator.Calculate(pair.op1, pair.op2) or give the
local function a different name it compiles without error. Why?
I believe that the problem is the nested
Calculate()function having the same name as the static methodCalculate(decimal, decimal). The compiler thinks that you want to recursively call the nested function and finds out that it does not accept two arguments.I would suggest you use the class name anyway when calling a static method, as it belongs to the type itself rather than to a specific instance.
This question seems related: Static and Instance methods with the same name?