首页 > 技术文章 > Net中委托之一

gylhaut 2016-08-18 20:56 原文

1.委托的用法

委托是一种特殊的类型

a. 委托可以类外定义,也可以在类里面定义

b. 委托的操作步骤

   1.委托的声明

   2.委托的实例化

   3.委托的调用 

2.委托实例

amespace MyDelegate
{
    //委托可以声明类的外面也可以声明类的里面
    public delegate void WithParameterNoReturnOutClass(string para);
    public class MyDelegate
    {
        private delegate void NoParameterNoReturn();//1.声明委托
        public static void Show()
        {
            NoParameterNoReturn method = new NoParameterNoReturn(ShowSomething);//2.委托的实例化
            //委托也可以这样实例化
           // NoParameterNoReturn method = ShowSomething;
            method.Invoke();//3.委托实例的调用
            //method(); 委托实例的调用也可以这么写
        }
        private static void ShowSomething()
        {
            Console.WriteLine("这里是ShowSomething");
        }
    }
}

namespace MyDelegate
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("欢迎来到流星小子博客学习");
            MyDelegate.Show();
            Console.Read();
        }
    }
}

 3.输出结果

推荐阅读