LINQ Lambda表达式Word下载.docx
- 文档编号:22862940
- 上传时间:2023-02-05
- 格式:DOCX
- 页数:13
- 大小:18.91KB
LINQ Lambda表达式Word下载.docx
《LINQ Lambda表达式Word下载.docx》由会员分享,可在线阅读,更多相关《LINQ Lambda表达式Word下载.docx(13页珍藏版)》请在冰豆网上搜索。
"
i"
is
our
parameter
list.
0"
statement
set
to
process
.
应该这样来理解
My
list
of
parameters
(in
this
case
a
single
integer
named
will
be
processed
by
the
expression
0.
list.FindAll((i)
((i
0));
可以显式指定输入参数的类型
list.FindAll((int
i)
可以使用括号把输入参数和表达式括起来,如果参数或处理表达式只有一个,可以省略括号
有多行处理表达式时,需要使用花括号包起来
Console.WriteLine("
value
i
currently:
{0}"
i);
bool
isEven
isEven;
当输入参数有多个时
SimpleMath
m
new
SimpleMath();
m.SetMathHandler((msg,
result)
{Console.WriteLine("
Message:
{0},
Result:
{1}"
msg,
result);
或者显式指定输入参数类型
m.SetMathHandler((string
int
当没有输入参数时
VerySimpleDelegate
d
VerySimpleDelegate(
()
{return
Enjoy
your
string!
;
}
);
LINQLambda表达式实例
Postedon2008-02-1809:
56sunrack阅读(136)评论(0)
Lambda表达式可以在使用代理和匿名代理的地方
1、命名函数
public
class
Common
delegate
IntFilter(int
static
int[]
FilterArrayOfInts(int[]
ints,
IntFilter
filter)
ArrayList
aList
ArrayList();
foreach
in
ints)
if
(filter(i))
aList.Add(i);
}
((int[])aList.ToArray(typeof(int)));
Application
IsOdd(int
&
1)
1);
using
System.Collections;
nums
{
1,
2,
3,
4,
5,
6,
7,
8,
9,
10
};
oddNums
Common.FilterArrayOfInts(nums,
Application.IsOdd);
oddNums)
Console.WriteLine(i);
2、匿名函数
=
delegate(int
3、Lambda表达式
1));
三种方式的比较
method
anonymous
i){return((i
lambda
expression
命名函数虽然简短,但是而外还要定义处理函数,优点是可以重用
学关于Lambda表达式的一二三
我是白痴,所以我不知道什么是lambda,看了网上N篇文章仍然还是个白痴。
还不知道。
于是。
给自己写篇自己能看得懂的文章,希望以后再白痴的时候看看就不白痴了。
委托
委托是一种引用方法的类型。
一旦为委托分配了方法,委托将与该方法具有完全相同的行为。
委托方法的使用可以像其他任何方法一样,具有参数和返回值。
漫画
委托就是给一个函数起一个别的名字。
System;
Declare
--
defines
required
signature:
void
SampleDelegate(string
message);
MainClass
Regular
method
that
matches
SampleDelegateMethod(string
message)
Console.WriteLine(message);
Main()
Instantiate
with
method:
SampleDelegate
d1
SampleDelegateMethod;
d2
delegate(string
d1("
Hello"
d2("
World,
这玩意也是委托
Form1()
InitializeComponent();
this.Click
+=
this.MultiHandler;
private
MultiHandler(object
sender,
System.EventArgs
e)
MessageBox.Show(((MouseEventArgs)e).Location.ToString());
匿名方法
匿名方法就是没名儿的委托。
虽然没名,但是必须加”delegate“来表示我没名。
Create
instance
Del(int
x);
an
Del
k)
/*
...
*/
delegate()
System.Console.WriteLine("
Copy
#:
++n);
Printer(string
s);
Printer
p
j)
System.Console.WriteLine(j);
p("
The
called."
string
button1_Click(object
EventArgs
=delegate(string
(j)+"
烦死了"
Console.WriteLine(p("
));
或者有名。
。
的匿名委托。
哈哈,我在抽风还是微软在抽风。
让我死吧。
Printer(Form1.DoWork);
DoWork(string
k)
System.Console.WriteLine(k);
匿名方法就完了。
Lamdba就是(intx)=>
{x+1}就是这样的。
例子呢就是上面写了的一段用lambda就这样写
j
j+"
烦死了!
!
还能这么用
和上面那个一样,就是简化了。
(s,
e)
超难+===>
?
其实不难。
难的是转换成一般的函数怎么写呢?
?
Where是Enumerable的一个方法。
3.5才有的。
里面的参数是Func<
(Of<
(T,TResult>
)>
)泛型委托
Func<
ConvertMethod(string
inString);
DelegateExample
reference
UppercaseString
ConvertMethod
convertMeth
UppercaseString;
name
Dakota"
Use
instance
call
Console.WriteLine(convertMeth(name));
UppercaseString(string
inputString)
inputString.ToUpper();
写成泛型委托是
GenericFunc
string,
string>
convertMethod
Console.WriteLine(convertMethod(name));
于是应用到Linq,再变换到lambda
TestFunc(string
fruit);
fruits
=new
apple"
banana"
mango"
orange"
blueberry"
grape"
strawberry"
TestFunc
f
TestFunc(DoWork);
bool>
f2
DoWork;
IEnumerable<
query
fruits.Where(f2);
(string
fruit
query)
Console.WriteLine(fruit);
k.Length
<
6;
能用。
passionfruit"
=k=>
也能用
fruits.Where(
k){
能用~
fruits.Where(k=>
k.Length<
6);
最后,lambda,能用~~~~就酱紫了~~~~
于是,我这个白痴,被自己写的教程搞明白了。
于是我也会了。
但是心情依旧很糟糕。
~~~~~~~~~
啊啊啊啊啊~最后来个有意思的!
mydg(int
a,
b);
LambdaTest
oper(this
b,
mydg
dg)
dg(a,
Console.WriteLine(1.oper(2,
(a,
b)
+
b));
Console.WriteLine(2.oper(1,
-
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- LINQ Lambda表达式 Lambda 表达式