简体   繁体   English

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

[英]C# - Passing Delegate as a parameter to a method

I am trying to get familiar with the concept of delegates in C#. 我试图熟悉C#中代理的概念。 I have created this console application so far: 到目前为止,我已创建此控制台应用程序

Program.cs 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 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;
        }
    }
}

As you can see from the code, I am using a static class called ArithmeticOperation.cs to perform some arithmetic operations. 从代码中可以看出,我使用一个名为ArithmeticOperation.cs的静态类来执行一些算术运算。 The ConvertResultToString method takes one of the arithmetic operations methods as a parameter in order to display the result elegantly as a string. ConvertResultToString方法将其中一个算术运算方法作为参数,以便将结果优雅地显示为字符串。

Unfortunately, my code does not compile. 不幸的是,我的代码没有编译。 It is giving me the following error: 它给了我以下错误:

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

The error is being given on this line: 错误发生在这一行:

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

Can you please help me solve this problem as I am not very experienced with delegates? 能不能帮助我解决这个问题,因为我对代表不是很有经验吗? In fact, I have created this application to learn about delegates. 事实上,我已创建此应用程序以了解委托。

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

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

You are not passing the delegate here. 你没有在这里传递代表。 You are calling it, which results in an int . 你正在调用它,这会产生一个int

This would pass the delegate: 这将通过委托:

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

You would need to actually call the delegate somewhere though. 你需要在某个地方实际调用委托。

when you do this 当你这样做

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

you are forcing the compiler to execute the method and return the result to pass it as an argument which in this case it's an int while the ConvertResultTostring expect a delegate here how you can get it work without using generic 你强迫编译器执行该方法并返回结果以将其作为参数传递,在这种情况下它是一个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;
        }
    }

additionDelegate(10, 5) is an invocation of additionDelegate which actually is an Addition function. additionDelegate(10, 5)是的调用additionDelegate这实际上是一个Addition功能。 So, the result is an integer. 所以,结果是一个整数。

In order to pass a delegate you should pass additionDelegate itself instead of additionDelegate(10, 5) . 为了传递委托,你应该传递additionDelegate本身而不是additionDelegate(10, 5) But, in this case, the result of the operation will not be available in ConvertResultToString unless you invoke it there. 但是,在这种情况下,除非您在那里调用它,否则在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));

additionDelegate(10, 5) means invoke the delegate,the result is an int type which does not match the ConvertResultToString method stub. additionDelegate(10, 5) ConvertResultToString additionDelegate(10, 5)表示调用委托,结果是一个与ConvertResultToString方法存根不匹配的int类型。 you need change 你需要改变

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

to

Console.WriteLine(ArithmeticOperation.ConvertResultToString(additionDelegate))

and change 并改变

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

to

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM