繁体   English   中英

为什么我的委托作为参数传递给方法时不起作用?

[英]Why does my delegate not work when passed in as a parameter to a method?

我创建了一个委托,我想使用委托作为方法参数列表中的参数。 我想要像在Main方法中那样完美地调用处理程序。

问题:如何将委托传递给方法?

码:

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

namespace ConsoleApplication1
{
    class Program
    {

        public delegate void Del(string e);
        Del handler = DelegateMethod;

        public static void DelegateMethod(string message)
        {
            System.Console.WriteLine(message);
            System.Console.ReadKey();
        }

        public void testDel(Del d)     <--Write Here!!!
        {
            d.handler("33");
        }


        static void Main(string[] args)
        {
            // Instantiate the delegate.
            // Del handler = DelegateMethod;

            Program p = new Program();
            p.handler("Hello World");    <--Like this example here (these work)
            p.handler("DisneyLand");
            p.handler("Cattle Wars");

            testDel(p.handler("Cattle Wars");
        }
    }
}

错误列表:

Error   1   The best overloaded method match for 'ConsoleApplication1.Program.testDel(ConsoleApplication1.Program.Del)' has some invalid arguments  C:\Users\itpr13266\AppData\Local\Temporary Projects\ConsoleApplication1\Program.cs  36  12  ConsoleApplication1
Error   2   Argument 1: cannot convert from 'void' to 'ConsoleApplication1.Program.Del' C:\Users\itpr13266\AppData\Local\Temporary Projects\ConsoleApplication1\Program.cs  36  20  ConsoleApplication1
Error   3   ) expected  C:\Users\itpr13266\AppData\Local\Temporary Projects\ConsoleApplication1\Program.cs  36  44  ConsoleApplication1

工作守则:

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

namespace ConsoleApplication1
{
    class Program
    {
        public delegate void Del(string e);
        Del handler = DelegateMethod;

        public static void DelegateMethod(string message)
        {
            System.Console.WriteLine(message);
            System.Console.ReadKey();
        }

        public void testDel(Del d)
        {
            d.Invoke("L");
        }

        static void Main(string[] args)
        {
            // Instantiate the delegate.
            // Del handler = DelegateMethod;

            Program p = new Program();
            p.handler("Hello World");
            p.handler("DisneyLand");
            p.handler("Cattle Wars");
            p.testDel(p.handler);
        }
    }
}

在线

testDel(p.handler("Cattle Wars"));

调用p.handler Del不再传入testDel 也许您在寻找:

testDel(p.handler);

这会传递Del以便testDel可以调用它。

您必须将委托传递给方法而不使用“Cattle Wars”参数

   testDel(p.handler);

那么你的方法应该如下所示。

    public void testDel(Del d)
    {
        d.Invoke("L");
    }

我不得不说,代表们对我来说仍然很奇怪。 我知道它们只是类型安全且线程安全的变量。 您可以像调用方法名一样使用变量。

    public delegate void Del(string e);
    Del handler = DelegateMethod;

在您的示例中,您的委托可以设置为任何返回类型为void的方法,并且接受一个字符串参数。

   Del handler = DelegateMethod;

上面是您声明委托的地方,但您也可以将委托声明为除了void和一个参数之外的任何方法作为字符串。

    handler = BaseBall;


     public void BaseBall(string a) 
     {
         //code here
     }

我会继续读你会随时学习。

暂无
暂无

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

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