ConvertAll(Guid.Parse) 如何工作?

发布于 2024-10-15 06:18:27 字数 522 浏览 2 评论 0原文

首先我有这样的行

List<Guid> ratingComparisonIds = ratingComparison.ConvertAll<Guid>(o => Guid.Parse(o));

,然后 ReSharper 给了我一个提示,我可以将它转换为方法组。该操作的结果是这样的,

List<Guid> ratingComparisonIds = ratingComparison.ConvertAll<Guid>(Guid.Parse);

我对语法感到困惑..它是如何工作的以及为什么工作?我的意思是解析方法没有参数。我想说的是魔法。

更新

转换更加紧凑

List<Guid> ratingComparisonIds = ratingComparison.ConvertAll(Guid.Parse);

First I had this kind of line

List<Guid> ratingComparisonIds = ratingComparison.ConvertAll<Guid>(o => Guid.Parse(o));

Then ReSharper gave me a hint that I could convert it to method group. Result of that operation is this

List<Guid> ratingComparisonIds = ratingComparison.ConvertAll<Guid>(Guid.Parse);

I am confused about the syntax.. how and why does it work? I mean there is no parameters for the parse method. Magic I would say.

Update

Even more compact conversion

List<Guid> ratingComparisonIds = ratingComparison.ConvertAll(Guid.Parse);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

○闲身 2024-10-22 06:18:27

它使用方法组转换。它正在做这样的事情:

// Very long-winded C# 1 syntax
Converter<String, Guid> converter = new Converter<String, Guid>(Guid.Parse);
List<Guid> ratingComparisonIds = ratingComparison.ConvertAll(converter);

// Simpler C# 2 syntax
Converter<String, Guid> converter = Guid.Parse;
List<Guid> ratingComparisonIds = ratingComparison.ConvertAll(converter);

现在内联变量,然后您就可以看到原始代码。这会让事情变得更清楚吗?

It's using a method group conversion. It's doing something like this:

// Very long-winded C# 1 syntax
Converter<String, Guid> converter = new Converter<String, Guid>(Guid.Parse);
List<Guid> ratingComparisonIds = ratingComparison.ConvertAll(converter);

// Simpler C# 2 syntax
Converter<String, Guid> converter = Guid.Parse;
List<Guid> ratingComparisonIds = ratingComparison.ConvertAll(converter);

Now inline the variable, and you get to the original code. Does that make things clearer?

明天过后 2024-10-22 06:18:27

ConvertAll(在此上下文中)需要一个 Converter - 一个接受 string 并返回一个方法的委托指南。静态Guid.Parse完美满足这一点 - 与我们编写new Converter(Guid.Parse)完全相同,或写为:

Converter<string,Guid> conv = Guid.Parse; // perfectly valid
List<Guid> ratingComparisonIds = ratingComparison.ConvertAll<Guid>(conv);

lambda 版本通过匿名方法添加了一定程度的间接:

static Guid <>____blah(string o) { return Guid.Parse(o); }

因此直接调用 Guid.Parse 实际上稍微高效的。

ConvertAll (in this context) takes here requires a Converter<string,Guid> - a delegate to a method that takes a string and returns a Guid. The static Guid.Parse satisfies that perfectly - exactly the same as if we had written new Converter<string,Guid>(Guid.Parse), or written:

Converter<string,Guid> conv = Guid.Parse; // perfectly valid
List<Guid> ratingComparisonIds = ratingComparison.ConvertAll<Guid>(conv);

The lambda version adds a level of indirection via an anonymous method:

static Guid <>____blah(string o) { return Guid.Parse(o); }

so the direct call to Guid.Parse is actually marginally more efficient.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文