双精度和整数错误 CS0121

Error CS0121 with double and int

我有一个错误:

Error CS0121 The call is ambiguous between the following methods or properties: 'Program.calculateFee(double, int)' and 'Program.calculateFee(double)' DailyRate.

这是我的代码:

     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;
        }
    }
}

编辑:为什么这个有效?

 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; 
    }

编辑

第二个示例有效,因为您修复了 calulateFee() 的拼写错误,允许编译器绑定到 完全 匹配调用(无参数)的方法。

calculateFee拼写错误时,编译器没有找到完全匹配名称和参数的方法,因此它开始寻找合适的重载。它找到两个 - 一个带有一个可选参数,一个带有两个。重载决议规则不允许编译器选择一种方法而不是另一种方法,因此会导致构建错误。

来自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.

由于最后一种方法与您的调用签名完全匹配,因此选择了它。当它拼写错误时,找到了另外两个 "equally good" 候选人。由于可选参数被忽略,因此没有规则规定选择一个重载而不是另一个重载。

原始答案

您有一个方法带有一个可选的 double 参数,另一个方法带有一个可选的 double 参数和一个可选的 int 参数。当你不带参数调用它时,编译器不知道你是想调用不带参数的方法,只带 double 参数还是带 doubleint 参数。您有一些选项可以解决此问题:

要么使用可选参数,要么使用重载;没有理由同时使用两者。

原因是在运行()中你调用了calculateFee()。

由于这两种方法都只有可选参数,因此它不知道您要调用的是哪一个(因此不明确)。没有任何参数可以调用任何没有声明没有参数或只有可选参数的方法。

可选参数表示它们可以包含在调用中,也可以不包含在调用中。这些方法应该结合到最上面的一个 calculateFee(double theDailyRate = 500.0, int noOfDays = 1).

    private double calculateFee(double theDailyRate = 500.0, int no0fdays = 1){
        ...
    }

    private double calculateFee(double dailyRate = 500.0)
    {
        ...
    }