Philipp Schmid Philipp Schmid - 1 year ago 89
C# Question

Difference between Action<T> as parameter and plain lambda as parameter

I was experimenting with multithreading when I fund something which I can't explain. The following code compiles and works without errors:

new Thread(() => Console.WriteLine("Hello")).Start();

Whereas this one doesn't:

Action a = () => Console.WriteLine("World");
new Thread(a).Start();

Why, what's the difference between these two?

Answer Source

A Lambda can be converted into any number of possible delegates or expression trees. An Action is a specific delegate type. There's no Constructor for Thread that accepts, specifically, an Action.

Here, specifically, the Lambda is turned into a ThreadStart delegate.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download