我在理解 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?
使用委托的先决条件是什么?
What are the pre-requisites of using delegates?
我希望这能让问题更清楚.
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"的引用和调用该方法的实例this".MyWindow 构造函数中的实例this"和 ButtonClicked 中的this"是一样的.
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.
这是一个称为 closures 概念的特定案例,它允许在创建委托时保存"状态 - 当前对象、局部变量等.在上面的示例中,我们在委托的构造函数中使用了this".我们可以做的不止这些:
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 构造函数的范围内,因此它可以访问所有本地参数、变量和成员实例this".即使在 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.
作为旁注,委托还将持有对对象实例的引用 - this" - 即使在对类 a 的所有其他引用都已删除之后.因此,为了确保一个类被垃圾回收,所有非静态成员方法的委托(或在一个范围内创建的委托)都应该被删除.
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# 中的代表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!