There is no automatic conversion between a "value" and "a function with no arguments that returns a value" (let's call it a Func<T>
), nor is there an automatic conversion in the opposite direction.
To convert a Func<T>
into a value, T
, you have to put ()
after it. To convert a value into a Func<T>
, you can put () =>
in front of it.
If you want to call some method that accepts a Func<T>
, you cannot directly pass it a value, so you have to convert that value into a Func<T>
, and to do that you can put () =>
in front of it.
If you want to call some method that accepts a value, you cannot directly pass it a Func<T>
, so you have to convert that Func<T>
into a value by putting ()
after it.
Some languages automatically convert between these things, but C# doesn't.
In your case, you have a function that accepts a value, and you're trying to pass it a function, even though you already have a value, so you don't need to do anything special apart from give the value to the function.
int val = 5; // value
Func<int> f = () => val; // convert value to function
int val2 = f(); // convert back to value
The "anonymous methods" syntax is just the ugly old way of doing this. There are two problems with what you're trying to do (aside from the fact that it's unnecessary).
Firstly, you need to give the compiler a type hint by explicitly stating the delegate type with a new Func<int>(...)
wrapper.
Secondly, you need to add the ()
after it to get the value.
FibonacciWithLinq.Skip(new Func<int>
(delegate()
{
return 5;
})());
But it can't be stressed enough - this is completely pointless.