C#基础委托回顾

C#基础委托回顾

前言
  • 快忘记了。
委托的特点
  • 委托类似于 C++ 函数指针,但它们是类型安全的。
  • 委托允许将方法作为参数进行传递。
  • 委托可用于定义回调方法。
  • 委托可以链接在一起;例如,可以对一个事件调用多个方法。
  • 方法不必与委托签名完全匹配。
  • 委托是事件的基础。

官网介绍

用法
  1. delegate
  • 至少0个参数,至多32个参数,可以无返回值,也可以指定返回值类型
  • 示例:
public delegate Int32 ComputeDelegate(Int32 a, Int32 b);
private static Int32 Compute(Int32 a, Int32 b)
{
  return a + b;
}

public delegate Int32 ComputeDelegate2(Int32 a, Int32 b);
//delegate参数
private static Int32 ReceiveDelegateArgsFunc(ComputeDelegate2 func)
{
  return func(1, 2);
}
private static Int32 DelegateFunction(Int32 a, Int32 b)
{
  return a + b;
}
  1. Action(无返回值的泛型委托)
  • Action可以接受0个至16个传入参数,无返回值
  • 示例:
public static void TestAction<T>(Action<T> action, T t)
{
    action(t);
}
private static void Printf(string s)
{
    Console.WriteLine($"3、{s}");
}
private static void Printf(int s)
{
    Console.WriteLine($"3、{s}");
}
  1. Func<T,TResult> 是有返回值的泛型委托
  • 封装一个具有一个参数并返回 TResult 参数指定的类型值的方法
  • public delegate TResult Func<in T, out TResult>(T arg)
  • 可以接受0个至16个传入参数,必须具有返回值
public static int TestFunc<T1, T2>(Func<T1, T2, int> func, T1 a, T2 b)
{
    return func(a, b);
}
private static int Fun(int a, int b)
{
    return a + b;
}
  1. predicate(bool型的泛型委托)
  • 只能接受一个传入参数,返回值为bool类型
  • 表示定义一组条件并确定指定对象是否符合这些条件的方法。此委托由 Array 和 List 类的几种方法使用,用于在集合中搜索元素
private static bool ProductGT10(Point p)
{
    if (p.X * p.Y > 100000)
    {
        return true;
    }
    else
    {
        return false;
    }
}
  1. Expression<Func<T,TResult>>是表达式
 Expression<Func<int, int, int, int>> expr = (x, y, z) => (x + y) / z;
  1. 委托清理
  • 示例1:循环依次循环
public ComputeDelegate OnDelegate;
public void ClearDelegate()
{
    while (OnDelegate != null)
    {
        OnDelegate -= OnDelegate;
    }
}
  • 示例2:利用GetInvocationList方法
static void Main(string[] args)
{
    Program2 test = new Program2();
    if (test.OnDelegate != null)
    {
        Delegate[] dels = test.OnDelegate.GetInvocationList();
        for (int i = 0; i < dels.Length; i++)
        {
             test.OnDelegate -= dels[i] as ComputeDelegate;
        }
    }
}
  1. sample
static void Main(string[] args)
{
    ComputeDelegate computeDelegate = new ComputeDelegate(Compute);
    Console.WriteLine($"1、sum:{computeDelegate(1, 2)}");

    ComputeDelegate2 computeDelegate2 = new ComputeDelegate2(DelegateFunction);
    Console.WriteLine($"2、sum:{ReceiveDelegateArgsFunc(computeDelegate2)}");

    TestAction<String>(Printf, "action");
    TestAction<Int32>(Printf, 12);
    TestAction<String>(x => { Printf(x); }, "hello action!");//Lambda

    Console.WriteLine($"4、{ TestFunc(Fun, 1, 2)}");

    Point[] points = {
        new Point(100, 200),
        new Point(150, 250),
        new Point(250, 375),
        new Point(275, 395),
        new Point(295, 450) };
    Point first = Array.Find(points, ProductGT10);
    Console.WriteLine($"5、 X = {first.X}, Y = {first.Y}");

    Expression<Func<int, int, int, int>> expr = (x, y, z) => (x + y) / z;
    Console.WriteLine($"6、{expr.Compile()(1, 2, 3)}");

    Console.WriteLine("Press any key...");
    Console.ReadKey();
}

官网介绍


C#委托介绍


Expression

posted @ 2018-09-13 14:40  wzh0717  阅读(510)  评论(0编辑  收藏  举报