Philipp Schmid Philipp Schmid - 3 months ago 17
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

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.