C#委托最简单理解

来源:互联网 发布:2015加工贸易数据详细 编辑:程序博客网 时间:2024/06/08 22:22

1。委托要指向的函数保持一样的定义。(返回值、形参),只是多了一个delegate关键字

2。使用委托就象使用函数一样。

3。声明委托就象声明enum类型一样。

4。委托实例化的时候,传入对应的函数名称。

 

示例:

 class Program
    {
        delegate string pocess(string s);
        static string GetPre(string word)
        {
            return "==>" + word;
        }
        static  string GetLast(string word)
        {
            return word+"<===";
        }
        static void Main(string[] args)
        {
            pocess p;
            p = new pocess(GetPre);
            Console.WriteLine(p("word"));

            p = new pocess(GetLast);
            Console.WriteLine(p("word2"));


            Console.ReadLine();

        }