3

I was just wondering about this case

void exc(Func<int> fn) {
    fn();
}

I can do the below

public void test() {
    exc(delegate{return 1;});
}

However I like the => syntax so i tried

public void test() {
    exc(void=>1);
}

It didnt compile. Is there a way I may use the => syntax?

3 Answers 3

4

You almost did from the top of your head :). Check MSDN for more details, but this is what you are looking for:

public void test()
{
    exc(()=>1);
}
Sign up to request clarification or add additional context in comments.

Comments

4

Func<int> means a function that takes no arguments and returns an integer. So you could specify it as an anonymous function like this

public void test()
{
    exc(() => 1);
}

Comments

2

As simple as

() => 1

http://msdn.microsoft.com/en-us/library/bb397687.aspx

And ctrl+f for "Specify zero input parameters with empty parentheses:"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.