在C#中的代表 [英] Delegates in C#

查看:127
本文介绍了在C#中的代表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的有在了解了一些麻烦如何在C#中的工作委托。我有很多代码示例,但我仍然不能正确掌握它。

I`m having some trouble in understanding how delegates in C# work. I have many code examples, but i still could not grasp it properly.

有人可以解释给我简单的英语?当然! 。代码示例会有所帮助,但我想我需要更多的是如何/为什么它的工作原理说明

Can someone explain it to me in "plain english"? Of course! examples of code will help, but i think i need more of a description of how/why it works.

编辑:

那么,问题是:为什么代表工作?什么是整个过程的流程图?

Well, the question is: why does delegates work? What is a "flow chart" of the entire process?

什么是使用委托?

我希望这使得问题更加清晰。

I hope this makes the question clearer.

推荐答案

要思考一个代表一种方式是像的参考功能的。例如,假设你在一个窗口中的按钮,你要点击该按钮时发生的事情。您可以委托附加到按钮的Click事件,每当用户点击这个按钮,你的函数将被执行。

One way to think about a delegate is like a reference to a function. For example, say you have a button in a window, and you want something to happen when the button is clicked. You can attach a delegate to the Click event of the button, and whenever the user clicks this button, your function will be executed.

class MyWindow : Window
{
    Button _button;

    public MyWindow()
    {
        _button = new Button();
        // place the button in the window
        _button.Click += MyWindow.ButtonClicked;
    }

    static void ButtonClicked(object sender, RoutedEventArgs e)
    {
        MessageBox.Show("Button Clicked");
    }
}

请注意我做如何ButtonClicked静态函数 - 我想做一个关于非静态函数下一个点。假设而不是ButtonClicked是一个非静态成员:

Notice how I make ButtonClicked a static function - I want to make a point about non-static functions next. Suppose instead that ButtonClicked is a non-static member:

class MyWindow : Window
{
    Button _button;
    int _numClicked = 0;

    public MyWindow()
    {
        this._button = new Button();
        // place the button in the window
        this._button.Click += this.ButtonClicked;
    }

    void ButtonClicked(object sender, RoutedEventArgs e)
    {
        this._numClicked += 1;
        MessageBox.Show("Button Clicked " + this._numClicked + " times");
    }
}

现在委托包含的功能的引用ButtonClicked和实例,这,该方法被调用。实例这个的mywindow的构造和这个在ButtonClicked是相同的。

Now the delegate contains both a reference to the function "ButtonClicked" and the instance, "this", which the method is called on. The instance "this" in the MyWindow constructor and "this" in ButtonClicked are the same.

这是被称为<青霉>封闭一个概念的一个特定情况下这使得储蓄状态 - 当前对象,局部变量等 - 创建委托时。在上面的例子中,我们使用本从委托构造函数。我们可以做更多的事:

This is a specific case of a concept known as closures which allows "saving" the state - the current object, local variables, etc. - when creating a delegate. In the above example, we used "this" from the constructor in the delegate. We can do more than that:

class MyWindow : Window
{
    Button _button;
    int _numClicked = 0;

    public MyWindow(string localStringParam)
    {
        string localStringVar = "a local variable";
        this._button = new Button();
        // place the button in the window
        this._button.Click += new RoutedEventHandler(
            delegate(object sender, RoutedEventArgs args)
            {
                this._numClicked += 1;
                MessageBox.Show("Param was: " + localStringParam + 
                     " and local var " + localStringVar +
                     " button clicked " + this._numClicked + " times");
            });
    }
}

下面,我们创建了一个的匿名委托 - 这是没有给出一个明确的名称的功能。参考此功能的唯一方法是使用RoutedEventHandler委托对象。此外,该函数存在于mywindow的构造的范围内,所以它可以访问所有的本地参数,变量,和成员实例这个。它将继续持有,甚至在mywindow的构造函数退出后的局部变量和参数引用。

Here we created an anonymous delegate - a function which is not given an explicit name. The only way to refer to this function is using the RoutedEventHandler delegate object. Furthermore, this function exists in the scope of the MyWindow constructor, so it can access all local parameters, variables, and the member instance "this". It will continue to hold references to the local variables and parameters even after the MyWindow constructor exits.

作为一个方面说明,委托也将持有对象的实例的引用 - 本 - 即使到该类所有其他引用的删除。因此,要确保一个类垃圾回收,全部委托给非静态成员方法(或在一个范围创建委托)应该被删除。

As a side note, the delegate will also hold a reference to the object instance - "this" - even after all other references to the class a removed. Therefore, to ensure that a class is garbage collected, all delegates to a non-static member method (or delegates created in the scope of one) should be removed.

这篇关于在C#中的代表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆