学习委托

using System; //无参数的委托 namespace ConsoleApp1
{
    class Program
    {         delegate void MyDel();         public void Print1()
        {
            Console.WriteLine("I am print 1");
        }         public void Print2() { Console.WriteLine("I am print 2"); }
        static void Main(string[] args)
        {
          
            Program program = new Program();             MyDel myDe = program.Print1;
            myDe += program.Print2;             myDe();             Console.ReadKey();           }
    }
} ------------------------------------------------ using System; namespace ConsoleApp1
{
    class Program
    {         delegate void MyDel(string mystring);//有参数的委托         public void Print1(string mystring)
        {
            Console.WriteLine(mystring+"I am print 1");
        }         public void Print2(string mystring) { Console.WriteLine(mystring+ "I am print 2"); }
        static void Main(string[] args)
        {
          
            Program program = new Program();             MyDel my;
            my = program.Print2;//方法后面不加()
            my += program.Print1;             my("KKK");//最后运行委托时,后面再加括号
       
            Console.ReadKey();           }
    }
}
上一篇:.net Strings.Split静态方法和mystring.Split 实例方法在处理空字符串时的不同


下一篇:关于函数返回值/打印语句为忐或烫的问题