(第三季)委托(201~202)203-Action委托207 多播委托 208 匿名方法

什么是委托?

  如果我们要把方法当做参数来传递的话,就要用到委托。简单来说委托是一个类型,这个类型可以赋值一个方法的引用。

声明委托

在C#中使用一个类分两个阶段,首选定义这个类,告诉编译器这个类由什么字段和方法组成的,然后使用这个类实例化对象。在我们使用委托的时候,也需要经过这两个阶段,首先定义委托,告诉编译器我们这个委托可以指向哪些类型的方法,然后,创建该委托的实例。

定义委托的语法如下:

  delegate void IntMethodInvoker(int x);

定义了一个委托叫做IntMethodInvoker,这个委托可以指向什么类型的方法呢?

  这个方法要带有一个int类型的参数,并且方法的返回值是void的。

定义一个委托要定义方法的参数和返回值,使用关键字delegate定义。

定义委托的其他案例:

  delegate double TwoLongOp(long first,longsecond);

  delegate string GetAString();

使用委托

privatedelegate string GetAString();

staticvoid Main(){

  int x = 40;

  GetAString firstStringMethod = newGetAString(x.ToString);

  Console.WriteLine(firstStringMethod());

}

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

namespace taotao
{
    class Class1
    {
        // 定义一个委托的类型 ,这个委托类型的名字叫做GetAString();
        private delegate string GetAString();
        static void Main()
        {
            int x = 40;
           // 使用委托类型 创建实例
            //GetAString a = new GetAString(x.ToString);  // a指向了x中的ToString()方法
            GetAString a = x.ToString;


            //string s = a(); // 通过委托实例去调用方法
            string s = a.Invoke();  // 通过invoke方法调用a所引用的方法
            Console.WriteLine(s);   // 通过委托类型调用一个方法,跟直接调用方法作用是一样的
            //
            Console.ReadKey();
        }
    }
}


在这里我们首先使用GetAString委托声明了一个类型叫做fristStringMethod,接下来使用new对它进行初始化,使它引用到x中的ToString方法上,这样firstStringMethod就相当于x.ToString,我们通过firstStringMethod()执行方法就相当于x.ToString()

通过委托示例调用方法有两种方式

  fristStringMethod();

  firstStringMethod.Invoke();

委托的赋值

GetAStringfirstStringMethod = new GetAString(x.ToString);只需要把方法名给一个委托的构造方法就可以了

GetAStringfirstStringMethod = x.ToString;也可以把方法名直接给委托的实例

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

namespace taotao
{
    class Class1
    {
        static void Main()
        {
            //实例我使用委托类型作为方法的参数

            PrintString method = Method1;
            PrintStr(Method1);
            method = Method2;
            PrintStr(Method2);
            Console.ReadKey();
        }

        private delegate void PrintString();

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

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

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



简单委托示例

定义一个类MathsOperations里面有两个静态方法,使用委托调用该方法
class MathOperations{
	public static double MultiplyByTwo(double value){
		return value*2;
	}
	public static double Square(double value){
		return value*value;
	}
}
delegate double DoubleOp(double x);
static void Main(){
	DoubleOp[] operations={ MathOperations.MultiplyByTwo,MathOperations.Square };
	for(int i =0;i<operations.Length;i++){
		Console.WriteLine("Using operations "+i);
		ProcessAndDisplayNumber( operations[i],2.0 );
	}
}
static void ProcessAndDisplayNumber(DoubleOp action,double value){
	double res = action(value);
	Console.Writeline("Value :"+value+" Result:"+res);
}

Action委托和Func委托

除了我们自己定义的委托之外,系统还给我们提供过来一个内置的委托类型,Action和Func

Action委托引用了一个void返回类型的方法,T表示方法参数,先看Action委托有哪些

Action

Action<inT>

Action<inT1,in T2>

Action<inT1,in T2 ....  inT16>多有16种类型哦

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

namespace taotao
{
    class Program
    {
        static void Main(string[] args)
        {
            Action a = PrintString;
            Action<string> b = PrintString;

            Action<int, int> c = PrintDouble;
        }

        static void PrintDouble(int i, int j) { }

        static void PrintInt(int i)
        { }

        static void PrintString(string str) { }

        static void PrintString()
        {
            Console.WriteLine("Hello world!");  //  系统内置(预定义)的一个委托类型,它可以
            //指向一个没有返回值,没有参数的方法
            //Action<int> a = PrintInt;
            
        }
    }
}

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

namespace taotao
{
    class Class1
    {
        static void PrintString()
        {
            Console.WriteLine("hello world!");
        }

        static void PrintString(int a)
        {
            Console.WriteLine("hello world!");
        }

        static void PrintString(string a)
        {
            Console.WriteLine("hello world!");
        }

        static void Double(int a , int b)
        {
            Console.WriteLine(a + b);
        }

        static void Main()
        {
            // 返回值,没有参数的方法。
            // action是系统内(预定义)的一个委托类型,它可以指向一个没有
            Action a = PrintString;
            Action<int> b = PrintString;//定义了一个委托类型,该类型指向没有返回值,有一个参数int的方法。
            Action<string> c = PrintString;
            // Action最多有16个参数
            Action<int, int> d = Double;
            d(6, 8);
            // Action 可以后面通过泛型去指定action指向的多个参数的类型,参数的
            // 类型跟action后面的声明的委托类型是对应着的
            Console.ReadKey();
        }
    }
}


Func引用了一个带有一个返回值的方法,它可以传递0或者多到16个参数类型,和一个返回类型

Func<outTResult>

Func<inT,out TResult>

Func<intT1,inT2,,,,,,in T16,out TResult>

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

namespace taotao
{
    class Program
    {
        static void Main(string[] args)
        {
            Func<int> a = Test1;//Func中的类型指定的是方法的返回值类型
            Console.WriteLine(a());
            //

            Func<string, int> b = Test2;    // func 最后一个类型是返回值类型,前面是参数类型(最多16个),先写参数类型,最后一个是返回值类型
            int j = b("sdfdsf");

            Console.ReadKey();
        }

        static int Test1()
        {
            return 1;
        }

        static int Test2(string str)
        {
            Console.WriteLine(str);
            return 2;
        }
    }
}

多播委托

前面使用的委托都只包含一个方法的调用,但是委托也可以包含多个方法,这种委托叫做多播委托。使用多播委托就可以按照顺序调用多个方法,多播委托只能得到调用的最后一个方法的结果,一般我们把多播委托的返回类型声明为void。

  Action action1 = Test1;

  action2+=Test2;

  action2-=Test1;

多播委托包含一个逐个调用的委托集合,如果通过委托调用的其中一个方法抛出异常,整个迭代就会停止。  


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


Actiona1 = Method1;

a1+=Method2;

Delegate[]delegates=a1.GetInvocationList();

foreach(delegated in delegates){

  //d();

  d.DynamicInvoke(null);

}

遍历多播委托中所有的委托,然后单独调用

/*

猫 叫  老鼠跑,主人醒
*/

using System;
namespace CatCry
{
    class MainClass
    {

        static void Test1()
        {
            Console.WriteLine("Test1");
            //throw new Exception();
        }

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

        static void Main()
        {
            // 多播委托
            Action a = Test1;
            a += Test2; // 表示添加一个委托的引用
            //
           // a -= Test1;
            //a -= Test2; // 当一个委托没有指向任何方法的时候,调用的话会出现异常。
            //if (a != null)
            //    a();
            //
            Delegate[] delegates = a.GetInvocationList();
            foreach (var item in delegates)
            {
                item.DynamicInvoke();
            }

            Console.ReadKey();
        }
    }
}


匿名方法

到目前为止,使用委托,都是先定义一个方法,然后把方法给委托的实例。但还有另外一种使用委托的方式,不用去定义一个方法,应该说是使用匿名方法(方法没有名字)。

Func<int,int,int>plus = delegate (int a,int b){

  int temp = a+b;

  return temp;

};

intres = plus(34,34);

Console.WriteLine(res);

在这里相当于直接把要引用的方法直接写在了后面,优点是减少了要编写的代码,减少代码的复杂性


/*

猫 叫  老鼠跑,主人醒
*/

using System;
namespace CatCry
{
    class MainClass
    {
        static int Test1(int arg1, int arg2)
        {
            return arg1 + arg2;
        }

        static void Main()
        {
            //Func<int, int, int> plus = Test1;

            // 修改成: 匿名方法  的形式
            Func<int, int, int> plus = delegate (int arg1, int arg2)
            {
                return arg2 + arg1;
            };

            // 匿名方法本质上是一个方法,只是没有名字,任何使用委托变量的地方都可以使用匿名方法赋值
            
            Console.ReadKey();
        }
    }
}




好的,您的问题是关于委托和匿名函数的一些方面。我来逐一回答您的问题: 1. 泛型委托:泛型委托是一种可以用于任何类型委托。它可以在运行时指定委托类型,使得委托可以适用于不同的类型。泛型委托使用泛型类型参数来定义委托的参数类型和返回值类型。 2. 委托作为函数参数:在C#中,可以把委托作为函数的参数传递。这样可以让函数接受一个可以被调用的方法作为参数,从而达到动态调用方法的目的。委托作为函数参数的语法非常简单,只需要将委托类型作为函数参数即可。 3. Action和Func委托Action和Func都是.NET Framework中提供的通用委托类型。它们可以用于定义不同类型委托,其中Action用于不返回值的委托,而Func用于返回值的委托。例如,Action可以用于定义一个没有参数的委托,而Func可以用于定义一个接受一个字符串参数并返回一个整数值的委托。 4. 委托多播多播委托是一种可以包含多个委托实例的委托。它可以在运行时将多个委托实例组合成一个委托链,从而实现对多个方法的同时调用。多播委托的语法非常简单,只需要使用"+"和"-"运算符来添加和移除委托实例即可。 5. 事件:事件是一种特殊的委托,它只能被用于定义类成员和事件处理程序。事件可以让类的用户注册和注销事件处理程序,从而在事件发生时自动调用事件处理程序。事件的语法非常简单,只需要使用"event"关键字来定义事件,然后通过委托来实现事件的调用。 6. 普通匿名函数:C#中的匿名函数是一种没有名称的函数,它可以在定义时直接将其作为参数或赋值给委托变量。普通匿名函数是一种可以接受参数并且有返回值的匿名函数。普通匿名函数的语法非常简单,只需要使用"delegate"关键字来定义匿名函数的委托类型,然后使用"()"和"{}"来定义函数体即可。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值