Use delegate to wrap function - CSharp Custom Type

CSharp examples for Custom Type:delegate

Description

Use delegate to wrap function

Demo Code

using System;/*w ww .  j  a va 2  s.c  om*/
class Calculator
{
   public delegate double Calculation(int x, int y);
   public static double Sum(int num1, int num2)
   {
      return num1 + num2;
   }
   public static void Main()
   {
      double result;
      Math myMath = new Math();
      Calculation myCalculation = new Calculation(myMath.Average);
      result = myCalculation(10, 20);
      Console.WriteLine("Result of passing 10,20 to myCalculation: {0}", result);
      myCalculation = new Calculation(Sum);
      result = myCalculation(10, 20);
      Console.WriteLine("Result of passing 10,20 to myCalculation: {0}", result);
   }
}
class Math
{
   public double Average(int number1, int number2)
   {
      return (number1 + number2) / 2;
   }
}

Result


Related Tutorials