Last active
September 29, 2017 07:21
-
-
Save gongdo/409f585aec3c94287057071c029f342d to your computer and use it in GitHub Desktop.
Lambda vs Local Function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace ConsoleApp6 | |
{ | |
public class LambdaVsLocalFunction | |
{ | |
private int member = 42; | |
private int PlainLambda() | |
{ | |
Func<int, int> func = a => a + 1; | |
return func(42); | |
} | |
private int PlainLocalFunction() | |
{ | |
int func(int number) => number + 1; | |
return func(42); | |
} | |
private int LambdaWithLocalVar() | |
{ | |
var a = 42; | |
Func<int> func = () => a + 1; | |
return func(); | |
} | |
private int LocalFunctionWithLocalVar() | |
{ | |
var a = 42; | |
int func() => a + 1; | |
return func(); | |
} | |
private int LambdaWithMemberVar() | |
{ | |
Func<int> func = () => { return member + 1; }; | |
return func(); | |
} | |
private int LocalFunctionMemberVar() | |
{ | |
int func() => member + 1; | |
return func(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment