using System;namespace iuhxq
{
class App
{
static string[] s1 = {"a","b","c","d"};
static string[] s2 = {"1","2","3","4"}; private delegate string MyDelegate(string[] s); private static string Concat(string[] s)
{
return String.Concat(s);
} private static string Sum(string[] s)
{
int result = 0;
foreach (string si in s)
{
result += Int32.Parse(si);
} return result.ToString();
} private static void Run(MyDelegate del, string[] s)
{
Console.WriteLine(del(s));
} public static void Main()
{
Run(new MyDelegate(Concat), s1);
Run(new MyDelegate(Sum), s2);
}
};
}

解决方案 »

  1.   

    using System;namespace iuhxq
    {
    class App
    {
    static string[] s1 = {"a","b","c","d"};
    static string[] s2 = {"1","2","3","4"}; private delegate string MyDelegate(string[] s); private static string Concat(string[] s)
    {
    return String.Concat(s);
    } private static string Sum(string[] s)
    {
    int result = 0;
    foreach (string si in s)
    {
    result += Int32.Parse(si);
    } return result.ToString();
    } private static void Run(MyDelegate del, string[] s)
    {
    Console.WriteLine(del(s));
    } public static void Main()
    {
    Run(new MyDelegate(Concat), s1);
    Run(new MyDelegate(Sum), s2);
    Run(new MyDelegate(Sum)+new MyDelegate(Concat), s2);//这条为什么不是输出101234?
    }
    };
    }
      

  2.   

    to Run(new MyDelegate(Sum)+new MyDelegate(Concat), s2);//这条为什么不是输出101234?当然不会是101234,第一个参数相当于是函数指针,虽说是传进去两个,真正起作用的是后面那个。
      

  3.   

    // compose.cs
    using System;delegate void MyDelegate(string s);class MyClass
    {
        public static void Hello(string s)
        {
            Console.WriteLine("  Hello, {0}!", s);
        }    public static void Goodbye(string s)
        {
            Console.WriteLine("  Goodbye, {0}!", s);
        }    public static void Main()
        {
            MyDelegate a, b, c, d;        // Create the delegate object a that references 
            // the method Hello:
            a = new MyDelegate(Hello);
            // Create the delegate object b that references 
            // the method Goodbye:
            b = new MyDelegate(Goodbye);
            // The two delegates, a and b, are composed to form c: 
            c = a + b;
            // Remove a from the composed delegate, leaving d, 
            // which calls only the method Goodbye:
            d = c - a;        Console.WriteLine("Invoking delegate a:");
            a("A");
            Console.WriteLine("Invoking delegate b:");
            b("B");
            Console.WriteLine("Invoking delegate c:");
            c("C");
            Console.WriteLine("Invoking delegate d:");
            d("D");
        }
    }
    跟这个有什么区别吗?
      

  4.   

    我的理解:
    委托是C#里的一种新模式,可以对应C/C++里的函数指针.与函数指针的不同如下:
    1. delegate是类型安全的,也就是不能把签名(C里面叫原型)不一样的方法(C里面叫函数)添加到delegate中.
    2. delegate内部有一个previous引用,也就是说它们可以组成一个链表.所以delegate可以一次调用多个类型一致的方法.
    从字面上理解,可以把一个delegate理解成一些方法的委托人,你可以通过他调用这些方法.:)
      

  5.   

    当然有区别了,你还没有真正了解函数参数类型,你的函数定义如下
    private static void Run(MyDelegate del, string[] s)第一个参数的类型是MyDelegate,即一个委托方法类型,那么当你
    Run(new MyDelegate(Sum)+new MyDelegate(Concat), s2);
    进行调用的时候,你的意思是想分别用两个委托方法处理s2,然后再进行字符串合并;
    但是编译器并不是按照你所想的运行,首先对第一个参数进行处理,如下
    new MyDelegate(Sum)+new MyDelegate(Concat)
    即要匹配MyDelegate类型,那么以上的参数值匹配成到函数中,真正起作用的是“new MyDelegate(Concat)”这部分,那么就相当于
    Run( new MyDelegate(Concat), s2);
    效果。
      

  6.   

    c = a + b;
    d = c - a;这些只是delegate重载了+/-操作符,和重载的+=操作符道理一样.delegate在内部还是要调用类似Delegate Add(Delegate d);这样的方法.因为只有基础类型像int,float 才配备操作符的IL指令.
      

  7.   

    建议看一下《.net框架程序设计》(jeffrey richter)一书,把讲.net的基础知识非常透彻。
      

  8.   

    看看系统的委托
    this.Load += new System.EventHandler(this.Page_Load); private void Page_Load(object sender, System.EventArgs e)
    {
    // 在此处放置用户代码以初始化页面
    }this.Button1.Click += new System.EventHandler(this.Button1_Click); private void Button1_Click(object sender, System.EventArgs e)
    { }
      

  9.   

    谢谢老猎人,正在看.net框架程序设计,看到第七章了,还没到讲解委托的地方,呵呵