【发布时间】:2015-12-19 02:55:57
【问题描述】:
我不明白为什么下面的代码有输出 230。请帮忙。 代码和输出发布在下面。您可以使用 linqpad 编辑下面的代码,也可以阅读下面的代码。
void Main() {
int ten = 10;
int twenty = 20;
int thirty = 30;
int temp = 10.Where(t => 20); // X.Where(10,20);
int result0 = temp.Select(t => 30);
result0.Dump("result0");
int result1 =
from c in ten
where twenty
select thirty;
result1.Dump("result1");
int result2 =
ten.Where(t => twenty).Select(t => thirty);
result2.Dump("result2");
}
static class X {
public static int Where(this int c, Func<int, int> f) {
return c * f(0);
}
public static int Select(this int c, Func<int, int> f) {
return c + f(0);
}
}
输出:
result0
230
result1
230
result2
230
【问题讨论】:
-
这段代码伤害了我的眼睛。你到底想达到什么目的?
-
编译成哪种语言?
-
提示:
t => 20是一个总是返回 20 的函数。它完全忽略了它的参数。 -
我想你完全不知道你在那里做什么。
-
它无法编译,至少
10.Where(t => 20);
标签: c# linq functional-programming