当前位置: 移动技术网 > IT编程>开发语言>.net > C#基础委托回顾

C#基础委托回顾

2018年09月14日  | 移动技术网IT编程  | 我要评论

权力的游戏第七季第一集下载,hb商店,安闲自得的意思

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();
}



expression

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网