Created
September 30, 2017 06:53
-
-
Save gyuwon/77d8ab798dcabefcf0f7826c15709b36 to your computer and use it in GitHub Desktop.
This file contains 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 AnonymousFunctionsVsLocalFunctions | |
{ | |
public class Program | |
{ | |
public static void Main(string[] args) | |
{ | |
var functions = new Functions(); | |
Console.WriteLine(functions.PerformAnonymousFunction()); | |
Console.WriteLine(functions.PerformAnonymousFunctionWithParam()); | |
Console.WriteLine(functions.PerformAnonymousFunctionWithParamAsCallback()); | |
Console.WriteLine(functions.PerformAnonymousFunctionWithLocal()); | |
Console.WriteLine(functions.PerformAnonymousFunctionWithMember()); | |
Console.WriteLine(functions.PerformAnonymousFunctionWithLocalSideEffect()); | |
Console.WriteLine(functions.PerformAnonymousFunctionWithLocalSideEffectAsCallback()); | |
Console.WriteLine(functions.PerformAnonymousFunctionWithMemberSideEffect()); | |
Console.WriteLine(functions.PerformLocalFunction()); | |
Console.WriteLine(functions.PerformLocalFunctionWithParam()); | |
Console.WriteLine(functions.PerformLocalFunctionWithParamAsCallback()); | |
Console.WriteLine(functions.PerformLocalFunctionWithLocal()); | |
Console.WriteLine(functions.PerformLocalFunctionWithMember()); | |
Console.WriteLine(functions.PerformLocalFunctionWithLocalSideEffect()); | |
Console.WriteLine(functions.PerformLocalFunctionWithLocalSideEffectAsCallback()); | |
Console.WriteLine(functions.PerformLocalFunctionWithMemberSideEffect()); | |
} | |
} | |
public class Functions | |
{ | |
private int _value = 0; | |
public int PerformAnonymousFunction() | |
{ | |
Func<int> func = () => 1; | |
return func(); | |
} | |
public int PerformAnonymousFunctionWithParam() | |
{ | |
Func<int, int> func = x => x + 1; | |
return func(1); | |
} | |
public int PerformAnonymousFunctionWithParamAsCallback() | |
{ | |
Func<int, int> func = x => x + 1; | |
return Invoke(func, 1); | |
} | |
public int PerformAnonymousFunctionWithLocal() | |
{ | |
int x = 1; | |
Func<int> func = () => x + 1; | |
return func(); | |
} | |
public int PerformAnonymousFunctionWithMember() | |
{ | |
Func<int> func = () => _value + 1; | |
return func(); | |
} | |
public int PerformAnonymousFunctionWithLocalSideEffect() | |
{ | |
int x = 1; | |
Func<int> func = () => ++x; | |
return func(); | |
} | |
public int PerformAnonymousFunctionWithLocalSideEffectAsCallback() | |
{ | |
int x = 1; | |
Func<int> func = () => ++x; | |
return Invoke(func); | |
} | |
public int PerformAnonymousFunctionWithMemberSideEffect() | |
{ | |
Func<int> func = () => ++_value; | |
return func(); | |
} | |
public int PerformLocalFunction() | |
{ | |
int func() => 1; | |
return func(); | |
} | |
public int PerformLocalFunctionWithParam() | |
{ | |
int func(int x) => x + 1; | |
return func(1); | |
} | |
public int PerformLocalFunctionWithParamAsCallback() | |
{ | |
int func(int x) => x + 1; | |
return Invoke(func, 1); | |
} | |
public int PerformLocalFunctionWithLocal() | |
{ | |
int x = 1; | |
int func() => x + 1; | |
return func(); | |
} | |
public int PerformLocalFunctionWithMember() | |
{ | |
int func() => _value + 1; | |
return func(); | |
} | |
public int PerformLocalFunctionWithLocalSideEffect() | |
{ | |
int x = 1; | |
int func() => ++x; | |
return func(); | |
} | |
public int PerformLocalFunctionWithLocalSideEffectAsCallback() | |
{ | |
int x = 1; | |
int func() => ++x; | |
return Invoke(func); | |
} | |
public int PerformLocalFunctionWithMemberSideEffect() | |
{ | |
int func() => ++_value; | |
return func(); | |
} | |
private int Invoke(Func<int> func) => func(); | |
private int Invoke(Func<int, int> func, int arg) => func(arg); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment