C# 委托

7 篇文章 1 订阅
本文介绍了C#中的委托概念,包括如何声明、实例化和调用委托,以及委托如何用于方法的传递。同时讲解了无返回值的泛型委托Action和有返回值的泛型委托Func的使用。此外,还探讨了匿名方法和Lambda表达式的应用,并展示了多播委托如何关联和调用多个方法。
摘要由CSDN通过智能技术生成

委托 Delegate

什么是委托?
委托是一个类,在实例化委托时,可以将委托的实例与具有相同返回值类型的方法相关联,这样就可以通过委托来调用方法。另外,使用委托还可以将方法作为参数传递给其他方法。

委托的使用

  1. 声明委托
  2. 有一个或以上与委托具有相同类型的返回值和参数,且参数个数相同的方法。
  3. 实例化委托,使用 new 关键字。
  4. 调用委托

示例:

using System;

namespace 委托的同步_异步
{
    //1.声明委托
    public delegate void DelPrintln(string msg);
    class Program
    {
        static void Main(string[] args)
        {
            //3.实例化委托
            DelPrintln println = new DelPrintln(Println);
            //4.调用委托
            println("我是委托");  
            
            Console.ReadKey();              
        }
        //2.写一个与委托返回值类型相同,参数类型和个数相同的方法
        static void Println(string text)
        {
            Console.WriteLine(text);
        }
    }      
}

泛型委托

  1. Action 没有返回值的泛型委托
    示例1:没有参数没有返回值的泛型委托
using System;

namespace 委托的同步_异步
{
    class Program
    {
        static void Main(string[] args)
        {
            //1.声明没有参数没有返回值的泛型委托
            Action action1;
            //3.给泛型委托赋值
            action1 = new Action(M1);
            //4.调用泛型委托
            action1();

            Console.ReadKey();
        }
        //2.写一个没有参数没有返回值的方法
        static void M1()
        {
            Console.WriteLine("我是没有参数没有返回值的方法");
        }
    }
}

示例2:一个参数为 string 类型没有返回值的泛型委托

using System;

namespace 委托的同步_异步
{
    class Program
    {
        static void Main(string[] args)
        {
            //1.声明没有参数没有返回值的泛型委托
            Action<string> action1 ;
            //3.给泛型委托赋值
            action1 = new Action<string>(M1);
            //4.调用泛型委托
            action1("我是有一个参数类型为string类型的没有返回值的方法");

            Console.ReadKey();
        }
        //2.写一个参数为string类型没有返回值的方法
        static void M1(string msg)
        {
            Console.WriteLine(msg);
        }
    }
}

  1. Func 有返回值的泛型委托
    实例1:两个参数为 int ,返回值为 int 的泛型委托
using System;

namespace 委托的同步_异步
{
    class Program
    {
        static void Main(string[] args)
        {
            //1.声明
            Func<int, int, int> func;
            //2.赋值
            func = new Func<int, int, int>(Add);
            //3.调用
            int sum = func(1,2);

            Console.WriteLine(sum);
            Console.ReadKey();
        }
        //2.写一个有两个int参数且返回值为int的方法
        static int Add(int a,int b)
        {
            return a + b;
        }
    }
}

匿名方法 Lambda表达式

using System;

namespace 委托的同步_异步
{
    class Program
    {
        static void Main(string[] args)
        {
            Action action1 = Test;
            action1();

            Action action2 = delegate() { Console.WriteLine("匿名方法"); } ;
            action2();

            Action action3 = () => { Console.WriteLine("Lambda表达式"); };
            action3();



            Func<int, int, int> func1 = Add;
            func1(1,1);

            Func<int, int, int> func2 = delegate (int a, int b) { return a + b; };
            func2(2,2);

            Func<int, int, int> func3 = (int a, int b) => { return a + b; };
            func3(3,3);

            Console.ReadKey();
        }
        
        static void Test()
        {
            Console.WriteLine("Test方法");
        }

        static int Add(int a,int b)
        {            
            return a + b;
        }
    }
}

多播委托

一个委托可以关联一个或多个方法,当委托中只有一个关联方法称为单播委托,当委托关联多个方法称为多播委托。
实例:

using System;

namespace 委托的同步_异步
{
    class Program
    {
        static void Main(string[] args)
        {
            Action action1 = Test1;
            action1 += Test2;
            action1 += Test3;

            action1();

            Console.ReadKey();
        }
        
        static void Test1()
        {
            Console.WriteLine("Test1方法");
        }

        static void Test2()
        {
            Console.WriteLine("Test2方法");
        }

        static void Test3()
        {
            Console.WriteLine("Test3方法");
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值