zoukankan      html  css  js  c++  java
  • 委托的使用

    namespace ConsoleApplication2
    {
        //最简单的委托的用法-----------------------------------
        //delegate void SayHello(string pString);//声明一个委托
        //class Program
        //{
        //    static void Main(string[] args)
        //    {
        //        SayHello sh = EnglishHello;
        //        sh("English");
        //        sh = new Program().ChineseHello;
        //        sh("Chinese");
        //        Console.Read();
        //    }
        //    private void ChineseHello(string message)
        //    {
        //        Console.WriteLine(message);
        //    }
        //    private static void EnglishHello(string message)
        //    {
        //        Console.WriteLine(message);
        //    }
        //}
        //-----------------------------------------------------
        //进化为匿名方法
        //delegate void SayHello(string message);
        //class Program
        //{
        //    static void Main(string[] args)
        //    {
        //        SayHello sh = delegate(string message) { Console.WriteLine(message); };
        //        sh("hello");
        //        Console.Read();
        //    }
        //}
        //-----------------------------------------------------
        //lamda表达式
        //delegate void SayHello(string message);
        //class Program
        //{
        //    static void Main(string[] args)
        //    {
        //        SayHello sh = x => { Console.WriteLine(x); };
        //        sh("lamda");
        //        Console.Read();
        //    }
        //}
        //-----------------------------------------------------
        //上面的代码都要声明一个委托,那么可以不声明委托吗,当然可以了,那么就涉及到两个系统声明的委托方法Action、Func
        //Action  该方法只有一个参数,没有返回值
        //public delegate void Action<in T>(T obj)
        //Func封装一个具有一个参数并返回 TResult 参数指定的类型值的方法。
        //public delegate TResult Func<in T, out TResult>(T arg)

        更多请查看MSDN介绍  ActionFunc
        class Program
        {
            static void Main(string[] args)
            {
                Action<string> ac = sayHello;
                ac("Action");
                Action<string,int> ac1=sayHello1;
                ac1("Action",1);
                Action<string> ac2 = delegate(string message) { Console.WriteLine("ac2 "+message); };
                ac2("ac2message");
                Action<string> ac3 = s => { Console.WriteLine("ac3 "+s); };
                ac3("ac3message");

                Func<int, string> fc = sayHello2;
                Console.WriteLine(fc(1));
                Console.Read();
            }
            private static void sayHello(string message)
            {
                Console.WriteLine("hello word"+message);
            }
            private static void sayHello1(string message1,int message2)
            {
                Console.WriteLine("hello word" + message1+"-"+message2);
            }
            private static string sayHello2(int message1)
            {
                return ("hello word" + message1 + "-func");
            }
        }
        //
        //-------------------------------------------------------
        //class Program
        //{
        //    static void Main(string[] args)
        //    {
        //    }
        //}
    }

  • 相关阅读:
    已经连接到空闲例程的解决方法
    SQLplus命令中导出数据
    Oracle设置时间格式
    Oracle热备份
    多元化控制文件
    ORA28000: the account is locked
    Windows 7 如何删除 Windows.old 文件夹
    Linux常用命令之文件命令
    Linux 常用命令之进程管理
    Python中的apply,filter和map函数
  • 原文地址:https://www.cnblogs.com/yidianfeng/p/2235683.html
Copyright © 2011-2022 走看看