简体   繁体   English

匿名方法是内联定义的吗?

[英]Are anonymous methods defined inline?

Are anonymous methods defined inline ? 匿名方法是内联定义的吗? In the example below, the delegate object "d" is having reference of an anonymous method, which is accessing "x" variable which is defined in the Fun method. 在下面的示例中,委托对象“ d”引用了一个匿名方法,该方法正在访问在Fun方法中定义的“ x”变量。 The scope of "x" should be limited to Fun method, but when we call MyFun, which invokes the delegate passed as parameter and increments the value of "x". “ x”的范围应限于Fun方法,但是当我们调用MyFun时,它将调用作为参数传递的委托并递增“ x”的值。

The output comes out to be "6", how does this happen ? 输出结果为“ 6”,这是怎么发生的? How the value of "x", or in first place "x" variable itself was available in the anonymous method ? 如何在匿名方法中使用“ x”的值,或者首先是“ x”变量本身?

public delegate void Del();

public void Fun()
{
    int x = 5;
    Del d = delegate { x++;  };
    MyFun(d);
    Console.WriteLine(x);
}

public static void MyFun(Del d)
{
    d();
}

Are anonymous methods defined inline? 匿名方法是内联定义的吗?

I don't know what "inline" means. 我不知道“内联”是什么意思。

In the example below, the delegate object "d" is having reference of an anonymous method, which is accessing "x" variable which is defined in the Del method. 在下面的示例中,委托对象“ d”引用了一个匿名方法,该方法正在访问在Del方法中定义的“ x”变量。

No, x is defined in the Fun method. 不, x是在Fun方法中定义的。 Del is not a method, it's a delegate type. Del不是方法,它是委托类型。

The scope of "x" should be limited to Fun method. “ x”的范围应限于Fun方法。

Correct. 正确。 Recall that "the scope of x" is defined as "the region of program text in which x may be looked up by its unqualified name". 回想一下,“ x的范围”被定义为“程序文本中可以通过其非限定名称查找x的区域”。 You are using the term correctly; 您使用的是正确的术语; the body of Fun is the scope of x . Fun的主体是x的范围。 Note that the anonymous method is inside Fun and therefore x is in scope. 请注意,匿名方法在Fun内部,因此x在范围内。

when we call MyFun, it invokes the delegate passed as parameter and increments the value of "x". 当我们调用MyFun时,它将调用作为参数传递的委托,并增加“ x”的值。

Correct. 正确。

The output comes out to be "6". 输出结果为“ 6”。

Correct. 正确。

how does this happen ? 这是怎么发生的?

The question is not entirely clear. 问题尚不完全清楚。 It sounds like you've already provided an explanation for how this happens: the variable x is in scope inside the anonymous function, the function increments the variable, so the value of the variable changes. 听起来您已经提供了有关此情况的解释:变量x在匿名函数内的作用域内,该函数递增变量,因此变量的值会更改。

If your question is "what code does the compiler generate in order to make this happen?" 如果您的问题是“为了使这种情况发生,编译器会生成什么代码?” the compiler pretends that you wrote something like: 编译器假装您编写了以下内容:

public delegate void Del();

private class Locals
{
    public int x;
    public void AnonymousMethod() { x++; }
}    

public void Fun()
{
    Locals locals = new Locals();
    locals.x = 5;
    Del d = locals.AnonymousMethod;
    MyFun(d);
    Console.WriteLine(locals.x);
}

public static void MyFun(Del d)
{
    d();
}

This is called Closure . 这称为Closure It captures x and uses it as soon as the delegate is executed. 它捕获x并在委托执行后立即使用它。 It's done through some compiler magic. 这是通过一些编译器魔术来完成的。

It can be confusing sometimes, though. 但是,有时可能会造成混淆。 For instance, if you change x after defining the delegate, that change value would be used. 例如,如果您在定义委托后更改x ,则将使用该更改值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM