Delegate 多种写法总结

  • 委托是嘛?

委托是一个类型安全的对象,它指向程序中另一个以后会被调用的方法(或多个方法)。通俗的说,委托是一个可以引用方法的对象,当创建一个委托,也就创建一个引用方法的对象,进而就可以调用那个方法,即委托可以调用它所指的方法。

  • 如何使用委托?

1、定义委托类型

[访问修饰符]delegate 返回类型 委托名(形参);

2、声明委托对象

委托名 委托实例名;

3、创建委托对象(确定与哪些方法进行绑定)

委托实例名=new 委托名(某个类的方法)

4、使用委托调用方法

委托实例名(实参)

  • 委托注意事项:

1、委托和方法必须具有相同的参数。

2、委托可以调用多个方法,即一个委托对象可以维护一个可调用方法的列表而不是单独的一个方法,称为多路广播(多播)。

3、使用+=和-=运算实现方法的增加和减少

  • 示例
复制代码
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Delegate;

namespace Delegate
{

public delegate int Call(int num1, int num2);//第一步:定义委托类型
class SimpleMath
{
// 乘法方法
public int Multiply(int num1, int num2)
{
return num1 * num2;
}

// 除法方法
public int Divide(int num1, int num2)
{
return num1 / num2;
}
}
}
class Test
{
static void Main(string[] args)
{
Call objCall;//第二步:声明委托对象
// Math 类的对象
SimpleMath objMath = new SimpleMath();
// 第三步:创建委托对象,将方法与委托关联起来
objCall = new Call(objMath.Multiply);

Call objCall1 = new Call(objMath.Divide);
objCall += objCall1;//向委托增加一个方法
//objCall -= objCall1;//向委托减去一个方法
            // 调用委托实例,先执行objMath.Multiply,然后执行objMath.Divide
int result = objCall(5, 3);
System.Console.WriteLine("结果为 {0}", result);
Console.ReadKey();
}
}
 
复制代码

写法:

1、委托 委托名=new 委托(会调用的方法名); 委托名(参数);

2、委托 委托名 =会调用的方法名; 委托名(参数);

3、匿名方法

委托 委托名=delegate(参数){会调用的方法体};委托名(参数);

4、拉姆达表达式

委托 委托名=((参数1,。。参数n)=>{会调用的方法体});委托名(参数);

5、用Action<T>和Func<T>,第一个无返回值

Func<参数1, 参数2, 返回值> 委托名= ((参数1,参数2) => {带返回值的方法体 });返回值=委托名(参数1,参数2);

贴代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using @delegate;
 
namespace @delegate
{
        public delegate int Call(int num1, int num2);
        class SimpleMath
        {
            // 乘法方法
            public static int Multiply(int num1, int num2)
            {
                return num1 * num2;
            }
 
            // 除法方法
            public int Divide(int num1, int num2)
            {
                return num1 / num2;
            }
        }
    class Test
    {
        static void Main(string[] args)
        {
            //--------------------第一种写法------------------------//
            //Call objCall = new Call(SimpleMath.Multiply);
            //Call objCall1 = new Call(new SimpleMath().Divide);
            //--------------------第二种写法------------------------//
            //Call objCall = SimpleMath.Multiply;
            //Call objCall1 = new SimpleMath().Divide;
            //--------------------第三种写法------------------------//
            //Call objCall = delegate(int a, int b)
            //{
            //    return a * b;
            //};
            //Call objCall1 = delegate(int a, int b)
            //{
            //    return a / b;
            //};
            //--------------------第四种写法------------------------//
            //Call objCall =((int a,int b)=> { return a*b;});
            //Call objCall1 = ((int a, int b) => { return a / b; });
            //--------------------第五种写法------------------------//
            Func<int, int, int> objCall = ((a, b) => { return a * b; });
            Func<int, int, int> objCall1 = ((a, b) => { return a / b; });
             Action<int, int> ob = ((a, b) => { Console.WriteLine(a * b); });
             ob(5, 3);
            //----------------------------------------------------//
             int result = objCall(5, 3);
             int result1 = objCall1(5, 3);
             System.Console.WriteLine("结果1为 {0},结果2为{1}", result,result1);
            Console.ReadKey();
        }
    }
}

一、委托调用方式

1. 最原始版本:

复制代码
    delegate string PlusStringHandle(string x, string y);
    class Program
    {
        static void Main(string[] args)
        {
            PlusStringHandle pHandle = new PlusStringHandle(plusString);
            Console.WriteLine(pHandle("abc", "edf"));

            Console.Read();
        }

        static string plusString(string x, string y)
        {
            return x + y;
        }
    }
复制代码

 2. 原始匿名函数版:去掉“plusString”方法,改为

            PlusStringHandle pHandle = new PlusStringHandle(delegate(string x, string y)
            {
                return x + y;
            });
            Console.WriteLine(pHandle("abc", "edf"));

3. 使用Lambda(C#3.0+),继续去掉“plusString”方法(以下代码均不再需要该方法)

            PlusStringHandle pHandle = (string x, string y) =>
            {
                return x + y;
            };
            Console.WriteLine(pHandle("abc", "edf"));

还有更甚的写法(省去参数类型)

            PlusStringHandle pHandle = (x, y) =>
            {
                return x + y;
            };
            Console.WriteLine(pHandle("abc", "edf"));

如果只有一个参数

复制代码
        delegate void WriteStringHandle(string str);
        static void Main(string[] args)
        {
            //如果只有一个参数
            WriteStringHandle handle = p => Console.WriteLine(p);
            handle("lisi");

            Console.Read();
        }
复制代码

二、委托声明方式

1. 原始声明方式见上述Demo

2. 直接使用.NET Framework定义好的泛型委托 Func 与 Action ,从而省却每次都进行的委托声明。

复制代码
        static void Main(string[] args)
        {
            WritePrint<int>(p => Console.WriteLine("{0}是一个整数", p), 10);

            Console.Read();
        }

        static void WritePrint<T>(Action<T> action, T t)
        {
            Console.WriteLine("类型为:{0},值为:{1}", t.GetType(), t);
            action(t);
        }
复制代码

3. 再加上个扩展方法,就能搞成所谓的“链式编程”啦。

复制代码
    class Program
    {   
        static void Main(string[] args)
        {
            string str = "所有童鞋:".plusString(p => p = p + " girl: lisi、lili
").plusString(p => p + "boy: wangwu") ;
            Console.WriteLine(str);

            Console.Read();
        }
    }

    static class Extentions
    {
        public static string plusString<TParam>(this TParam source, Func<TParam, string> func)
        {
            Console.WriteLine("字符串相加前原值为:{0}。。。。。。", source);
            return func(source);
        }
    }
复制代码

看这个代码是不是和我们平时写的"list.Where(p => p.Age > 18)"很像呢?没错Where等方法就是使用类似的方式来实现的。

好了,我总结完了,如有遗漏,还望补上,臣不尽感激。

原文地址:https://www.cnblogs.com/JianGuoWan/p/9159711.html