《C#高級編程》第4版 Chapter6.2.3 多播委託

《C#高級編程》第4版  Chapter6.2.3 多播委託

using System;

namespace MulticastDelegate
{
  delegate void DoubleOp(double value);

  class MainEntryPoint
  {
    static void Main()
    {
      DoubleOp operations = new DoubleOp(MathsOperations.MultiplyByTwo);
      operations += new DoubleOp(MathsOperations.Square);

      ProcessAndDisplayNumber(operations, 2.0);
      ProcessAndDisplayNumber(operations, 7.94);
      ProcessAndDisplayNumber(operations, 1.414);
      Console.WriteLine();
      Console.ReadLine();
    }

    static void ProcessAndDisplayNumber(DoubleOp action, double value)
    {
      Console.WriteLine("/nProcessAndDisplayNumber called with value = " + value);
      action(value);
    }
  }

  class MathsOperations
  {
    public static void MultiplyByTwo(double value)
    {
      double result = value*2;
      Console.WriteLine("Multiplying by 2: {0} gives {1}", value, result);
    }

    public static void Square(double value)
    {
      double result = value*value;
      Console.WriteLine("Squaring: {0} gives {1}", value, result);
    }
  }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章