wingman007

2015_OOP_Features_DelegateTest

May 27th, 2015
564
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.75 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace World1
  8. {
  9.     public delegate double MathAction(double num);
  10.  
  11.     public delegate void MyDelegate(string message);
  12.  
  13.     // public delegate void MyEventHandler(object sender, EventArgs arg);
  14.     class DelegateTest
  15.     {
  16.         public double Double(double num)
  17.         {
  18.             return 2 * num;
  19.         }
  20.  
  21.         public static void Test()
  22.         {
  23.             DelegateTest p = new DelegateTest();
  24.             // 1. Named function
  25.             MathAction d1 = p.Double;
  26.             // MathAction d1 = new MathAction(Double);
  27.  
  28.             // Console.WriteLine(Calculate(Double, 3.14)); // this also works
  29.  
  30.             Console.WriteLine("The delegate says {0}", d1(3.14));
  31.  
  32.             // free variable. Used in the context of the anonymous function and lambdas
  33.             // The functions can create closure.
  34.             // Keeps information about the lexical scope where the function was defined.
  35.             int freeVar = 3; // it is used in the functions bellow to create closure
  36.  
  37.             // 2. Anonymous function
  38.             d1 = delegate(double num) {
  39.                 // return num * num; // no closure
  40.                 return num * num * freeVar; // closure - variable from the outer context is used
  41.             };
  42.  
  43.             Console.WriteLine("The delegate says {0}", d1(3.14));
  44.            
  45.             // Lambda is a code snipped which can be passed as an argument of a function
  46.             // or returned from a function as a return value
  47.             // Usually anonymous functions are used as lambdas. They may or not create closure
  48.             // 3. Lambda Expressions
  49.             // d1 = (x, y) => { x++;  return x * x * x; }; // statement. Has {...} and "return"
  50.             // 3.1. lambda Expression
  51.             // (The expression consists of operators literals, identifiers, evaluates to a value)
  52.             d1 = x => x * x * x; // expression
  53.             d1 = x => x * x * x * freeVar; // expression and creates closure
  54.             (freeVar is used in the body but it is not declared in the body of the function)
  55.  
  56.             // 3.2. Lambda Statement
  57.             d1 = x => {x++; return x * x * x}; // Lambda statement
  58.             d1 = x => {x++; return x * x * x * freeVar}; // Lambda statement creates closure (freeVar is used)
  59.  
  60.             Console.WriteLine("The delegate says {0}", d1(3.14));
  61.  
  62.             // Console.WriteLine("Functional calculator {0}", p.Calculate(s => s * s, 2.71));
  63.  
  64.             // Console.WriteLine("Functional calculator {0}", p.Calculate(delegate(double num) { return num * num; }, 2.71));
  65.  
  66.             Console.WriteLine("Functional calculator {0}", p.Calculate(d1, 2.71));
  67.         }
  68.  
  69.         public double Calculate(MathAction action, double num)
  70.         {
  71.             return action(num);
  72.         }
  73.  
  74.     }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment