lamda表达式

格式:( 形参列表 ) => { 函数体 }

作用:简化匿名方法的书写,可用在任何可使用匿名方法和强类型代理的地方;

Action是无返回值的泛型委托。

   Action 表示无参,无返回值的委托

   Action<int,string> 表示有传入参数int,string无返回值的委托

  Action<int,string,bool> 表示有传入参数int,string,bool无返回值的委托

Action<int,int,int,int> 表示有传入4个int型参数,无返回值的委托

   Action至少0个参数,至多16个参数,无返回值。

Func是有返回值的泛型委托

   Func<int> 表示无参,返回值为int的委托

   Func<object,string,int> 表示传入参数为object, string 返回值为int的委托

   Func<object,string,int> 表示传入参数为object, string 返回值为int的委托

   Func<T1,T2,,T3,int> 表示传入参数为T1,T2,,T3(泛型)返回值为int的委托

   Func至少0个参数,至多16个参数,根据返回值泛型返回。必须有返回值,不可void

实例:

通过Acton 分装用户方法,以两种更新界面UI  (源代码)

 private void btnFunc_Click(object sender, EventArgs e)

{
var n = TestFunc((N, Q) =>
{
return N * Q;
}, , ); txtResult.Text = "Func-结果是:" + n;
} private void btnAction_Click(object sender, EventArgs e)
{
TestAtction((N, Q) =>
{
txtResult.Text = "Action-结果" + (N * Q).ToString();
}, , );
} private void btnThrend_Click(object sender, EventArgs e)
{
txtResult.Text = string.Empty;
threadStatus = true;
new Thread(() =>
{
Dothread((n) =>
{
this.Invoke(new Action(() =>
{
txtResult.Text += string.Format("现在是:{0},第{1}次执行", DateTime.Now.ToString(), n.ToString()) + "\r\n";
}));
});
}).Start();
} private void btnthreaddelegate_Click(object sender, EventArgs e)
{
txtResult.Text = string.Empty;
threadStatus = true;
new Thread(() =>
{
Dothread((n) =>
{
WriteInvoke(string.Format("现在是:{0},第{1}次执行", DateTime.Now.ToString(), n.ToString()) + "\r\n");
});
}).Start();
} public void TestAtction(Action<int, int> ac, int n, int q)
{
ac(n, q);
} public int TestFunc(Func<int, int, int> func, int a, int b)
{
return func(a, b);
} private static bool threadStatus; #region "线程UI委托"
delegate void WriteInvokeCallBack(string msg);
void WriteInvoke(string msg)
{
if (this.InvokeRequired)
{
WriteInvokeCallBack Wc = new WriteInvokeCallBack(WriteInvoke);
this.Invoke(Wc, msg);
}
else
{
txtResult.Text += msg;
}
}
#endregion private void Dothread(Action<int> ac)
{
lock (this)
{
int i = ;
while (threadStatus)
{
i += ;
ac(i);
if (i > ) threadStatus = false;
Thread.Sleep();
}
}
}

Lamda Action Func Thread 实例的更多相关文章

  1. VS2012 Unit Test(Void, Action, Func) —— 对无返回值、使用Action或Func作为参数、多重载的方法进行单元测试

    [提示] 1. 阅读文本前希望您具备如下知识:了解单元测试,了解Dynamic,熟悉泛型(协变与逆变)和Lambda,熟悉.NET Framework提供的 Action与Func委托.2.如果您对单 ...

  2. 关于struts和Spring 结合到一起之后存在ACtion创建单实例还是多

    struts 2的Action是多实例的并非单例,也就是每次请求产生一个Action的对象.原因是:struts 2的Action中包含数据,例如你在页面填写的数据就会包含在Action的成员变量里面 ...

  3. (C#) Action, Func, Predicate 等泛型委托

    (转载网络文章) (1). delegate delegate我们常用到的一种声明   Delegate至少0个参数,至多32个参数,可以无返回值,也可以指定返回值类型.   例:public del ...

  4. Lambda Action Func练习

    namespace lambda { delegate void TestDelegate(string s); class Program { static void Main(string[] a ...

  5. Delegate,Action,Func,匿名方法,匿名委托,事件 (转载)

    Delegate,Action,Func,匿名方法,匿名委托,事件 (转载) 一.委托Delegate 一般的方法(Method)中,我们的参数总是string,int,DateTime...这些基本 ...

  6. c# Action,Func,Predicate委托

    System命名空间下已经预先定义好了三中泛型委托,Action,Func和Predicate,这样我们在编程的时候,就不必要自己去定义这些委托了 Action是没有返回值的 Func是带返回值的 不 ...

  7. Delegate,Action,Func,Predicate的使用与区别

    C#4.0推出后,类似Linq,Lamda表达式等许多新的程序写法层次不穷.与之相关的Delegate,Action,Func,Predicate的使用和区别也常常让大家迷惑,此处就结合实际的应用,对 ...

  8. C# 委托应用总结(委托,Delegate,Action,Func,predicate)

    C# 委托应用总结 一.什么是委托 1.1官方解释 委托是一种定义方法签名的类型.当实例化委托时,您可以将其实例与任何具有兼容签名的方法相关联.您可以通过委托实例调用方法. 1.2个人理解 委托就是执 ...

  9. winform总结2> Action<> ,Action,func<>,委托相关的理解

    1,他们是什么: Action 封装一个方法,该方法不具有参数并且不返回值. Action<> 封装一个方法,该方法具有最多16个参数并且不返回值. func<> 封装一个具有 ...

随机推荐

  1. C/C++基础笔试题1.1.2(十问解决方案运营商)

    他写在博客上不负责任,有人踩在上面,原因一点点失去了它. 其实纯常见问题.特别糟糕,由于鲍文看起来很大的优势就是想更直接看到的东西. 好了.返回整个标题. 1.你脑海中的运算符有哪些.按优先级排个序? ...

  2. Java Web整合开发(3) -- Servlet

    Servlert基本程序架构: (FirstServlet.java + web.xml) FirstServlet.java package com.helloben.servlet; import ...

  3. keyboard splitting bug on ipad with ios 5 and 6 (Cocos2d-x)

    Had the same issue - the solution is to stop the opengl layer from rendering while this is happening ...

  4. contentWindow,

    a>contentWindow 兼容各个浏览器,可取得子窗口的 window 对象.b>contentDocument Firefox 支持,> ie8 的ie支持.可取得子窗口的 ...

  5. 十天学Linux内核之第四天---如何处理输入输出操作

    原文:十天学Linux内核之第四天---如何处理输入输出操作 真的是悲喜交加呀,本来这个寒假早上8点都去练车,两个小时之后再来实验室陪伴Linux内核,但是今天教练说没名额考试了,好纠结,不过想想就可 ...

  6. hibernate 管理 Session(单独使用session,不spring)

    Hibernate 本身提供了三个管理 Session 对象的方法 Session 对象的生命周期与本地线程绑定 Session 对象的生命周期与 JTA 事务绑定 Hibernate 托付程序管理 ...

  7. Ubuntu 14.1 构造NFS

    主办IP:129.1.4.189.目标计算机IP:129.1.22.96 1.运行命令:apt-get install nfs-kernel-server ; 2.运行命令:mkdir /tftpbo ...

  8. HDU 1203 I NEED A OFFER!(dp)

    Problem Description Speakless很长时间,我想出国.现在,他已经完成了所有需要的检查.准备好所有要准备的材料,于是,便须要去申请学校了.要申请国外的不论什么大学.你都要交纳一 ...

  9. NET动态调用WebService

    NET动态调用WebService 这不是一篇教你了解WebService的博文,也不是对WebService的深入理解, 这是一篇教你在开发过程中,如果动态的调用WebService一个方法. 在比 ...

  10. MVC6项目

    解读ASP.NET 5 & MVC6系列(2):初识项目 2015-05-14 09:08 by 汤姆大叔, 2866 阅读, 19 评论, 收藏, 编辑 初识项目 打开VS2015,创建We ...