public static void Main232() { //Instantiating a delegate //Using the new keyword PerformCalculation performCal = new PerformCalculation(Add); //Using delegate inference PerformCalculation performCalculation = Add; SecondDelegate secondDelegate = CountEvenNumbers; //Invoking a delegate //Invoking Asynchronously int[] numArray = { 42, 44, 45, 46, 47, 48, 49, 50 }; secondDelegate.BeginInvoke(numArray, null, null); //Using the () operator performCal(40, 45); //Using the Invoke() method of the delegate class performCalculation.Invoke(40, 45); //End on the asynchronous invocation int sam = secondDelegate.EndInvoke(null); }
/// <summary> /// Initializes a new instance of the <see cref="DelegateExample"/> class. /// </summary> public DelegateExample() { //Type safety PerformCalculationVar = new PerformCalculation(PerformSumMethod); PerformCalculationVar = new PerformCalculation(PerformSubtractMethod); //PerformCalculationVar = new PerformCalculation(PerformSubtractDoubleMethod); }
/// <summary> /// Initializes a new instance of the <see cref="DelegateExample"/> class. /// </summary> public DelegateExample() { //Type safety PerformCalculationVar = new PerformCalculation(PerformSumMethod); PerformCalculationVar = new PerformCalculation(PerformSubtractMethod); //PerformCalculationVar = new PerformCalculation(PerformSubtractDoubleMethod); }
static void Main(string[] args) { PerformCalculation performCalculation1 = Operations.Sum; PerformCalculation performCalculation2 = Operations.Multiply; // operator += performCalculation1 += performCalculation2; // static method Delegate.Combine performCalculation1 = (PerformCalculation)Delegate.Combine( performCalculation1, performCalculation2); var multipleResult = performCalculation1(10, 5); Console.WriteLine($"{nameof(multipleResult)} = {multipleResult}"); Console.WriteLine("\n***\n"); // operator -= performCalculation1 -= performCalculation2; // method Remove() performCalculation1 = (PerformCalculation)Delegate.Remove( performCalculation1, performCalculation2); multipleResult = performCalculation1(10, 5); Console.WriteLine($"{nameof(multipleResult)} = {multipleResult}"); }
private static void LambdaExample() { Console.WriteLine("=====Example 4 (Lambda example)====="); //Use lamba expression to create a Func delegate instance Func <double, double, double> sum_Function = (double var1, double var2) => { return(var1 + var2); }; //Use lambda expression without data type to create a Func delegate instance Func <double, double, double> sum_Function_withoutType = (var1, var2) => var1 + var2; //Use lamba expression to create a delegate instance PerformCalculation prod_Function = (double var1, double var2) => var1 * var2; //Use lamba expression without data type to create a delegate instance PerformCalculation prod_Function_withoutType = (var1, var2) => var1 * var2; double val1 = 4.0, val2 = 3.0; //Call sum function double sum_Result = sum_Function(val1, val2); Console.WriteLine($"{val1} + {val2} = {sum_Result}"); //Call product function double prod_Result = prod_Function(val1, val2); Console.WriteLine($"{val1} * {val2} = {prod_Result}"); //Using sum_function reference Console.Write($"{val1} + {val2} = "); SpecialFunctions.ExecuteFunctionUsingFunc(sum_Function, val1, val2); //Using product_function reference Console.Write($"{val1} * {val2} = "); SpecialFunctions.ExecuteFunction(prod_Function, val1, val2); //Omitting the explicit creation of a Func instance Console.Write($"{val1} - {val2} = "); SpecialFunctions.ExecuteFunctionUsingFunc((var1, var2) => var1 - var2, val1, val2); //Omitting the explicit creation of a delegate instance Console.Write($"{val1} - {val2} = "); SpecialFunctions.ExecuteFunction((var1, var2) => var1 - var2, val1, val2); List <int> numbersList = new List <int>(new int[] { 0, 1, 2, 6, 8, 9, 21, 24, 10 }); /** * TODO 9 * * Create a lambda expression which receives two parameters and returns the biggest number * and use it to extract the biggest number from numbersList collection. */ /** * TODO 10 (for home) * Use the lambda expression from TODO 9 to sort the collection ascending. */ Console.WriteLine(); }
private static void DelegateWithAnonymousFunction() { // Anonymous functions PerformCalculation pc4 = (x, y) => x * y; Helpers.Print(pc4(2, 5)); }
static void Main(string[] args) { var calc = new SimpleCalculator(); int result; PerformCalculation calcReference; /*calcReference = calc.Sum; * result = calcReference(10, 15); * Console.WriteLine(result);*/ //DoCalculation(10, 15, calc.Sum); //DoCalculation(10, 15, calc.Multiply); PerformCalculation first = calc.Sum; PerformCalculation second = calc.Multiply; second = (PerformCalculation)Delegate.Combine(first, second); result = second(10, 15); Console.WriteLine(result); result = second.Invoke(20, 20); Console.WriteLine(result); PerformCalculation emptyCalculation = null; //NullReferenceException result = emptyCalculation.Invoke(40, 40); Console.WriteLine(result); }
static void Main(string[] args) { PerformCalculation handler = Calculation; Console.WriteLine("Result is {0}", handler(1, 2, 3)); Console.ReadKey(); }
static void Main(string[] args) { PerformCalculation getSum = Addition; //getSum(5.0, 5.0); PerformCalculation getQuotient = Division; //getQuotient(5.0, 5.0); PerformCalculation multiCal = getSum + getQuotient; multiCal += Subtraction; multiCal(3.2, 3.2); GetTextDelegate getTextDelegate = delegate(string name) { return("Hello, " + name); }; GetTextDelegate getHelloText = (string name) => { return("Hello, " + name); }; // Statement lamda GetTextDelegate getGoodbyeText = (string name) => { Console.WriteLine("I'm inside of an statment lambda"); return("Goodbye, " + name); }; GetTextDelegate getWelcomeText = name => "Welcom, " + name; Console.WriteLine(getHelloText("Denis")); }
static void Main(string [] args) { PerformCalculation calc1 = Addition; PerformCalculation calc2 = Substraction; PerformCalculation mix = calc1 + calc2; Console.WriteLine(mix); }
static void Main(string[] args) { PerformCalculation functionVariable = Substract; functionVariable += Multiply; functionVariable += Substract; functionVariable -= Multiply; Console.WriteLine(functionVariable(5, 6)); }
public static void Main() { PerformCalculation addition = delegate(int i, int j) { return(i + j); }; Console.WriteLine($"Addition of two numbers {addition(10, 20)}"); }
static void Main(string[] args) { PerformCalculation simpleInterestCalc = GetSimpleInterest; PerformCalculation compelexInterestCalc = GetCompoundInterest; var simpleInterest = new InterestCalculator(2500, 0.072, 15, simpleInterestCalc); var complexInterest = new InterestCalculator(500, 0.056, 10, compelexInterestCalc); Console.WriteLine(simpleInterest); Console.WriteLine(complexInterest); }
static void Main(string[] args) { PerformCalculation add = Add; PerformCalculation sub = Subtract; PerformCalculation mul = Multiply; PerformCalculation div = Divide; Console.WriteLine((sub(10, 5))); }
private static void DelegateExample() { Console.WriteLine("=====Example 1 (Delegate)==="); //Create Delegate instances PerformCalculation sum_Function = new PerformCalculation(SpecialFunctions.Sum); PerformCalculation prod_Function = new PerformCalculation(SpecialFunctions.Product); double val1 = 2.0, val2 = 3.0; //Call sum function double sum_Result = sum_Function(val1, val2); Console.WriteLine("{0} + {1} = {2}", val1, val2, sum_Result); //Call product function double prod_Result = prod_Function(val1, val2); Console.WriteLine("{0} * {1} = {2}", val1, val2, prod_Result); //Using sum_function reference Console.Write("{0} + {1} = ", val1, val2); SpecialFunctions.ExecuteFunction(sum_Function, val1, val2); //Using product_function reference Console.Write("{0} * {1} = ", val1, val2); SpecialFunctions.ExecuteFunction(prod_Function, val1, val2); /** * TODO 0 * Goto SpecialFunctions sources and resolve TODO 1, 2 and 3. */ //TODO 4: Create an instance of NumberCheck (TODO 1) NumberCheck even_Numbers = new NumberCheck(SpecialFunctions.IsNumberEven); //TODO 5: Use function GetEvenNumbers to select the even numbers from numbersList collection List <int> numbersList = new List <int>() { 0, 1, 2, 6, 8, 9, 21, 24, 10 }; List <int> result = SpecialFunctions.GetEvenNumbers(even_Numbers, numbersList); //alternativa pt todo4 si 5 //List<int> result = SpecialFunctions.GetEvenNumbers(SpecialFunctions.IsNumberEven, numbersList); Console.WriteLine(); //TODO 6: Print the resulted numbers foreach (int evenNumber in result) { Console.WriteLine(evenNumber); } }
static void Main(string [] args) { PerformCalculation calc = Addition; calc = Addition; Console.WriteLine(calc(45, 33)); calc = Substraction; Console.WriteLine(calc(34, 12)); calc = Divide; Console.WriteLine(calc(45, 3)); }
static void Main(string[] args) { PerformCalculation performCalculation = Operations.Sum; var result = performCalculation(10, 5); Console.WriteLine(result); performCalculation = Operations.Multiply; result = performCalculation(10, 5); Console.WriteLine(result); }
static void Main(string[] args) { Calculator _calcObj = new Calculator(); PerformCalculation _funObject = new PerformCalculation(_calcObj.Add); Computer _comp = new Computer(); _comp.Compute(_funObject); PerformCalculation _newFunObject = new PerformCalculation(_calcObj.Sub); _comp.Compute(_newFunObject); }
static void Main(string[] args) { PerformCalculation getSum = Addition; //getSum(5.0, 5.0); PerformCalculation getQuotient = Devision; //getQuotient(5.0, 5.0); PerformCalculation multiCalc = getSum + getQuotient; multiCalc += Substraction; multiCalc(3.2, 3.2); }
private void SetDelegate() { // Set delegate to function PerformCalculation pc = new PerformCalculation(Add); Helpers.Print(pc(2, 5)); // Set same delegate to another function pc = Sub; Helpers.Print(pc(2, 5)); }
private static void Main(string[] args) { Console.WriteLine("\n-------- Calculation --------\n"); PerformCalculation getSum = Addition; Console.WriteLine(getSum(6, 4.5)); PerformCalculation getSub = Substraction; Console.WriteLine(getSub(6, 4.5)); /********* Multi-cast ***********/ PerformCalculation operations = getSum + getSub; operations(3, 5); operations += Multiplication; operations(3, 5); operations -= Addition; operations(3, 5); operations -= getSub; operations(3, 5); //Dont do it: Give a logical fails, it runs but return last method. //Console.WriteLine(operations(3, 5)); Console.WriteLine("\n\n\n"); GetText welcomeMsg = Intro; Console.WriteLine(welcomeMsg("raph")); GetText welcomeName = delegate(string nametxt) { return("Hello, " + nametxt); }; Console.WriteLine(welcomeName("Raphael")); // Call display method, send anonymous delegate function (one param and one string return) Display(welcomeName); Console.WriteLine("\n"); Console.WriteLine(benVindo("Luana")); Console.WriteLine(bemVindo("Luana")); }
public static void Main() { PerformCalculation performCalculation = new PerformCalculation(Add); PerformCalculation performCal = Multiply; performCalculation += Multiply; //performCalculation += performCal; int val = performCalculation(40, 45); //returns 1800 performCalculation -= Multiply; int result = performCalculation(40, 45); //returns 95 }
private void DelegateAndFunctionThatTakesDelegates() { // using 'delegate' keyword PerformCalculation pc5 = delegate(int x, int y) { return(x * 5 + x - y); }; Helpers.Print(pc5(5, 1)); // function that has delegate as input param SomeCalc(2, 5, Add); SomeCalc(2, 5, pc5); }
private void MulticastDelegates() { // Multicast delegates / combine delegates PerformCalculation pc1 = Add; PerformCalculation pc2 = Sub; PerformCalculation pc3 = pc1 + pc2; Helpers.Print(pc3(2, 5)); pc3 += pc3; Helpers.Print(pc3(2, 5)); }
private static void AnonymousFunctExample() { Console.WriteLine("=====Example 3 (Anonymous Functions)====="); //Create a Func Delegate instance Func <double, double, double> sum_Function = delegate(double var1, double var2) { return(var1 + var2); }; //Create a Delegate instance PerformCalculation prod_Function = delegate(double var1, double var2) { return(var1 * var2); }; double val1 = 4.0, val2 = 3.0; //Call sum function double sum_Result = sum_Function(val1, val2); Console.WriteLine($"{val1} + {val2} = {sum_Result}"); //Call product function double prod_Result = prod_Function(val1, val2); Console.WriteLine($"{val1} * {val2} = {prod_Result}"); //Using sum_function reference Console.Write($"{val1} + {val2} = "); SpecialFunctions.ExecuteFunctionUsingFunc(sum_Function, val1, val2); //Using product_function reference Console.Write($"{val1} * {val2} = "); SpecialFunctions.ExecuteFunction(prod_Function, val1, val2); List <int> numbersList = new List <int>(new int[] { 0, 1, 2, 6, 8, 9, 21, 24, 10 }); /** * TODO 8 * Create an instance of function created at TODO 2 and use it to print the odd numbers from numbersList collection */ //Omitting the explicit creation of a Func instance Console.Write($"{val1} + {val2} = "); SpecialFunctions.ExecuteFunctionUsingFunc(delegate(double var1, double var2) { return(var1 + var2); }, val1, val2); Console.WriteLine(); }
public static void Main() { PerformCalculation performCalculation = Add; Console.WriteLine($"Addition of two numbers {performCalculation(10, 20)}"); performCalculation = Multiply; Console.WriteLine($"Multiplication of two numbers {performCalculation(10, 20)}"); AdditionalOperations additionalOperations = new AdditionalOperations(); performCalculation = additionalOperations.Substractions; Console.WriteLine($"Substractions of two numbers {performCalculation(20, 10)}"); }
public Testing() { CountdownCompleted = null; OurDelegate = Calculate; OurDelegate -= Calculate; OurDelegate += Calculate; OurDelegate += Calculate; OurDelegate += Calculate; OurDelegate += CalculateSum; OurDelegate = null; OurDelegate?.Invoke(4, 8); //CountdownCompleted.Invoke(this, new EventArgs()); // CountdownCompleted = null; }
static void Main(string[] args) { PerformCalculation getSum = Addition; //getSum(5.0, 5.0); PerformCalculation getQuotient = Division; //getQuotient(5.0, 5.0); PerformCalculation multiCalc = getSum + getQuotient; multiCalc += Subtraction; // adds subtraction method reference multiCalc -= getSum; // removes getSum reference multiCalc(3.2, 3.2); Console.ReadKey(); }
static void Main(string[] args) { PerformCalculation getSum = Addition; getSum(2.5, 5.6); PerformCalculation getQuotient = Division; getQuotient(5.0, 2.4); PerformCalculation multiCalc = getSum + getQuotient; multiCalc += Substraction; multiCalc(3.2, 6.4); }
public static void ExplainLambdaExpressions() { //Expression Lambda GetTextDelegate getTextDelegate = (string name) => { return("Hello " + name); }; //Statement Lambda GetTextDelegate getTextDelegate1 = (string name) => { string value = "Hello " + name + " Good morning"; return(value); }; Console.WriteLine(getTextDelegate("Raj")); PerformCalculation performCalculation = (a, b) => (a + b); Console.WriteLine(performCalculation(10, 20)); }
static void Main(string[] args) { // point at a method PerformCalculation getSum = Addition; getSum(7.6, 8.5); PerformCalculation getDivided = Divide; getDivided(25, 5); // multi-cast delegate -> useful to trigger multiple calculation with events PerformCalculation multipleCalculation = getSum + getDivided; multipleCalculation(99, 100.2); }
private static void DelegateExample() { Console.WriteLine("=====Example 1 (Delegate)==="); //Create Delegate instances PerformCalculation sum_Function = new PerformCalculation(SpecialFunctions.Sum); PerformCalculation prod_Function = new PerformCalculation(SpecialFunctions.Product); double val1 = 2.0, val2 = 3.0; //Call sum function double sum_Result = sum_Function(val1, val2); Console.WriteLine("{0} + {1} = {2}", val1, val2, sum_Result); //Call product function double prod_Result = prod_Function(val1, val2); Console.WriteLine("{0} * {1} = {2}", val1, val2, prod_Result); //Using sum_function reference Console.Write("{0} + {1} = ", val1, val2); SpecialFunctions.ExecuteFunction(sum_Function, val1, val2); //Using product_function reference Console.Write("{0} * {1} = ", val1, val2); SpecialFunctions.ExecuteFunction(prod_Function, val1, val2); /** * TODO 0 * Goto SpecialFunctions sources and resolve TODO 1, 2 and 3. */ //TODO 4: Create an instance of NumberCheck (TODO 1) //TODO 5: Use function GetEvenNumbers to select the even numbers from numbersList collection List<int> numbersList = new List<int>(new int[] { 0, 1, 2, 6, 8, 9, 21, 24, 10 }); //TODO 6: Print the resulted numbers Console.WriteLine(); }
public static void ExecuteFunction(PerformCalculation function, double param1, double param2) { double result = function(param1, param2); Console.WriteLine(result); }
public InterestCalculator(int sum, double interest, int years, PerformCalculation calc) { this.Sum = calc(sum, interest, years); }