Chinaunix首页 | 论坛 | 博客
  • 博客访问: 226745
  • 博文数量: 59
  • 博客积分: 3000
  • 博客等级: 中校
  • 技术积分: 1340
  • 用 户 组: 普通用户
  • 注册时间: 2006-02-14 09:50
文章分类

全部博文(59)

文章存档

2009年(2)

2008年(57)

我的朋友
最近访客

分类:

2008-05-27 09:49:16

using System;

namespace SimpleDelegate
{
  delegate double DoubleOp(double x);

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

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

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

  class MathsOperations
  {
    public static double MultiplyByTwo(double value)
    {
      return value*2;
    }

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

阅读(230) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~