zoukankan      html  css  js  c++  java
  • Lambda expressions , Action , Func and Predicate

    http://www.tutorialsteacher.com/csharp/csharp-action-delegate

    lambda 表达式

    Action,func

    lambda表达式是什么,其有什么优点,不使用lambda

    其的目的:简化代码。

    在JAVA当中一般是使用接口来实现

    Action is also a delegate type defined in the System namespace. An Action type delegate is the same as Func delegate except that the Action delegate doesn't return a value. In other words, an Action delegate can be used with a method that has a void return type.

    Action  is also a delegate type 其也可以推导出来两个结论。

    1. action 是一个type,类似于int, 那就可以定义变量,其定义一个什么样类型的变量,其变量的含义是什么。就需要下面一个推论。

    2. action 是一个delegate type,其就是一个delegate,那就是一个函数指针,用于方法之间的调用。

    其包含有delegate的所有特点,那其又有什么不同。

    3. 有了delegate那又要有Action,使用action其的目的是为了更加简化。

    public delegate void Print(int val);

    delegate 其需要先定义且要定义方法名

    Action<int>  其就直接定义形参的类型且不要返回值。
     

    Advantages of Action and Func delegates:

    1. Easy and quick to define delegates.
    2. Makes code short.
    3. Compatible type throughout the application.

    【action 其使用的场景】

    1. 在框架当中的线程之间使用

    this._readThread = ThreadEx.ThreadCall(new Action(this.ReadThread));

    public static System.Threading.Thread ThreadCall(System.Action action)
    {
    return ThreadEx.ThreadCall(action, null);
    }

    public static System.Threading.Thread ThreadCall(System.Action action, string name)
    {
    return ThreadEx.Call(action, name);
    }

    public static System.Threading.Thread Call(System.Action action, string name)
    {
        System.Threading.Thread thread = new System.Threading.Thread(new System.Threading.ThreadStart(action.Invoke));
    if (name != null)
        {
            thread.Name = name;
        }
        thread.Start();
    return thread;
    }

  • 相关阅读:
    设计模式之工厂模式 练习
    c++智能指针(1)
    记录下 UTF6 GBK 转换函数
    ip白名单 通过* ? 检测IP匹配 轻量级
    stl学习记录(2)
    boost 学习(1)
    stl string 小练习
    stl string 使用指定的分隔符分割成数个子字符串
    [open source] skinbuilder发布
    Builder模式实例分析(C语言版)
  • 原文地址:https://www.cnblogs.com/pengxinglove/p/5449416.html
Copyright © 2011-2022 走看看