C#问题:代表们

时间:2009-10-27 01:41:00

标签: c# delegates

我一直在网上搜索,包括MSDN.com,以便在c#中对代表进行易于理解的解释。有很多教程/课程......但这对我来说是一个难以理解的概念。所以我想我会问其他程序员。谁能解释一下?

5 个答案:

答案 0 :(得分:5)

代理喜欢函数指针。

看看这个

using System;

namespace Akadia.BasicDelegate
{
    // Declaration
    public delegate void SimpleDelegate();

    class TestDelegate
    {
        public static void MyFunc()
        {
            Console.WriteLine("I was called by delegate ...");
        }

        public static void Main()
        {
            // Instantiation-- we set this simpleDelegate to MyFunc
            SimpleDelegate simpleDelegate = new SimpleDelegate(MyFunc);

            // Invocation-- MyFunc is called. 
            simpleDelegate();
        }
    }
}

所以当您拨打simpleDelegate时,会调用MyFunc,因为simpleDelegate指向MyFunc

如果有疑问,您应该复制上面的代码,将其粘贴到VS中,然后通过它运行调试器。检查程序如何从一个地方流向另一个地方;在调用simpleDelegate()行后自己查看,程序跳转到MyFunc并从那里执行。检查simpleDelegate变量,看它是否包含对MyFunc方法的引用。这将是您熟悉整个委托事务的最佳方式。

答案 1 :(得分:1)

基本上,委托会运行您订阅的任意数量的方法。它们必须匹配参数和返回类型(这称为“签名”),也就是说,如果您的委托采用两个整数并返回一个void:

public delegate void MyDelegate(int i1, int i2);

您订阅它的方法必须使用两个字符串并返回一个空格:

public void Add(int int1, int int2)
{
    MessageBox.Show((int1 + int2).ToString());
}

public void Multiply(int int1, int int2)
{
    MessageBox.Show((int1 * int2).ToString());
}

现在订阅,运行并查看结果:

public void SubscribeAndRun()
{
    MyDelegate d = new MyDelegate(Add);

    d += Multiply;

    d.Invoke(2, 3);
}

代理广泛用于在事件发生时调用其他方法。在C#事件中,封装的委托使用add和remove(添加或删除事件在触发时将运行的方法)。

答案 2 :(得分:0)

有关优秀教程,请参阅Delegates

  

委托是引用a的类型   方法。一旦代表被分配了一个   方法,它的行为完全一样   方法。委托方法可以是   像任何其他方法一样使用   参数和返回值,如   这个例子:

答案 3 :(得分:0)

这是一个简单的例子

using System;

namespace delegates
{
    class Program
    {
        // An event which you set up by attaching handlers (delegates)
        // and then running using the Invoke() method.
        private EventHandler<EventArgs> _event;

        void SetupFoo()
        {
            // Attach the function foo to the _event
            _event += Foo;
        }

        void SetupBar()
        {
            // Attach a delegate (aka anonymous method) to the _event
            _event += delegate { Console.WriteLine("Bar is called"); };
        }

        void SetupBaz()
        {
            // Attach a lambda to the _event
            _event += (sender, e) => Console.WriteLine("Baz is called");
        }

        void Fire()
        {
            // Run all the attached methods/delegates/lambdas
            _event.Invoke(this, EventArgs.Empty); 
        }


        static void Main(string[] args)
        {
            var prog = new Program();
            prog.SetupFoo();
            prog.SetupBar();
            prog.SetupBaz();
            prog.Fire();
            Console.ReadKey();
        }

        private static void Foo(object sender, EventArgs e)
        {
            Console.WriteLine("Foo is called");
        }
    }
}

这会产生输出:

Foo is called
Bar is called
Baz is called

为什么代表很重要?因为Fire()只负责事件执行的时间,但其他函数负责事件的后果。这些后果表示为代表。

答案 4 :(得分:-1)

思考Functors。即可以像对象(即指针)那样处理的函数