program.cs

来自「C#高级编程第6版随书源代码 值得下载」· CS 代码 · 共 53 行

CS
53
字号
using System;
using System.Collections.Generic;
using System.Text;

namespace Wrox.ProCSharp.Delegates
{
   class MathOperations
   {
      public static double MultiplyByTwo(double value)
      {
         return value * 2;
      }

      public static double Square(double value)
      {
         return value * value;
      }
   }


   delegate double DoubleOp(double x);

   class MainEntryPoint
   {
      static void Main()
      {
         DoubleOp[] operations = 
            {
               MathOperations.MultiplyByTwo,
               MathOperations.Square
               //new DoubleOp(MathOperations.MultiplyByTwo),
               //new DoubleOp(MathOperations.Square)
            };

         for (int i = 0; i < operations.Length; i++)
         {
            Console.WriteLine("Using operations[{0}]:", i);
            ProcessAndDisplayNumber(operations[i], 2.0);
            ProcessAndDisplayNumber(operations[i], 7.94);
            ProcessAndDisplayNumber(operations[i], 1.414);
            Console.WriteLine();
         }
      }

      static void ProcessAndDisplayNumber(DoubleOp action, double value)
      {
         double result = action(value);
         Console.WriteLine(
            "Value is {0}, result of operation is {1}", value, result);
      }
   }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?