lambda用法 [英] lambda usages

查看:68
本文介绍了lambda用法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白下面的代码中发生了什么(来自

加速c#) - 我知道结果但不知道.net是怎么做的:


使用System;

使用System.Linq;

公共类LambdaTest

{

static void Main()

{

int counter = 0;

WriteStream(()= counter ++);

Console.WriteLine("计数器的最终值:{0}",counter);

}

static void WriteStream(Func< intcounter)

{

for(int i = 0; i< 10; ++ i)

{

Console.Write( " {0},",counter());

}

Console.WriteLine();

}

}


据我所知,我们正在创建Func泛型委托,

,它不带参数并返回int。我们和那个

委托我们传递本地值类型计数器。那么为什么在WriteStream重新启动后会改变

?没有WriteSteam在当地的柜台争论中运作




需要一些lambda教育....


谢谢

解决方案

" puzzlecracker" < ir ********* @ gmail.comwrote in message

news:9b ********************* ************* @ p49g2000 hsd.googlegroups.com ...


我不明白发生了什么下面的代码(从

加速c#) - 我知道结果但不是.net如何做到:


使用系统;

使用System.Linq;


公共类LambdaTest

{

static void Main()

{

int counter = 0;

WriteStream(()= counter ++);

Console.WriteLine(" final value of counter:{0}",counter);

}

static void WriteStream(Func< intcounter)

{

for(int i = 0; i< 10; ++ i)

{

Console.Write(" {0},",counter ());

}

Console.WriteLine();

}

}


从我的理解,我们ar创建Func泛型委托,

,它不带参数并返回int。我们和那个

委托我们传递本地值类型计数器。那么为什么在WriteStream重新启动后会改变

?没有WriteSteam在当地的柜台争论中运作




需要一些lambda教育....


谢谢



好​​吧,我们没有将计数器传递给通用委托,通用

委托使用本地计数器(本地计数器)从Main

函数或代理更新/修改时,外部函数)

在同一地址的值相同。 等同物是指等同物。方法是:


static int Increment(ref int counter){counter ++; }


相当于我的意思是计数器正在被传递。进入方法(通过

引用)而不是直接访问父代码变量的方法

阻止。


我相信这是正确的,但我不是100%,所以我希望有人回复如果

我错了:)


HTH,

Mythran


Mythran写道:


" puzzlecracker" < ir ********* @ gmail.comwrote in message

news:9b ********************* ************* @ p49g2000 hsd.googlegroups.com ...


>我不明白发生了什么在下面的代码中(来自
加速c#) - 我知道结果但不是.net如何做到:

使用System;
使用System.Linq;

公共类LambdaTest
{
static void Main()
{int = counter = 0;
WriteStream(()= counter ++); < /> Console.WriteLine(计数器的最终值:{0},计数器);
}
static void WriteStream(Func< intcounter)
{
for (int i = 0; i< 10; ++ i)
{
Console.Write(" {0},",counter());
} Console.WriteLine();
}


根据我的理解,我们正在创建Func泛型委托,
它不带参数并返回int 。我们和那个
委托我们传递本地值类型计数器。那么为什么在WriteStream重新启动后会反击
?难道WriteSteam在当地的反驳论点上运作吗?

需要一些lambda教育....

谢谢



好​​吧,我们没有将计数器传递给通用委托,

泛型委托使用本地计数器(本地外部

函数)当从Main函数或委托更新/修改

时,在同一地址的值相同。

等价物方法是:

static int Increment(ref int counter){counter ++; }


相当于我的意思是计数器正在被传递。进入方法

(通过引用)而不是直接访问

父代码块变量的方法。


我相信这是正确的,但我不是100%所以我希望别人

回复如果我错了:)



大多数情况下,但不同的是,这里我们有一个闭包,

对函数的计数器变量的引用是委托的一部分。使用

a引用参数时,调用者(即WriteStream)必须传递

引用,这里调用者不会以任何方式参与。


>

HTH,

Mythran


10月22日,2:20 * pm,Ben Voigt [C ++ MVP]" < r ... @nospam.nospamwrote:


Mythran写道:


" puzzlecracker" < ironsel2 ... @ gmail.com写信息

新闻:9b *************************** ******* @ p49g2000 hsd.googlegroups.com ....


我不明白下面的代码中发生了什么(来自

加速c#) - 我知道结果但不是.net如何做到:


使用System;

使用System.Linq;


public class LambdaTest

{

* static void Main()

* {

* * int counter = 0;

* * WriteStream(()= counter ++) ;

* * Console.WriteLine(计数器的最终值:{0},计数器);

*}

* static void WriteStream(Func< intcounter)

* {

* * * for(int i = 0; i< 10; ++ i)

* * * {

* * * * * Console.Write(" {0},",counter());

* * *}

* * * Console.WriteLine();

*}

}


从我的理解,我们正在创建Func *泛型委托,

,它不带参数并返回int。我们和那个

委托我们传递本地值类型计数器。然后*为什么在WriteStream重新启动后会改变

?在本地*计数器参数上,WriteSteam是否运行



需要一些lambda教育....


谢谢


好​​吧,我们没有将计数器传递给泛型委托,

泛型委托使用本地计数器(外部

函数的本地),它在同一地址的值是相同的

从Main函数或委托更新/修改。 *

等价物方法是:

static int Increment(ref int counter){counter ++; }


相当于我的意思是计数器正在被传递。进入方法

(通过引用)而不是直接访问

父代码块的变量的方法。


我相信这是正确的但我不是100%所以我希望别人

如果我回答'错了:)



大部分是正确的,但不同的是,这里我们有一个闭包,

引用函数'' s计数器变量是委托的一部分。 *使用

a引用参数时,调用者(即WriteStream)必须通过

引用,此处调用者不会以任何方式参与。
< blockquote class =post_quotes>
HTH,

Mythran



有谁知道如何定义Func并实施了?


I am not understanding what''s happening in the code below (from
accelerated c#) -- I know what the outcome but not how .net does it:

using System;
using System.Linq;
public class LambdaTest
{
static void Main()
{
int counter = 0;
WriteStream( () =counter++ );
Console.WriteLine( "Final value of counter: {0}", counter );
}
static void WriteStream( Func<intcounter )
{
for( int i = 0; i < 10; ++i )
{
Console.Write( "{0}, ", counter() );
}
Console.WriteLine();
}
}

From what I understand, we are creating Func generic delegate ,
which is takes no parameters and returns the int. We and to that
delegate we pass local value type counter. Then why would counter
changed upon the retrun from WriteStream? Doesn''t WriteSteam operate
on the local counter argument?

need some lambda education....

Thanks

解决方案

"puzzlecracker" <ir*********@gmail.comwrote in message
news:9b**********************************@p49g2000 hsd.googlegroups.com...

I am not understanding what''s happening in the code below (from
accelerated c#) -- I know what the outcome but not how .net does it:

using System;
using System.Linq;
public class LambdaTest
{
static void Main()
{
int counter = 0;
WriteStream( () =counter++ );
Console.WriteLine( "Final value of counter: {0}", counter );
}
static void WriteStream( Func<intcounter )
{
for( int i = 0; i < 10; ++i )
{
Console.Write( "{0}, ", counter() );
}
Console.WriteLine();
}
}

From what I understand, we are creating Func generic delegate ,
which is takes no parameters and returns the int. We and to that
delegate we pass local value type counter. Then why would counter
changed upon the retrun from WriteStream? Doesn''t WriteSteam operate
on the local counter argument?

need some lambda education....

Thanks

Well, we are not passing counter into the generic delegate, the generic
delegate is using the local counter (local to the outer function) which is
the same value at the same address when updated/modified from the Main
function or the delegate. The "equivalent" method would be:

static int Increment(ref int counter) { counter++; }

By equivalent I mean that counter is being "passed" into the method (by
reference) instead of the method accessing the variable of the parent code
block directly.

I believe this is correct but I''m not 100% so I hope someone else replies if
I''m wrong :)

HTH,
Mythran


Mythran wrote:

"puzzlecracker" <ir*********@gmail.comwrote in message
news:9b**********************************@p49g2000 hsd.googlegroups.com...

>I am not understanding what''s happening in the code below (from
accelerated c#) -- I know what the outcome but not how .net does it:

using System;
using System.Linq;
public class LambdaTest
{
static void Main()
{
int counter = 0;
WriteStream( () =counter++ );
Console.WriteLine( "Final value of counter: {0}", counter );
}
static void WriteStream( Func<intcounter )
{
for( int i = 0; i < 10; ++i )
{
Console.Write( "{0}, ", counter() );
}
Console.WriteLine();
}
}

From what I understand, we are creating Func generic delegate ,
which is takes no parameters and returns the int. We and to that
delegate we pass local value type counter. Then why would counter
changed upon the retrun from WriteStream? Doesn''t WriteSteam operate
on the local counter argument?

need some lambda education....

Thanks


Well, we are not passing counter into the generic delegate, the
generic delegate is using the local counter (local to the outer
function) which is the same value at the same address when
updated/modified from the Main function or the delegate. The
"equivalent" method would be:
static int Increment(ref int counter) { counter++; }

By equivalent I mean that counter is being "passed" into the method
(by reference) instead of the method accessing the variable of the
parent code block directly.

I believe this is correct but I''m not 100% so I hope someone else
replies if I''m wrong :)

Mostly right, but the difference is that here we have a closure, the
reference to the function''s counter variable is part of the delegate. With
a reference argument the caller (i.e. WriteStream) would have to pass the
reference, here the caller isn''t involved in any way.

>
HTH,
Mythran



On Oct 22, 2:20*pm, "Ben Voigt [C++ MVP]" <r...@nospam.nospamwrote:

Mythran wrote:

"puzzlecracker" <ironsel2...@gmail.comwrote in message
news:9b**********************************@p49g2000 hsd.googlegroups.com....

I am not understanding what''s happening in the code below (from
accelerated c#) -- I know what the outcome but not how .net does it:

using System;
using System.Linq;

public class LambdaTest
{
*static void Main()
*{
* * int counter = 0;
* * WriteStream( () =counter++ );
* * Console.WriteLine( "Final value of counter: {0}", counter );
*}
*static void WriteStream( Func<intcounter )
*{
* * *for( int i = 0; i < 10; ++i )
* * *{
* * * * *Console.Write( "{0}, ", counter() );
* * *}
* * *Console.WriteLine();
* }
}

From what *I understand, we are creating Func *generic delegate ,
which is takes no parameters and returns the int. We and to that
delegate we pass local value type counter. Then *why would counter
changed upon the retrun from WriteStream? Doesn''t WriteSteam operate
on the local *counter argument?

need some lambda education....

Thanks

Well, we are not passing counter into the generic delegate, the
generic delegate is using the local counter (local to the outer
function) which is the same value at the same address when
updated/modified from the Main function or the delegate. *The
"equivalent" method would be:
static int Increment(ref int counter) { counter++; }

By equivalent I mean that counter is being "passed" into the method
(by reference) instead of the method accessing the variable of the
parent code block directly.

I believe this is correct but I''m not 100% so I hope someone else
replies if I''m wrong :)


Mostly right, but the difference is that here we have a closure, the
reference to the function''s counter variable is part of the delegate. *With
a reference argument the caller (i.e. WriteStream) would have to pass the
reference, here the caller isn''t involved in any way.

HTH,
Mythran

Does anyone know what how Func is defined and implemented?


这篇关于lambda用法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆