开发者

C#: meaning of statement... Action wrappedAction = () =>

开发者_JAVA百科Saw the code here. Can any one tell me what it means?

    Action wrappedAction = () => 
    { 
        threadToKill = Thread.CurrentThread; 
        action(); 
    }; 

Can we write such code with .net v2.0...?


It means that wrapAction is a delegate, that takes in no parameter and that executes the following code block

  threadToKill = Thread.CurrentThread; 
  action();

It's equivalent to

public delegate void wrapActionDel(); 

public void wrapAction()
{
      threadToKill = Thread.CurrentThread; 
      action();
}

public void CallwrapAction()
{
   wrapActionDel del = wrapAction;
   del ();
}

You can see that this is verbose, but Action is not.

And, this is only available in .Net 3.5. Don't worry, your .net 2.0 code will work seamlessly with .net 3.5, so you can just upgrade.


That is a lambda expression, and is only available in C# 3.0+. The C# 2.0 version of that code looks like this:

Action wrappedAction = delegate() 
{ 
    threadToKill = Thread.CurrentThread; 
    action(); 
};

Assuming you have declared the Action delegate previously.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜