函数指针和C#代理

先看一段C的面向过程的函数指针代码:

int max(int x,int y)

{

return (x>y?x:y);

}

int min(int x,int y)

{

return(x<y?x:y);

}

int sub(int x, int y)

{

return(x+y);

}

int minus(int x,int y)

{

return(x-y);

}

void test(int (*p)(int,int),

         int (*q)(int,int),

         int a,

         int b)

{

int Int1,Int2;

Int1=(*p)(a,b);

Int2=(*q)(a,b);

printf("%d,/t%d/n",Int1,Int2);

}

 

int main(int argc, _TCHAR* argv[])

{

    test(max,min,10,3);

    test(sub,minus,10,3);

    return 0;

}

 

 

再看一段C#的代理代码:

using System;

using System.Collections.Generic;

using System.Text;

namespace ConsoleApplication3

{

class MathClass

{

public static int max(int a, int b)

{

return (a > b ? a : b);

}

public static int min(int a, int b)

{

return (a < b ? a : b);

}

public static int sub(int a, int b)

{

return (a + b);

}

public static int minus(int a, int b)

{

return (a - b);

}

}

class Handler

{

public delegate int Calculation(int a, int b);

public static void EventHandler(Calculation c1,Calculation c2, int a, int b)

{

int x, y;

x = c1(a, b);

y = c2(a, b);

 

Console.WriteLine("{0}:{1}/n{2}:{3}",c1.Method.ToString(), x, c2.Method.ToString(),y);

}

}

class Program

{

static void Main(string[] args)

{

Handler.EventHandler(new Handler.Calculation(MathClass.max), new Handler.Calculation(MathClass.min), 10, 3);

Handler.EventHandler(new Handler.Calculation(MathClass.sub), new Handler.Calculation(MathClass.minus), 10, 3);

Console.ReadKey();

}

}

}

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值