【问题标题】:C# Delegates. Casting not working for some reasonC# 代表。铸造由于某种原因无法正常工作
【发布时间】:2015-04-29 10:28:16
【问题描述】:

2 当我尝试构建此代码时出现错误。 第一个:“参数 2:无法从 double 转换为 int。” 第二个:“CalculatePay(double, int, Calculate) 的最佳重载方法有一些无效参数。 我不明白为什么我在 BankHolidayShift 和 NormalShift 方法中应用的转换不起作用。谢谢。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication8
{

    public delegate int Calculate(double val1, int val2);

    class PayCalculator
    {

        static double HourlyPay = 10;
        static int HoursPerShift = 8;

        static int NormalShift(double HourlyPay, int HoursPerShift)
        {
            return (int) HourlyPay * HoursPerShift;
        }

        static int BankHolidayShift(double HourlyPay, int HoursPerShift)
        {
            return (int)(HourlyPay * HoursPerShift) + 50;
        }

        public static int CalculatePay(double a, int b, Calculate calc)
        {

            int TotalPay = calc(a, b);
            return TotalPay;
        }

        static void Main()
        {
            Calculate calc = new Calculate(BankHolidayShift);

            int TotalPay =  CalculatePay(HourlyPay, HourlyPay, calc);
            Console.WriteLine("Total Pay for this shift is : {0}", TotalPay);
            Console.ReadLine();
        }


    }
}

【问题讨论】:

    标签: c# casting delegates


    【解决方案1】:

    你有int TotalPay = CalculatePay(HourlyPay, HourlyPay, calc);,显然是拼写,你应该有:

    int TotalPay =  CalculatePay(HourlyPay, HoursPerShift, calc);
    

    顺便说一句,局部变量以及方法参数应该是驼峰式。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-12-28
      • 1970-01-01
      • 1970-01-01
      • 2016-12-06
      • 2012-12-15
      • 1970-01-01
      • 1970-01-01
      • 2022-10-07
      相关资源
      最近更新 更多