看到代码here。任何人都可以告诉我这是什么意思?C#:声明的含义...动作wrappedAction =()=>
Action wrappedAction =() =>
{
threadToKill = Thread.CurrentThread;
action();
};
我们可以用.net v2.0编写这样的代码吗?
看到代码here。任何人都可以告诉我这是什么意思?C#:声明的含义...动作wrappedAction =()=>
Action wrappedAction =() =>
{
threadToKill = Thread.CurrentThread;
action();
};
我们可以用.net v2.0编写这样的代码吗?
这是一个lambda表达式,仅在C#3.0+中可用。该代码的C#2.0版本如下所示:
Action wrappedAction = delegate()
{
threadToKill = Thread.CurrentThread;
action();
};
假设您先前已声明了Action委托。
这是句法糖,不是框架魔法。所以,应该阅读“...只适用于C#3.0+。C#2.0版本...” – spoulson 2009-11-16 18:00:16
这是一个很好的选择,但我为你做了编辑。 – 2009-11-16 18:10:03
这意味着wrapAction
为代表,是发生在没有参数和执行以下代码块
threadToKill = Thread.CurrentThread;
action();
这相当于
public delegate void wrapActionDel();
public void wrapAction()
{
threadToKill = Thread.CurrentThread;
action();
}
public void CallwrapAction()
{
wrapActionDel del = wrapAction;
del();
}
你可以看到,这是冗长的,但Action
不是。
而且,这只适用于.net 3.5。别担心,.net 2.0代码可以与.net 3.5无缝协作,因此您可以升级。
只是一个注释...这将是C#3.0语法,这将需要.NET 3.5,但是,回想起.NET 3.0带来的C#3.0,而.NET 3.5实际上只是其他库,它仍然构建在CLR v2上0.0。 – 2009-11-16 17:57:43
FWIW,C#3.0语法至少需要.NET 2.0框架。项目设置确定它是为2.0或3.5框架编译的。 – spoulson 2009-11-16 17:58:59