c#委托的定义和使用

什么是委托?当你需要将方法当成一个参数传递的时候就需要使用委托,委托是一个类型,可以赋值一个方法的引用。具体怎么使用下面就简单展示一下。

1.定义委托

delegate void MyDelegate(int x);

定义委托要使用到delegate关键字,然后是返回值和参数。这里的例子就代表定义了一个叫做MyDelegate的委托,这个委托只能指向参数为int类型,返回值为空的方法。

2.使用委托

定义好了委托之后,我们就可以吧这个MyDelegate当成一个类使用了。然后我们就可以通过这个类创建变量了。

int x=40;

这里有两种赋值方式

①MyDelegate firstDelegate = new MyDelegate(x.ToString);

②MyDelegate firstDelegate = x.ToString;

注意这里的ToString后面并没有加上括号,也就是说并没有调用这个方法。实际上这里是把ToString方法赋值给firstDelegate这个变量。我们之后就可以使用firstDelegate这个变量来代替x.ToString()这个方法了。那么怎么通过委托调用ToString()这个方法。大致有两种方法。

扫描二维码关注公众号,回复: 15261529 查看本文章

①firstDelegte();

②firstDelegate.Invoke();

如果赋值的方法有参数,那么使用委托调用的时候也要加上参数。大部分情况赋值和调用还是采用第一种方法,更为简单一些。

同时委托类型本身也可以当成参数进行使用。

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

namespace Exercise
{
    class Program
    {
        private delegate void PrintString();

        static void PrintStr(PrintString print)
        {
            print();
        }

        static void Main(string[] args)
        {
            PrintString method = Method1;
            PrintStr(method);
            method = Method2;
            PrintStr(method);

            Console.ReadKey();
        }

        static void Method1()
        {
            Console.WriteLine("Method1");
        }

        static void Method2()
        {
            Console.WriteLine("Method2");
        }
    }
}

OK,上面都是我们自己定义的委托,实际上,系统已经帮我们预定义好了委托,叫做Action和Func,我们只需要知道怎么使用就可以了。

1.Action委托(没有返回值,参数可能有)

Action委托是没有参数没有返回值类型的委托。

定义和上面写的委托一样。

Action a = PrintString;

那么如果我们想要赋值一个有参数的该怎么办,系统其实对Action做了扩展,通过使用泛型的Action可以实现带参数的方法赋值。

Action<int,string> a;

这里的意思就是定义了一个委托类型的变量,可以指向没有返回值,有一个int类型和一个string类型的参数的方法。Action最大可以支持16个参数。但是一定要记得Action的类型和赋值的方法的参数要一一对应。

2.Func委托(必须有返回值,返回值的类型通过泛型指定)

Func<int,string> a;

Func委托泛型列表中最后一个参数是返回值类型,前面的都是参数列表。同样最多支持16个参数。

3.多播委托

前面的都是一个变量代表着一个方法,那么如果想通过调用这个委托可以实现多个方法该怎么做呢,就比如服务器想发消息到客户端,如果一个一个调用发送消息的方法太复杂了,如果能同时调用这些方法就方便多了,这就是多播委托的作用。

具体使用就很简单了,只需要添加方法就行

            Action a = Method1;
            a += Method2;//表示添加一个方法

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

namespace Exercise
{
    class Program
    {
        static void Main(string[] args)
        {
            Action a = Method1;
            a += Method2;//表示添加一个方法
            a();
            Console.ReadKey();
        }

        static void Method1()
        {
            Console.WriteLine("Method1");
        }

        static void Method2()
        {
            Console.WriteLine("Method2");
        }
    }
}

如果想要去掉一个方法就变成减号就OK啦。

取得多播委托中所有方法的委托

Delegate[] delegates = a.GetInvocationList();

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

namespace Exercise
{
    class Program
    {
        static void Main(string[] args)
        {
            Action a = Method1;
            a += Method2;
            a();
            Delegate[] delegates = a.GetInvocationList();
            foreach(Delegate d in delegates){
                d.DynamicInvoke();
            }

            Console.ReadKey();
        }

        static void Method1()
        {
            Console.WriteLine("Method1");
        }

        static void Method2()
        {
            Console.WriteLine("Method2");
        }
    }
}

以上就是委托的简单讲解啦。

猜你喜欢

转载自blog.csdn.net/qq_52397831/article/details/126958895