c#中匿名函数lamb表达式
c#中匿名函数lamb表达式
实例一:(其实,这样都是些语法糖)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace ConsoleApplication2
{
//c#中的匿名函数
//申明一委托
delegate void Del();
class Program
{ static void show()
{
Console.WriteLine("show ......");
} static void Test()
{
Del d = new Del(show);
d();
}
static void Test2()
{
//你可以这么写....
Del d = delegate()
{
Console.WriteLine("show......");
};
d(); //你也可以这么写
Del d1 = delegate
{
Console.WriteLine("show.....");
}; d1();
//你还可以这么写;这就是lamb表达式;
Del d2 = () =>
{
Console.WriteLine("show.....");
};
d2(); }
static void Main(string[] args)
{ Test2();
Console.ReadLine(); }
}
}
有参数的lamb表达式:
static void Test()
{
//你可以这么写
Dele d = delegate(int j)
{
Console.WriteLine(j);
};
d(); Dele d1 = (j) => { Console.WriteLine(j); };
d1();
//这个就是有参参的lamb表达式;
} static void Main(string[] args)
{
Test();
Console.ReadLine(); }
顺便提一下c#中的Action Func Predicate;
Delegate至少0个参数,至多32个参数,可以无返回值,也可以指定返回值类型。
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
(4) predicate
predicate 是返回bool型的泛型委托
predicate<int> 表示传入参数为int 返回bool的委托
Predicate有且只有一个参数,返回值固定为bool
实例:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading; namespace ConsoleApplication2
{ delegate void Dele(int i);
class Program
{
static void add(int i,int j)
{
Console.WriteLine(i+j);
}
static int sub(int i, int j)
{
return i - j;
} static bool isTrue(int i)
{
if (i > )
return true;
else
return false;
} static void Test()
{ List<Action<int,int>> list = new List<Action<int,int>>();
list.Add(add);
//调用方式一
list[](,);
//最后一个参数是返回值;
Func<int,int,int> func=sub;
int result=func(,);
Console.WriteLine(result); Predicate<int> p = isTrue;
bool re = p();
Console.WriteLine(re); }
static void Test2()
{
//当然我们可以换一种方式写滴呀
Action<int, int> action = delegate(int i, int j)
{
Console.WriteLine(i+j);
};
action(,); Func<int, int, int> func = delegate(int i, int j)
{
return i - j;
};
int result = func(,); Predicate<int> pre = delegate(int i)
{
bool re=
i==?true: false;
return re;
}; }
static void Test3()
{
//当然我们也可以这样写滴呀
Action<int, int> action = (i, j) => { Console.WriteLine(i + j); };
action(,); Func<int,int,int> func=(i,j)=>{return i-j;};
int result=func(,); Predicate<int> pre = (i) => { if (i == )return true; else return false; };
bool val = pre(); } static void Test4()
{
Thread thread = new Thread(delegate()
{
Console.WriteLine("hahhahah....");
});
thread.Start(); Thread thread2 = new Thread(() =>
{
Console.WriteLine("hahah......");
});
thread2.Start(); } static void Main(string[] args)
{
//其实,就相当于这样滴呀: public delegate void Action<T>(T arg);
Test();
Console.ReadLine(); }
}
}
c#中匿名函数lamb表达式的更多相关文章
- Python中匿名函数与内置高阶函数详解
大家好,从今天起早起Python将持续更新由小甜同学从 初学者的角度 学习Python的笔记,其特点就是全文大多由 新手易理解 的 代码与注释及动态演示 .刚入门的读者千万不要错过! 很多人学习pyt ...
- 【Unity|C#】基础篇(9)——匿名函数 / Lambda表达式
[学习资料] <C#图解教程>(第13章):https://www.cnblogs.com/moonache/p/7687551.html 电子书下载:https://pan.baidu. ...
- Qt中使用匿名函数lambda表达式
一.为什么要使用匿名函数lamdba 首先,lambda表达式可以使代码变得简单,C++中,一个lambda表达式表示一个可调用的代码单元.如代码: #include <QCoreApplica ...
- 匿名函数 lambda表达式(lambda expression)
阅读g2log时,发现有两行代码居然看不懂. 1. auto bg_call = [this, log_directory]() {return pimpl_->backgroundChang ...
- python中匿名函数lambda
简单来说,编程中提到的 lambda 表达式,通常是在需要一个函数,但是又不想费神去命 名一个函数的场合下使用,也就是指匿名函数. 先看它的几个用法: map( lambda x: x*x, [y f ...
- C#中匿名函数、委托delegate和Action、Func、Expression、还有Lambda的关系和区别
以前一直迷迷糊糊的,现在总算搞明白. Lambda表达式 Lamda表达式基本写法是()=>{ };Lambda和方法一样都可以传入参数和拥有返回值.(int x)=>{return x; ...
- (28)C#委托,匿名函数,lambda表达式,事件
一.委托 委托是一种用于封装命名和匿名方法的引用类型. 把方法当参数,传给另一个方法(这么说好理解,但实际上方法不能当参数,传入的是委托类型),委托是一种引用类型,委托里包含很多方法的引用 创建的方法 ...
- JavaScript中匿名函数循环传参数(不触发函数的执行)
我们都知道定义函数的方式有两种,一种是函数声明,另一种是函数表达式,函数声明的语法是这样的: function functionName(arg0, arg1, arg2) { // 函数体 } 函数 ...
- python3中匿名函数做参数,匿名函数做实参,eval关键字
一:说到匿名函数,大家都感到陌生又熟悉,今天我带大家了解一下py3中的匿名函数,以及匿名函数作为函数的参数的情况 主要通过以下实例来说明: 实例一: newarr =[33,44444,6222,88 ...
随机推荐
- Cobar_基于MySQL的分布式数据库服务中间件
Cobar是阿里巴巴研发的关系型数据的分布式处理系统,是提供关系型数据库(MySQL)分布式服务的中间件,该产品成功替代了原先基于Oracle的数据存储方案,它可以让传统的数据库得到良好的线性扩展,并 ...
- nosql数据库比较
- [转]3天搞定的小型B/S内部管理类软件定制开发项目【软件开发实战10步骤详解】
本文转自:http://www.cnblogs.com/jirigala/archive/2010/10/07/1845275.html 2010-10-07 21:39 by 通用C#系统架构, 5 ...
- 【iCore2双核心板视频教程】 AD模块(iM_AD_GP和iM_AD_SYNC)介绍及数据采集实验三
建议设定成 “超清” 模式并 “全屏” 观看. ============================== 技术论坛:http://www.eeschool.org 博客地址:http://xiao ...
- 使用Grunt启动和运行
开始使用Grunt 大多数开发人员都一致认为,JavaScript开发的速度和节奏在过去的几年里已经相当惊人.不管是Backbone.js和Ember.js的框架还是JS Bin社区,这种语言的发展变 ...
- 被spring和hibernate4逼疯
spring3.1整合hibernate4,事务都配置上了的,但getCurrentSession()仍然获得不到 以下是各配置 web.xml ? 1 2 3 4 5 6 7 8 9 10 11 1 ...
- [听听音乐]when you believe [singer: mariah carey]
movie: prince of egypt Lyrics Many nights we prayed With no proof anyone could hear In our hearts ...
- java build path->source folder分析
1.build path下的source folde,指的是项目存放源码的位置,即存放Java代码的位置!!! 如果将一个文件夹设为java build path里的source folder下,则这 ...
- lua学习笔记
工作需要,上周对lua赶进度似地学习了一遍,主要参考<lua中文教程>一书,中间参考一些<lua游戏开发实践>,首先说说这两本书,后者不适合初学,里面是对一个游戏脚本系统进行粗 ...
- 【转载】MySQL性能优化的最佳20+条经验
今天,数据库的操作越来越成为整个应用的性能瓶颈了,这点对于Web应用尤其明显.关于数据库的性能,这并不只是DBA才需要担心的事,而这更是我们程序员需要去关注的事情.当我们去设计数据库表结构,对操作数据 ...