为什么这段代码返回109?
有人可以告诉我它是如何工作的吗?我将不胜感激。不知道如何调试这个。
using System;
class Prg
{
private static Func<double, Func<double, double>> Add(int c)
{
return x => y => x + y++ + (c *= 2);
}
public static void Main(string[] args)
{
int a = 1;
int b = 100;
var F = Add(2);
var G = F(a);
G(b);
Console.WriteLine(G(b));
}
}
编辑:如果您想享受我们的 C# 考试,我还有另一份。这是代码。
delegate int F();
class Prg { int a = 10;
public F Adder(int x) {
int i = x;
return delegate {
return a += i++; };
}
static void Main() {
Prg p = new Prg();
F f = p.Adder(5);
p.Adder(10);
f();
System.Console.Write(f());
} }
Could someone tell me how does it work? I would be grateful. Dunno how to debug this.
using System;
class Prg
{
private static Func<double, Func<double, double>> Add(int c)
{
return x => y => x + y++ + (c *= 2);
}
public static void Main(string[] args)
{
int a = 1;
int b = 100;
var F = Add(2);
var G = F(a);
G(b);
Console.WriteLine(G(b));
}
}
EDIT: I have got another one if you want to enjoy our C# exam.. Here is the code.
delegate int F();
class Prg { int a = 10;
public F Adder(int x) {
int i = x;
return delegate {
return a += i++; };
}
static void Main() {
Prg p = new Prg();
F f = p.Adder(5);
p.Adder(10);
f();
System.Console.Write(f());
} }
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这部分执行两次:
第一次调用 G(b) 时。第二次打印到控制台时。
一次执行实际上会计算:
但由于每次执行时 c 都会乘以 2(并分配回 c),因此您会得到 109 而不是 105。变量
c
被引用为中的一个精确内存位置函数 Add 的范围,实际上用作 Func 委托(并且 Func 的实例始终保持不变),这就是为什么这与使用函数进行一些嵌套调用不同(其中仅传递经过值将出现在函数中,该值将被返回,函数的范围将关闭并且永远不会在相同的上下文中再次访问)。This part executes twice:
The first time when you call G(b). The second time when you print to console.
One execution actually calculates:
But since c gets multiplied by 2 (and assigned back to c) on each execution, you get 109 instead of 105. Variable
c
is referenced as one exact memory location in the scope of function Add, actually used as Func delegate (and instance of Func remains the same all the time), that's why this is not the same as if you would do some nested calls with functions (where only the transfer by value would occur into the function, the value would be returned, the scope of function closed and never accessed again with the same context).我首先要说的是:永远不要编写这样的代码。
如果您确实想知道发生了什么:
var F = Add(2)
行中,方法 Add() 创建并返回一个 c==2 的 lambda 表达式。这里重要的是c
被 lambda 捕获(您可以在 msdn。var G = F(x)
行中,您只需使用参数a = 1
调用函数,然后您结果得到另一个函数,double -> double
准确地说,G(b)
行中,您正在调用您的函数,正是这个:x + y++ + (c *= 2);
现在:Console....
行中,您再次调用您的函数。这次,参数再次等于100
,因此结果将相同,但捕获的变量c
现在等于 4,因此您收到的结果1 + (100+) + (4 *= 2) -> 1+100+8-> 109
这是一个简短的示例,可以帮助我们了解那里发生的事情:
I will start with: don't write such code, ever.
And if you really want to know what is happening:
var F = Add(2)
, method Add() creates and returns a lambda expression with c==2. What is important here, is thatc
is caught by lambda (you can read about capturing variables on msdn.var G = F(x)
you are simply calling your function with parametera = 1
and you get another function as a result,double -> double
to be exact.G(b)
you are calling your function, exactly this one:x + y++ + (c *= 2);
Now:Console....
you are invoking your function again. This time, parameter is again equal to100
, so a result would be identical, but your captured variablec
is now equal to 4, so a result you receive1 + (100+) + (4 *= 2) -> 1 + 100 + 8 -> 109
Here's a short example that might shine some light on what has happened there: