C#委托初识


”委托是一个类,它定义了方法的类型,使得可以将方法当作另一个方法的参数来进行传递,这种将方法动态地赋给参数的做法,可以避免在程序中大量使用If-Else(Switch)语句,同时使得程序具有更好的可扩展性。“

委托类似于函数指针,但函数指针只能引用静态方法,而委托既能引用静态方法,也能引用实例方法。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DelegateStudy
{
    public delegate void PrintPoem();
   
    class Program
    {

        static void Main(string[] args)
        {
            Program Pro = new Program();
            //常用定义委托的两种方法
            OperNumAdd Op = new OperNumAdd(Pro.NumAdd);
            OperNumAdd OpSimple = Pro.NumAdd;

            Console.WriteLine("Two nums sum is {0}", Op(102,100));
            Console.WriteLine("Simple delegate Two nums sum is {0}", OpSimple(1, 10));

            PrintPoem pr_poem = new PrintPoem(Poems);
            pr_poem();

            Console.Read();

        }


        delegate int OperNumAdd(int a, int b);  //委托声明
        private int NumAdd(int a, int b)
        {
            int sum = 0;
            sum = a + b;
            return sum;
        }

        //输出诗词
        private  static void Poems()
        {
            //字符串太长时在前面加@,则可以实现随意回车换行。
            String str_poem = @" 帝高阳之苗裔兮,朕皇考曰伯庸。
                                                摄提贞于孟陬兮,惟庚寅吾以降。
                                                皇览揆余初度兮,肇锡余以嘉名:
                                                名余曰正则兮,字余曰灵均。
                                                纷吾既有此内美兮,又重之以修能。
                                                扈江离与辟芷兮,纫秋兰以为佩。
                                                汩余若将不及兮,恐年岁之不吾与。
                                                朝搴阰之木兰兮,夕揽洲之宿莽。
                                                日月忽其不淹兮,春与秋其代序。
                                                惟草木之零落兮,恐美人之迟暮。
                                                不抚壮而弃秽兮,何不改乎此度?
                                                乘骐骥以驰骋兮,来吾道夫先路!";

            //常用第二种字符串太长的方式。
            String  strs = "庐山烟雨浙江潮,";
                        strs += "未至千般恨不消。";
                        strs += "到得还来别无事,";
                        strs += "庐山烟雨浙江潮。";

            Console.WriteLine(strs);
            Console.WriteLine();
            Console.WriteLine(str_poem);
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DelegateStudy
{
    public delegate void PrintPoem();

    class Program
    {
        static void Main(string[] args)
        {
            const string poemName = "LiSao";
            const string poet = "QuYuan";

            //调用函数参数的不同写法
            PrintPoetPoems(Poems_QuYuan_LiSao, poemName, poet);
            PrintPoetPoems(poet: "SuShi", poemName: "LuShanYanYu", pp: Poems_SuShi_LSYY);
            PrintPoetPoems(poet: "LiQiangZhao", pp:Poems_LiQingZhao_RuMengLing, poemName:"RuMengLing");
            //思考使用委托的好处是什么呢?

            Console.Read();
        }

        //如何让函数参数像C/C++里面一样是const不能修改只能用呢????
         private static void PrintPoetPoems(PrintPoem pp,  String poemName, string poet)
         {
            pp();  //调用委托输出诗词
            Console.WriteLine("This is {0}, the author is {1}!", poemName, poet);
            Console.WriteLine(poet +" "+  poemName + " " + "is good!");
        }

      //输出诗词
        private static void Poems_QuYuan_LiSao()
        {
            String str_poem = @" 帝高阳之苗裔兮,朕皇考曰伯庸。
                                                摄提贞于孟陬兮,惟庚寅吾以降。
                                                皇览揆余初度兮,肇锡余以嘉名:
                                                名余曰正则兮,字余曰灵均。
                                                纷吾既有此内美兮,又重之以修能。
                                                扈江离与辟芷兮,纫秋兰以为佩。
                                                汩余若将不及兮,恐年岁之不吾与。
                                                朝搴阰之木兰兮,夕揽洲之宿莽。
                                                日月忽其不淹兮,春与秋其代序。
                                                惟草木之零落兮,恐美人之迟暮。
                                                不抚壮而弃秽兮,何不改乎此度?
                                                乘骐骥以驰骋兮,来吾道夫先路!";

            Console.WriteLine(str_poem);
        }
        private static void Poems_SuShi_LSYY()
        {
            String strs = "庐山烟雨浙江潮,";
            strs += "未至千般恨不消。";
            strs += "到得还来别无事,";
            strs += "庐山烟雨浙江潮。";

            Console.WriteLine(strs);
        }

        private static void Poems_LiQingZhao_RuMengLing()
        {
            String strs = "常记溪亭日暮,沉醉不知归路。";
            strs += "兴尽晚回舟,误入藕花深处。";
            strs += "争渡,争渡,惊起一滩鸥鹭。";

            Console.WriteLine(strs);
        }
    }
}

    


猜你喜欢

转载自blog.csdn.net/moonlightpeng/article/details/80629886