C#委托的进一步学习

一.委托的说明

namespace LearningCsharp
{
    class Program
    {
        //定义一个委托,使用delegate加上方法签名
        //将委托理解为存储方法的“数组”,方法签名指明了所存储的方法的返回值类型和参数类型,这里返回string,参数为空
        //所有符合定义的返回值类型和参数类型的方法都可以存储到“数组”中,不符合的不能存储
        delegate string IntToString();
        static void Main(string[] args)
        {
            int a = 10086;
            int b = 10010;
            int c = 10000;
            int d = 10001;
            //实例化一个委托,在实例化委托时使用new关键字,括号中的内容不是参数类型,而是在“委托数组”中存储的第一个方法,可以称之为注册一个方法
            //IntToString int2String = new IntToString(a.ToString);
            //也可以不使用new关键字,直接将第一个方法存储到一个委托中
            IntToString int2String = a.ToString;
//为“委托数组”添加和删减方法,多个方法的委托称为多播委托 int2String += b.ToString;//添加方法 int2String -= a.ToString;//删减方法,委托中所有方法都被删掉调用时会报错,因此调用委托时可以先判断是否为空再调用,防止调用空委托的情况出现 int2String += c.ToString; int2String += d.ToString; //调用委托中的方法 //委托中的方法一次性会被全部调用,有返回值的化返回最后存储的方法的返回值,也就是按照存储的顺序进行调用 //调用时需要指明参数,所有方法的参数类型都是一样的,当前委托的参数为空 string s = int2String();
       //这里也可以通过invoke方法调用委托中的方法(不常用)
            string s = int2String.Invoke(); Console.WriteLine(s); Console.ReadLine(); } } }

   返回的是最后注册的d.ToString()方法的返回值

二.委托的使用演示 

namespace LearningCsharp
{
    class Program
    {
        //定义委托
        private delegate void PrintString(string s);
       
        static void Main(string[] args)
        {
            //声明委托
            PrintString printString = Method1;
            //委托可以当作变量进行新的赋值
            printString = Method2;

            //调用方法,将委托当作参数传递
            PrintString2Console(printString);
        }

         /// <summary>
        /// 将委托作为参数传递,这个方法负责调用委托
        /// </summary>
        static void PrintString2Console(PrintString ps)
        {
            ps("我是大帅比");
            Console.ReadKey();
        }
        /// <summary>
        /// 两个供委托调用的方法method1和method2
        /// </summary>
        static void Method1(string s)
        {
            Console.WriteLine("method1" + s);
        }
        static void Method2(string s)
        {
            Console.WriteLine("method2" + s);
        }
    }
}

  

委托可以作为参数传递给方法,也可以当作参数进行赋值,和基本数据类型相似

三.Action委托

  这是系统内部已经定义好的委托类型,这个委托没有返回值没有参数,但是可以使用泛型是委托具有参数

namespace LearningCsharp
{
    class Program
    {
       
        static void Main(string[] args)
        {
            //系统已经定义好的Action委托
            Action a = Method1;
            //通过泛型指定Action委托的参数
            Action<int> aInt = Method2;
            //可以为委托指定多个参数,Action最多支持16个参数
            Action<int, int> aIntInt = Method3;
           
        }
        /// <summary>
        /// 供委托注册的方法们
        /// </summary>
        static void Method1()
        {
            return;
        }
        static void Method2(int i)
        {
            return;
        }
        static void Method3(int i1,int i2)
        {
            return;
        }
    }
}

四.Function委托

  这也是系统定义好的委托,但是要求方法必须有返回值,使用泛型指定返回值类型,也可以同时指定参数,泛型中最后一个类型为返回值类型

namespace LearningCsharp
{
    class Program
    {
       
        static void Main(string[] args)
        {
            //系统已经定义好的Function委托,无参数,返回值为int类型
            Func<int> fInt = Method1;
            //通过泛型指定委托的参数,有一个string参数,返回值为int类型
            Func<string,int> fStringInt = Method2;
            //通过泛型指定委托的参数,有两个string参数,返回值为int类型,参数最多16个
            Func<string,string, int> fStringStringInt = Method3;
           
        }
        /// <summary>
        /// 供委托注册的方法
        /// </summary>
        static int Method1()
        {
            return 1;
        }
        static int Method2(string s)
        {
            return 2;
        }
        static int Method3(string s1,string s2)
        {
            return 3;
        }
    }
}

 五.多播委托

  前面已经提到多播委托是委托中能存储不止一个方法,通过+=和-=的方式添加和删除方法。

namespace LearningCsharp
{
    class Program
    {
        //定义委托
        delegate void MyDelegate();
        static void Main(string[] args)
        {
            //多播委托
            MyDelegate myDel = Method1;
            myDel += Method2;
            myDel += Method3;
            //多播委托按照添加顺序执行,当遇到异常后续的方法都不会执行
            myDel();
           
        }
        /// <summary>
        /// 供委托注册的方法
        /// </summary>
        static void Method1()
        {
            Console.WriteLine("m1");
            return;
        }
        static void Method2()
        {
            Console.WriteLine("m2");
            //抛出异常
            throw new Exception();
        }
        static void Method3()
        {
            Console.WriteLine("m3");
            return;
        }
    }
}

 

 

原文地址:https://www.cnblogs.com/movin2333/p/14085135.html