I have an error:
Error CS0121 The call is ambiguous between the following methods or properties: 'Program.calculateFee(double, int)' and 'Program.calculateFee(double)' DailyRate.
here is my code:
void run()
{
double fee = calculateFee();
Console.WriteLine("Fee is {0}", fee);
}
private double calculateFee(double theDailyRate = 500.0, int no0fdays = 1)
{
Console.WriteLine("calculateFee using two optional parameters");
return theDailyRate * no0fdays;
}
private double calculateFee(double dailyRate = 500.0)
{
Console.WriteLine("calculateFee using one optional parameter");
int defaultNo0fDays = 1;
return dailyRate * defaultNo0fDays;
}
private double calulateFee()
{
Console.WriteLine("calculateFee using hardcoded values");
double defaultDailyRate = 400.0;
int defaultNo0fDays = 1;
return defaultNo0fDays * defaultDailyRate;
}
}
}
edit: why does this one do work??
static void Main(string[] args)
{
(new Program()).run();
}
void run()
{
double fee = calculateFee();
Console.WriteLine("Fee is {0}", fee);
}
private double calculateFee(double theDailyRate = 500.0, int noOfDays = 1)
{
Console.WriteLine("calculateFee using two optional parameters");
return theDailyRate * noOfDays;
}
private double calculateFee(double dailyRate = 500.0)
{
Console.WriteLine("calculateFee using one optional parameter");
int defaultNoOfDays = 1;
return dailyRate * defaultNoOfDays;
}
private double calculateFee()
{
Console.WriteLine("calculateFee using hardcoded values");
double defaultDailyRate = 400.0;
int defaultNoOfDays = 1;
return defaultDailyRate * defaultNoOfDays;
}
EDIT
The second example works because you have fixed the misspelling of calulateFee()
, allowing the compiler to bind to the method that exactly matches the call (no parameters).
When calculateFee
was misspelled, the compiler did not find a method that exactly matched the name and parameters, so it began searching for appropriate overloads. It find two - one with one optional parameter and one with two. The overload resolution rules do not allow the compiler to pick one method over the other, so it results in a build error.
From MSDN:
- If more than one candidate is found, overload resolution rules for preferred conversions are applied to the arguments that are explicitly specified. Omitted arguments for optional parameters are ignored.
- If two candidates are judged to be equally good, preference goes to a candidate that does not have optional parameters for which arguments were omitted in the call. This is a consequence of a general preference in overload resolution for candidates that have fewer parameters.
Since the last method exactly matches your calling signature, it is chosen. When it was misspelled, two other "equally good" candidates were found. Since optional parameters are ignored, there are no rules to dictate that one overload be chosen over another.
ORIGINAL ANSWER
You have one method with an optional double
parameter and another method with an optional double
parameter and an optional int
parameter. When you call it with no parameters the compiler doesn't know if you want to call the method with no parameters, with just the double
parameter or with the double
and int
parameters. You have some options to fix this:
Either use optional parameters or use overloads; there's no reason to use both.