刚随手写了个C#委托的例子~现在贴上来...

一个非常简单的委托例子,,,贴着玩吧...
计算两个数相加的结果

[code]

using System;
namespace Add
{
    delegate void numberAdd(int a,int b);
    class program
    {
        static void ADD0(int a, int b)
        {
            Console.WriteLine(a + b);
        }
        static void Main()
        {
            numberAdd n1 = ADD0;
            int x;
            int y;
            x = int.Parse(Console.ReadLine());
            y = int.Parse(Console.ReadLine());
            n1(x, y);
            Console.ReadLine();
        }
    }
}

[/code]

 

偶又用匿名方法优化了下代码,这样就简洁多了

[code]

using System;
namespace Add
{
    delegate void numberAdd(int a, int b);
    class program
    {
        static void Main()
        {
            numberAdd n1 = delegate(int a, int b)
            {
                Console.WriteLine(a + b);
            };
            int x = int.Parse(Console.ReadLine());
            int y = int.Parse(Console.ReadLine());
            n1(x, y);
            Console.ReadLine();
        }
    }
}

[/code]

酱紫,委托的两种简单用法就都在这了=,=||

posted on 2008-07-28 21:58  gc319  阅读(887)  评论(1)    收藏  举报

导航