简体   繁体   中英

C# Conversion of lambda expression

What is the way to convert the following into lambda expression?

ThreadPool.QueueUserWorkItem(delegate
     {
        Console.WriteLine("Current Thread Id is {0}:",
         Thread.CurrentThread.ManagedThreadId);
        Console.WriteLine("I will be used as Callback");
      }
    );

You could definitely write this as a lambda expression:

// The underscore is simply a placeholder for the "state"
// parameter that the WaitCallback delegate expects - you could
// use any character but you must specify one as lamba expressions cannot
// omit parameters like anonymous functions can.
ThreadPool.QueueUserWorkItem((_) =>
    {
        Console.WriteLine("Current Thread Id is {0}:",
        Thread.CurrentThread.ManagedThreadId);
        Console.WriteLine("I will be used as Callback");
    });

But remember that a lambda expression has no meaning outside of your source code. The C# compiler will convert your lambda expression right back to the code you have now.

A lambda expression is simply syntactic sugar that you can use to express an anonymous function - the compiler will convert this to either an anonymous function or an expression tree.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM