[C#学习杂记] CSDN上看到的一个题目

对于委托中的隐藏方法有些迷惑,请高手给与说明
下面是三段代码,请问CODE1 、CODE2 和 CODE3的区别,最好给予详细说明。

 

CODE1:

using System;



namespace ConsoleApplication1 {

class Program {

    delegate string delegateTest (string val);



    static void Main (string[] args){

        string mid = ",middle part,";

        

        

        delegateTest anonDel = delegate (string param){

            param += mid;

            param += "and this is added to the string.";

            return param;

            };

        

        

                

        Console.WriteLine (anonDel( "Start of string" ) );

        }

    }

}



CODE2:

 

using System;



namespace ConsoleApplication1 {

class Program {

    delegate string delegateTest (string val);

    static string mid = ",middle part,";

    static void Main (string[] args){

        

        delegateTest anonDel = new delegateTest (DelMethod);

                        

        Console.WriteLine (anonDel( "Start of string" ) );

        }

    static string DelMethod(string param){

            param += mid;

            param += "and this is added to the string.";

            return param;

            }

    }

}



 

CODE3:

using System;



namespace ConsoleApplication1 {

class Program {

    delegate string delegateTest (string val);



    static void Main (string[] args){

        string mid = ",middle part,";

        

        

        delegateTest anonDel = new delegateTest (delegate (string param){

            param += mid;

            param += "and this is added to the string.";

            return param;

            });

                

        Console.WriteLine (anonDel( "Start of string" ) );

        }

    }

}

 

 

刚好学了委托,我来回答下O(∩_∩)O哈哈~

 

CODE1 和CODE3应该都是匿名委托,CODE2是标准委托形式。 CODE1和CODE3只是写法上的问题。

你可能感兴趣的:(csdn)