作为返回值的函数 [英] Functions as return values

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

问题描述

大家好,

我正试图理解从

封闭函数中返回函数的概念。这个想法对我来说是新的,我不知道什么时候和

为什么我需要使用它。

有人可以对这个问题有所了解吗?


非常感谢,


Ben

Hi all,
I''m trying to understand the concept of returning functions from the
enclosing functions. This idea is new to me and I don''t understand when and
why I would need to use it.
Can someone please shed some light on this subject?

Thanks a lot,

Ben

推荐答案

On 8月11日,11:24 * pm,Ben< B ... @ discussion.microsoft.comwrote:
On Aug 11, 11:24*pm, Ben <B...@discussions.microsoft.comwrote:

大家好,

我正在尝试理解从

封闭函数返回函数的概念。这个想法对我来说是新的,我不知道什么时候和

为什么我需要使用它。

有人可以对这个问题有所了解吗?


非常感谢,


Ben
Hi all,
I''m trying to understand the concept of returning functions from the
enclosing functions. This idea is new to me and I don''t understand when and
why I would need to use it.
Can someone please shed some light on this subject?

Thanks a lot,

Ben



"返回来自封闭功能


嗯......你在说什么概念???


-Cnu

"returning functions from the enclosing functions"

Ummm... Which concept you are talking about???

-Cnu


2008年8月11日星期一11:24:08 -0700,Ben< Be*@discussions.microsoft.com>

写道:
On Mon, 11 Aug 2008 11:24:08 -0700, Ben <Be*@discussions.microsoft.com>
wrote:

大家好,

我正试图理解从

封闭函数返回函数的概念。这个想法对我来说是新的,我不明白什么时候



为什么我需要使用它。

有人可以请阐明了这个问题?
Hi all,
I''m trying to understand the concept of returning functions from the
enclosing functions. This idea is new to me and I don''t understand when
and
why I would need to use it.
Can someone please shed some light on this subject?



您的意思是匿名方法吗?如果是这样的话,这是与委托类型相关的更广泛的

主题的子集。


匿名方法本身是,恕我直言,有用,因为他们允许

实现细节保持接近使用位置,并且可以

甚至可以避免为保存数据而定义新类

特定于实现(参见变量捕获)。但是大部分

的用处和代表一般都是一样的。


Jon Skeet在这里有一篇关于代表的好文章:
< a rel =nofollowhref =http://yoda.arachsys.com/csharp/events.htmltarget =_ blank> http://yoda.arachsys.com/csharp/events.html


他还写了一篇关于闭包的文章。讨论了这个概念,

虽然在lambda表达式的上下文中(可以使用类似于匿名方法的
)。这是以下链接:
http:// csharpindepth。 com / Articles / Ch ... / Closures.aspx

在新闻组的背景下难以提供完整的

讨论回答与您一样广泛的问题。希望上面的

链接会有所帮助。如果您在阅读这些文章之后还有更多具体问题,请随时在这里发帖,我们可以尝试

来详细说明。


如果以上似乎没有解决你的问题,那么也许你可以更具体地了解你的问题。短语

的概念从封闭函数返回函数。并不是字面意思

在C#中描述任何东西,所以我已经对

做了一些推断你真正的意思。如果我做了一个不正确的假设,你可能需要

来尝试以更具体的方式重新解释你的问题(包括代码

必要的例子)。 />

Pete

Do you mean "anonymous methods"? If so, that''s a sub-set of a broader
topic related to delegate types.

The anonymous methods themselves are, IMHO, useful because they allow
implementation details to remain close to where they are used, and can
even avoid the need to define a new class for the purpose holding data
specific to the implementation (see "variable capturing"). But much of
the usefulness is just the same as for delegates generally.

Jon Skeet has a nice article on delegates generally here:
http://yoda.arachsys.com/csharp/events.html

He''s also written an article on "closures" that discusses the concept,
albeit in the context of lambda expressions (which can be used in ways
similar to anonymous methods). Here''s a link to that:
http://csharpindepth.com/Articles/Ch.../Closures.aspx

It''s difficult in the context of a newsgroup to provide a complete
discussion that answers a question as broad as yours. Hopefully the above
links will help. If you have more specific questions remaining after
reading those articles, please feel free to post those here and we can try
to elaborate.

If the above doesn''t seem to be addressing your question, then maybe you
can be more specific about what you''re asking. The phrase "concept of
returning functions from the enclosing functions" doesn''t literally
describe anything in C# at all, so I''ve made some inferences regarding
what you really mean. If I''ve made an incorrect assumption, you may want
to try to rephrase your question in a more specific way (including code
examples if necessary).

Pete


Ben写道:
Ben wrote:

大家好,

我试图理解从

封闭函数返回函数的概念。这个想法对我来说是新的,我不知道什么时候和

为什么我需要使用它。

有人可以对这个问题有所了解吗?


非常感谢,


Ben
Hi all,
I''m trying to understand the concept of returning functions from the
enclosing functions. This idea is new to me and I don''t understand when and
why I would need to use it.
Can someone please shed some light on this subject?

Thanks a lot,

Ben



你实际上会从该方法将是一个委托。


从一个方法返回委托并不常见,但委托是一个

类型,可用于返回价值就像任何其他类型一样。

代表最常用于事件,但它们也用于其他方式的
。例如,Array.Sort方法有一些重载,

需要代理,用于比较分类时的项目。


你可以制作一个以特殊方式比较两个字符串的方法:

public static int CompareIgnoreCase(string x,string y){

return string.Compare(x,y ,true);

}


然后你可以使用方法的名称将委托发送给Sort

方法:


Array.Sort< string>(myStringArray,CompareIgnoreCase);

或者使用匿名方法:


Array.Sort< string>(myStringArray,delegate(string x,string y){return

string.Compare(x,y,true);});


如果使用C#3,甚至是lambda表达式:


Array.Sort< string>(myStringArray,(x,y)= string.Compare(x,y ,true));

要向原始问题的方向扩展,你可以

有一个方法返回不同的代表取决于你想要的方式

来排序:


公共静态比较< stringGetComparer(bool ignoreCase){

if(ignoreCase){

返回委托(字符串x,字符串y){

返回string.Compare(x,y,true);

};

} else {

返回委托(字符串x,字符串y){

返回string.Compare(x,y);

};

}

}


调用方法会返回一个委托,你就是然后可以在

中使用对Sort方法的调用:

Array.Sort< string>(myStringArray,GetComparer(true));


你当然也可以直接给代表打电话:


int result = GetComparer(true)(Peter,Paul);


或者将它存储在一个变量中,然后调用它:


比较< stringcomp = GetComparer(true);

int result = comp(" Jack"," Jill");


-

G ??跑了Andersson

_____
http://www.guffa.com


这篇关于作为返回值的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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