public void AnonymousMethodsWithOuterVariables() { //CountItWithParamAndArg countIt = new CountItWithParamAndArg(Anonymousmethod.AnonymousMethodsWithOuterVariables()); CountItWithParamAndArg countIt = Anonymousmethod.AnonymousMethodsWithOuterVariables(); int Result = countIt(3); Console.WriteLine("Summation of 3 is : " + Result); Result = countIt(5); Console.WriteLine("Summation of 5 is : " + Result); Console.WriteLine("As you can see, the count still proceeds normally. However, notice the summation value for 5.It shows 21 instead of 15!"); }
public static CountItWithParamAndArg AnonymousMethodsWithOuterVariables() { int sum = 0; CountItWithParamAndArg CountItWithParamAndArgobj = delegate(int End) { for (int i = 0; i <= End; i++) { sum += i; Console.WriteLine(i); } return(sum); }; return(CountItWithParamAndArgobj); }
public static void AnonymousMethodsWithParameterArgument() { CountItWithParamAndArg countWithPA = delegate(int endedited) { int sum = 0; Console.WriteLine("This is a Default Delegate with No returned or passed arguent"); for (int i = 0; i <= endedited; i++) { Console.WriteLine(i); sum += i; } return(sum); }; int Summation = countWithPA(6); Console.WriteLine("sum = " + Summation); }