English 中文(简体)
C#: meaning of statement... Action wrappedAction = () =>
原标题:
  • 时间:2009-11-16 15:10:33
  •  标签:
  • c#-3.0

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...?

最佳回答

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.

问题回答

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.





相关问题
Linq - 2 tables - Sum

Asiento ******** Id_Asiento integer key Fecha date It_Asiento ********** Id_Asiento integer Forenkey Importe float I wan to do This SQL Query with Linq select Asiento.Id_Asiento, ...

Threading and un-safe variables

I have code listed here: Threading and Sockets. The answer to that question was to modify isListening with volatile. As I remarked, that modifier allowed me to access the variable from another thread....

How to cancel an asynchronous call?

How to cancel an asynchronous call? The .NET APM doesn t seem to support this operation. I have the following loop in my code which spawns multiple threads on the ThreadPool. When I click a button on ...

热门标签