Domanda

I am new to Linq and Delegates and all that an I have following issue:

I tried this:

Func<int> f = () => { return 123; };

Delegate t = f;

Visual Studio shows no errors but then I try this:

Delegate d = () => return 123;

It's not working

Then I tried this:

Action a = delegate { Console.Out.WriteLine("test"); };
Delegate x = a;

It works, but

Delegate j = delegate { Console.Out.WriteLine("test"); };

Directly casting seems not to work. Why?

Can somebody explain me please the differences between Delegate (first cap letter) and delegate (all small letters) and Func<> and Action?

È stato utile?

Soluzione

You are missing fact that:

Func<int> f = () => { return 123; };
Delegate t = f;

is in fact using constructor:

Func<int> f = new Func<int>(() => { return 123; });

But there is no Delegate constructor taking lambda expression or implicit conversion between them.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top