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

原文地址:https://www.cnblogs.com/pengxinglove/p/5449416.html