在C#中使用代理 [英] Using delegates in C#

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

问题描述

在C#语言和.NET框架中,你能帮助我理解代表吗?
我试图检查一些代码,发现我收到的结果对我来说是意想不到的。这里是:

  class Program 
{
public static int I = 0;

static Func< string> del = new Func< string>(I.ToString);

static void Main(string [] args)
{
I = 10;
Console.WriteLine({0},del());
}
}

答案是0,但不是10.为什么?

解决方案

原因如下:



代表它直接指向静态int实例的 ToString 方法。它在创建时被捕获。



由于flindeberg在下面的注释中指出,每个代理都有一个目标和一个要在目标上执行的方法。



在这种情况下,要执行的方法显然是 ToString 方法。有趣的部分是执行该方法的实例:它是创建时的 I 的实例,这意味着委托不使用获取实例使用,但它存储对实例本身的引用。



稍后更改 I 到一个不同的值,基本上分配一个新的实例。这不会神奇地改变您代表中捕获的实例,为什么要这样做?



要获得您期望的结果,您需要将代理更改为: / p>

  static Func< string> del = new Func< string>(()=> I.ToString()); 

像这样,代理指向一个匿名方法执行 ToString 当前 I 在执行代表时。



在这种情况下,要执行的方法是在其中声明代理的类中创建的匿名方法。实例为null,因为它是一个静态方法。



有一个看看编译器为代理的第二个版本生成的代码:

  private static Func< string> del = new Func< string>(UserQuery。< .cctor> b__0); 
private static string cctor> b__0()
{
return UserQuery.I.ToString();
}

如你所见,这是一种常见的方法, 。在我们的例子中,它返回在 I 的当前实例上调用 ToString 的结果。


In C# language and .NET framework, could you help me with understanding delegates? I was trying to check some code, and found that the results I received were unexpected for me. Here it is:

class Program
{
    public static int I = 0;

    static Func<string> del = new Func<string>(I.ToString);

    static void Main(string[] args)
    {
        I = 10;
        Console.WriteLine("{0}", del());
    }
}

The answer was 0, but not 10. Why?

解决方案

The reason is the following:

The way you declare the delegate it points directly to the ToString method of the static int instance. It is captured at the time of creation.

As flindeberg points out in the comments below, each delegate has a target and a method to be executed on the target.

In this case, the method to be executed is obviously the ToString method. The interesting part is the instance the method is executed on: It is the instance of I at the time of the creation, meaning that the delegate is not using I to get the instance to use but it stores the reference to the instance itself.

Later you change I to a different value, basically assigning it a new instance. This doesn't magically change the instance captured in your delegate, why should it?

To get the result you expect, you would need to change the delegate to this:

static Func<string> del = new Func<string>(() => I.ToString());

Like this, the delegate points to an anonymous method that executes ToString on the current I at the time of the execution of the delegate.

In this case, the method to be executed is an anonymous method created in the class in which the delegate is declared in. The instance is null as it is a static method.

Have a look at the code the compiler generates for the second version of the delegate:

private static Func<string> del = new Func<string>(UserQuery.<.cctor>b__0);
private static string cctor>b__0()
{
    return UserQuery.I.ToString();
}

As you can see, it is a normal method that does something. In our case it returns the result of calling ToString on the current instance of I.

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

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