C# – Purpose of ‘() =>’ in Lambda Expressions

c++delegateslambda

I have been trying to figure this out and it's really bugging me. I have some code that looks like this

static T MyFunction<T>(Func<T> action, int i, int i2)
{
    ...some code here
}

When I need to call this code I tried this

var result = MyFunction<List<string>>(MethodThatReturnsListofString(int number), 1,2)

It fails stating that the best overload has invalid arguments
But when I tried the following

var result = MyFunction<List<string>>(() => MethodThatReturnsListofString(int number), 1,2)

It works fine. What is the function of "() =>" in this case. I thought() could not be used with methods required more than 0 args.

Best Answer

You're passing a lambda that takes no arguments.
That is the correct syntax to write such a lambda.

The fact that your lambda happens to call a function that takes arguments is completely irrelevant.