SimpleDelegate.cs
上传用户:lxycoco
上传日期:2022-07-21
资源大小:38457k
文件大小:1k
源码类别:

C#编程

开发平台:

Others

  1. using System;
  2. namespace SimpleDelegate
  3. {
  4.   delegate double DoubleOp(double x);
  5.   class MainEntryPoint
  6.   {
  7.     static void Main()
  8.     {
  9.       DoubleOp [] operations = 
  10.             {
  11.               new DoubleOp(MathsOperations.MultiplyByTwo),
  12.               new DoubleOp(MathsOperations.Square)
  13.             };
  14.       for (int i=0 ; i<operations.Length ; i++)
  15.       {
  16.         Console.WriteLine("Using operations[{0}]:", i);
  17.         ProcessAndDisplayNumber(operations[i], 2.0);
  18.         ProcessAndDisplayNumber(operations[i], 7.94);
  19.         ProcessAndDisplayNumber(operations[i], 1.414);
  20.         Console.WriteLine();
  21.       }
  22.       Console.ReadLine();
  23.     }
  24.     static void ProcessAndDisplayNumber(DoubleOp action, double value)
  25.     {
  26.       double result = action(value);
  27.       Console.WriteLine("Value is {0}, result of operation is {1}", value, result);
  28.     }
  29.   }
  30.   class MathsOperations
  31.   {
  32.     public static double MultiplyByTwo(double value)
  33.     {
  34.       return value*2;
  35.     }
  36.     public static double Square(double value)
  37.     {
  38.       return value*value;
  39.     }
  40.   }
  41. }