√ C# - 11.怎么使用委托(P387、P390)

本文详细介绍了C#中的委托及其使用,包括无返回值的Action和有返回值的Func泛型委托,同步与异步调用方法,以及多播委托的概念和操作。通过实例展示了如何创建和使用自定义委托,强调了默认委托在大多数情况下的适用性。
摘要由CSDN通过智能技术生成
  1. 委托是一种特殊的类,它允许捕捉对方法的引用,并像传递其他对象那样传递该引用,像调用其他方法那样调用该方法。
  2. 默认委托包括无返回值的泛型委托Action<>、有返回值的泛型委托Func<>
  3. 调用方法的方式有两种,一种是利用方法名直接调用,另一种是利用委托间接调用。
  4. 同步调用是在同一线程内依次调用,异步调用是在不同线程内同时调用。
  5. 直接同步调用:利用方法名。
  6. 间接同步调用:调用委托的Invoke()。
  7. 显式异步调用:利用Thread类或者Task类。
  8. 隐式异步调用:调用委托的BeginInvoke()。
  9. 调用Invoke()后需要等待方法执行完毕才能继续执行主线程,调用BeginInvoke()后可以继续执行主线程。
  10. 多播委托是利用操作符【+=】和【-=】将多个方法的引用赋给委托对象,使其一次性可以依次调用多个方法。
  11. 一般情况下默认委托完全可以满足需求,不需要再声明自定义委托。
using System;

namespace _11
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("*****测试无参的Action*****");
            Action action1 = new Action(Test1.Print1);
            action1();
            Console.WriteLine("==========");
            action1 += Test2.Print1;
            action1();
            Console.WriteLine("==========");
            action1 -= Test1.Print1;
            action1.Invoke();
            Console.WriteLine("==========");
            action1 -= Test2.Print1;
            action1?.Invoke();
            Console.WriteLine("==========");
            Console.WriteLine();

            Console.WriteLine("*****测试有参的Action*****");
            Action<string> action2 = new Action<string>(Test1.Print2);
            action2("Test1");
            Console.WriteLine("==========");
            action2 += Test2.Print2;
            action2("Test1Test2");
            Console.WriteLine("==========");
            action2 -= Test1.Print2;
            action2.Invoke("Test2");
            Console.WriteLine("==========");
            action2 -= Test2.Print2;
            action2?.Invoke("null");
            Console.WriteLine("==========");
            Console.WriteLine();

            Console.WriteLine("*****测试一个参数的Func*****");
            Func<string, string> func1 = new Func<string, string>(Test1.Print3);
            Console.WriteLine(func1.Invoke("Test1"));
            Console.WriteLine("==========");
            Console.WriteLine();

            Console.WriteLine("*****测试多个参数的Func*****");
            Func<string, int, string> func2 = new Func<string, int, string>(Test1.Print4);
            Console.WriteLine(func2.Invoke("Test1", 3));
            Console.WriteLine("==========");
            Console.WriteLine();

            Console.WriteLine("*****测试自定义委托*****");
            Delegete1 delegete1 = new Delegete1(Test1.Print1);
            delegete1.Invoke();
            Console.WriteLine("==========");

            Delegete2 delegete2 = new Delegete2(Test1.Print2);
            delegete2.Invoke("Test1");
            Console.WriteLine("==========");

            Delegete3 delegete3 = new Delegete3(Test1.Print3);
            Console.WriteLine(delegete3.Invoke("Test1"));
            Console.WriteLine("==========");

            Delegete4<string, int, string> delegete4 = new Delegete4<string, int, string>(Test1.Print4);
            Console.WriteLine(delegete4.Invoke("Test1", 3));
            Console.WriteLine("==========");
            Console.WriteLine();
        }
    }

    class Test1
    {
        public static void Print1()
        {
            Console.WriteLine("Test1");
        }

        public static void Print2(string str)
        {
            Console.WriteLine(str);
        }

        public static string Print3(string str)
        {
            return str;
        }

        public static string Print4(string str, int count)
        {
            string newStr = str;
            for (int i = 1; i < count; i++)
            {
                newStr += str;
            }
            return newStr;
        }
    }

    class Test2
    {
        public static void Print1()
        {
            Console.WriteLine("Test2");
        }

        public static void Print2(string str)
        {
            Console.WriteLine(str);
        }
    }

    delegate void Delegete1();

    delegate void Delegete2(string str);

    delegate string Delegete3(string str);

    delegate TResult Delegete4<T1, T2, TResult>(T1 t1, T2 t2);
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值