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 ...
随机推荐
- hilbert
hilbert 难度级别: A: 编程语言:不限:运行时间限制:1000ms: 运行空间限制:131072KB: 代码长度限制:102400B 试题描述 图1为1阶Hilbert曲线,它由3条长度为1 ...
- linux rootfs制作
http://blog.sina.com.cn/s/blog_6795385f01011ifg.html 作一个嵌入式Linux rootfs,并且实现 web 服务 1. 文件系统简介 •理论上说一 ...
- RN组件之ScrollView
一.ScrollView 该组件封装了Android平台的ScrollView(滚动组件),并且提供触摸事件"responder"系统功能.使用ScrollView的时候 确保有一 ...
- 【回文串-Manacher】
Manacher算法能够在O(N)的时间复杂度内得到一个字符串以任意位置为中心的回文子串.其算法的基本原理就是利用已知回文串的左半部分来推导右半部分. 转:http://blog.sina.com.c ...
- Xcode4 使用技巧
Xcode4 使用技巧 使用 xcode4 也有一段时间了,今天整理了一下 xcode4 的一些使用技巧,在这里分享给大家. 设置作者 这里所指的作者就是每个源文件头部注释中的 "Creat ...
- iOS中--NSArray调用方法详解 (李洪强)
下面的例子以 NSArray *array = [NSArray arrayWithObjects:@"wendy",@"andy",@"to ...
- thinkphp框架中处理标签中条件输出
这个问题是这样的,当我在模板中想在标签option中在某个条件下输出selected.但是在里边嵌套if标签的话,condition里边第二个参数使用变量的时候,不能解析.所以只能使用别的方法了.达到 ...
- SQLi filter evasion cheat sheet (MySQL)
This week I presented my experiences in SQLi filter evasion techniques that I have gained during 3 y ...
- MUI - Scroll插件的使用
http://dev.dcloud.net.cn/mui/ui/#scroll 神坑1:如果在vuejs中使用,那么需要配合mui.ready(function(){}) 才能找到dom对象,具体de ...
- [转]动态调用webservice时 ServiceDescriptionImporter类在vs2010无法引用的解决方法
本文转自:http://blog.csdn.net/limlimlim/article/details/8647038 [导读]ServiceDescriptionImporter是创建Web Ser ...