C# - 匿名委托

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1748719/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 20:28:07  来源:igfitidea点击:

C# - Anonymous delegate

c#delegates

提问by user160677

Like Anonymous Methods ,the delegates i am declaring down using "delegate" keyword are anonymous delegates?

像匿名方法一样,我使用“delegate”关键字声明的委托是匿名委托?

namespace Test
{
    public delegate void MyDelegate();
    class Program
    {
        static void Main(string[] args)
        {
            DelegateTest tst = new DelegateTest();
            tst.Chaining();
            Console.ReadKey(true);
        }
    }

    class DelegateTest
    {
        public event MyDelegate del;

        public void Chaining()
        {
            del += delegate { Console.WriteLine("Hello World"); };
            del += delegate { Console.WriteLine("Good Things"); };
            del += delegate { Console.WriteLine("Wonderful World"); };
            del();
        }
    }
}

采纳答案by Brian Rasmussen

Your delegate collection in the example points to a number of anonymous methods. A delegate is "just a method pointer". It doesn't matter if it points to a real method or an anonymous method.

示例中的委托集合指向许多匿名方法。委托“只是一个方法指针”。它指向真实方法还是匿名方法并不重要。

Please see http://msdn.microsoft.com/en-us/library/0yw3tz5k(VS.80).aspx

请参阅http://msdn.microsoft.com/en-us/library/0yw3tz5k(VS.80).aspx

回答by Lucero

Yes, they are anonymous delegates (or, more specific, delegates calling an anonymous method).

是的,它们是匿名委托(或者更具体地说,是调用匿名方法的委托)。

回答by barkmadley

Yes. Anonymous delegates cannot be referred to directly by name, so using the delegate(){}syntax means they are anonymous.

是的。匿名委托不能通过名称直接引用,因此使用delegate(){}语法意味着它们是匿名的。

回答by Vilx-

They are delegates to anonymous methods. This is one way to make anonymous methods, which was available since .NET 2.0. With .NET 3.0 you can also use lambda expressions which are simpler to write (but compile to the same code). I suppose that's what you meant with "anonymouse methods". But really, they are one and the same thing.

它们是匿名方法的委托。这是创建匿名方法的一种方法,自 .NET 2.0 起可用。在 .NET 3.0 中,您还可以使用更易于编写的 lambda 表达式(但可以编译为相同的代码)。我想这就是你所说的“匿名方法”。但实际上,它们是一回事。

回答by Greg D

That's correct, you have assigned a number of anonymous methods to an event.

没错,您已经为一个事件分配了许多匿名方法。

If you're using a newer version of c#, you can also do something similar with lambdas. for example:

如果您使用的是较新版本的 c#,您还可以使用 lambdas 执行类似的操作。例如:

class DelegateTest
{
    public event Action del;

    public void Chaining()
    {
        del += () => Console.WriteLine("Hello World");
        del += () => Console.WriteLine("Good Things");
        del += () => Console.WriteLine("Wonderful World");
        del();
    }
}

回答by Jon Skeet

There's no such thing as an "anonymous delegate" (or rather, that's not a recognised term in the C# specification, or any other .NET-related specification I'm aware of).

没有“匿名委托”这样的东西(或者更确切地说,这不是 C# 规范或我知道的任何其他 .NET 相关规范中公认的术语)。

There are anonymous functions which include anonymous methods and lambda expressions.

有匿名函数,其中包括匿名方法和 lambda 表达式。

Your code shows plain old anonymous methods - although they areusing the one feature lambda expressions don't have: the ability to not express the parameters at all when you don't care about them.

你的代码所示普通的老式匿名方法-尽管它们使用一个功能lambda表达式没有:不表达所有的参数,当你不关心他们的能力。

回答by pero

Your delegate is not anonymous. It's called MyDelegate. Delegate in CLR is a class that derives from System.MulticastDelegate and in your case it's called MyDelegate. You cannot directly derive from MulticastDelegate, C# compiler will stop you.

您的代表不是匿名的。它被称为 MyDelegate。CLR 中的委托是一个派生自 System.MulticastDelegate 的类,在您的情况下,它称为 MyDelegate。你不能直接从 MulticastDelegate 派生,C# 编译器会阻止你。

In your code when you assign delegates to del, the type/name of the delegate is inferred by compiler because you declared del as event of type MyDelegate.

在您将委托分配给 del 时的代码中,委托的类型/名称由编译器推断,因为您将 del 声明为 MyDelegate 类型的事件。