C#-delegate

iamzxf發表於2015-03-30


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace delegateDemo
{
    public delegate int NumberOperate(int x,int y);
    public class SimpleMath
    {
        public static int add(int x, int y)
        { return x + y; }
        public static int sub(int x, int y)
        { return x - y; }
    }
    
    class Program
    {
        static void Main(string[] args)
        {
            int a = 10, b = 20;
            NumberOperate numOp = new NumberOperate(SimpleMath.add);
            Console.WriteLine("{0}+{1}={2}",a,b,numOp(a,b));
            numOp = new NumberOperate(SimpleMath.sub);
            Console.WriteLine("{0}-{1}={2}", a, b, numOp(a, b));
            Console.ReadLine();

        }
    }
}