C# - 将Delegate作为参数传递给方法

时间:2014-10-10 09:28:54

标签: c# .net exception delegates

我试图熟悉C#中委托的概念。到目前为止,我已经创建了这个控制台应用程序:

Program.cs的

using System;

namespace DelegatesTest
{
    class Program
    {
        static void Main(string[] args)
        {
            ArithmeticOperation.ArOpDel additionDelegate = new ArithmeticOperation.ArOpDel(ArithmeticOperation.Addition);
            Console.WriteLine(ArithmeticOperation.ConvertResultToString(additionDelegate(10, 5)));
            Console.ReadKey();
        }
    }
}

ArithmeticOperation.cs

using System;

namespace DelegatesTest
{
    public static class ArithmeticOperation
    {
        public delegate int ArOpDel(int x, int y);

        public static string ConvertResultToString(ArOpDel del)
        {
            string result = String.Format("The result of the {0} operation is {1}", del.Method.Name, del.ToString());
            return result;
        }

        public static int Addition(int x, int y)
        {
            return x + y;
        }

        public static int Subtraction(int x, int y)
        {
            return x - y;
        }

        public static int Multiplication(int x, int y)
        {
            return x * y;
        }

        public static int Division(int x, int y)
        {
            return x / y;
        }
    }
}

从代码中可以看出,我使用一个名为 ArithmeticOperation.cs 的静态类来执行一些算术运算。 ConvertResultToString 方法将其中一个算术运算方法作为参数,以便将结果优雅地显示为字符串。

不幸的是,我的代码无法编译。它给了我以下错误:

Argument '1': cannot convert from 'int' to 'DelegatesTest.ArithmeticOperation.ArOpDel'

这一行给出了错误:

Console.WriteLine(ArithmeticOperation.ConvertResultToString(additionDelegate(10, 5)));

我能帮助我解决这个问题,因为我对代表的经验不是很充实吗?事实上,我已创建此应用程序以了解委托。

5 个答案:

答案 0 :(得分:1)

additionDelegate(10, 5)将通过算术运算返回int,它不会返回deleagate编译错误的原因。

答案 1 :(得分:1)

Console.WriteLine(ArithmeticOperation.ConvertResultToString(additionDelegate(10, 5)));

你没有在这里传递代表。您正在调用,这会产生int

这将通过委托:

Console.WriteLine(ArithmeticOperation.ConvertResultToString(additionDelegate));

你需要在某个地方实际调用委托。

答案 2 :(得分:1)

当你这样做时

 Console.WriteLine(ArithmeticOperation.ConvertResultToString(additionDelegate(10, 5)));

你强迫编译器执行该方法并返回结果以将其作为参数传递,在这种情况下它是一个int而ConvertResultTostring期望一个委托 这里如何在不使用泛型

的情况下使其工作
 class Program
    {
        static void Main(string[] args)
        {
            ArithmeticOperation.ArOpDel additionDelegate = ArithmeticOperation.Addition;

            Console.WriteLine(ArithmeticOperation.ConvertResultToString(additionDelegate,5,6));
            Console.ReadKey();
        }
    }
    public static class ArithmeticOperation
    {
        public delegate int ArOpDel(int x, int y);

        public static string ConvertResultToString(ArOpDel del,int x, int y )
        {
            string result = String.Format("The result of the {0} operation is {1}", del.Method.Name, del(x,y).ToString());
            return result;
        }

        public static int Addition(int x, int y)
        {
            return x + y;
        }

        public static int Subtraction(int x, int y)
        {
            return x - y;
        }

        public static int Multiplication(int x, int y)
        {
            return x * y;
        }

        public static int Division(int x, int y)
        {
            return x / y;
        }
    }

答案 3 :(得分:0)

additionDelegate(10, 5)additionDelegate的调用,它实际上是Addition函数。所以,结果是一个整数。

为了传递委托,您应该传递additionDelegate本身而不是additionDelegate(10, 5)。但是,在这种情况下,除非您在那里调用操作,否则ConvertResultToString中将无法使用该操作的结果。

public static string ConvertResultToString(ArOpDel del)
{
     string result = String.Format("The result of the {0} operation is {1}", del.Method.Name, del(10, 5).ToString());
     return result;
}

...

Console.WriteLine(ArithmeticOperation.ConvertResultToString(additionDelegate));

答案 4 :(得分:0)

additionDelegate(10, 5)表示调用委托,结果是一个与ConvertResultToString方法存根不匹配的int类型。 你需要改变

Console.WriteLine(ArithmeticOperation.ConvertResultToString(additionDelegate(10, 5)))

Console.WriteLine(ArithmeticOperation.ConvertResultToString(additionDelegate))

并更改

string result = String.Format("The result of the {0} operation is {1}", del.Method.Name, del.ToString()); 

string result = String.Format("The result of the {0} operation is {1}", del.Method.Name, del(10,5).ToString());