惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Cisco Talos Blog
Cisco Talos Blog
阮一峰的网络日志
阮一峰的网络日志
云风的 BLOG
云风的 BLOG
D
Docker
Vercel News
Vercel News
IT之家
IT之家
Recent Announcements
Recent Announcements
Last Week in AI
Last Week in AI
V
Visual Studio Blog
Engineering at Meta
Engineering at Meta
腾讯CDC
Google DeepMind News
Google DeepMind News
I
InfoQ
博客园 - 三生石上(FineUI控件)
Apple Machine Learning Research
Apple Machine Learning Research
The GitHub Blog
The GitHub Blog
博客园 - Franky
The Cloudflare Blog
A
About on SuperTechFans
有赞技术团队
有赞技术团队
Y
Y Combinator Blog
T
Tenable Blog
P
Proofpoint News Feed
Recorded Future
Recorded Future
Security Latest
Security Latest
H
Hackread – Cybersecurity News, Data Breaches, AI and More
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
博客园 - 聂微东
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Google Online Security Blog
Google Online Security Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Simon Willison's Weblog
Simon Willison's Weblog
The Last Watchdog
The Last Watchdog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
N
News and Events Feed by Topic
TaoSecurity Blog
TaoSecurity Blog
U
Unit 42
The Hacker News
The Hacker News
Martin Fowler
Martin Fowler
T
Threat Research - Cisco Blogs
NISL@THU
NISL@THU
F
Full Disclosure
M
MIT News - Artificial intelligence
人人都是产品经理
人人都是产品经理
Hugging Face - Blog
Hugging Face - Blog
V
V2EX
Project Zero
Project Zero

博客园 - cp

HR:.Net(Senior)Software Engineer/Project Leader IT Consulting Positions (Senior Level) Software Sales Engineer / Software Engineer Scott Gu上海见面会 有个朋友公司急召C++程序员 对比面向对象和面向服务 预告一下接下来要写的心得 C# 3.0 规范(PDC2005)中文word版本 C#3.0规范(七)对象以及集合构造者 关于ComponentArt Web.UI 2006.1(build 1208)源代码之若干说明 ComponentArt Web.UI 2006.1 源代码 C#3.0规范(五)类型推断 C#3.0规范(四)Lambda 表达式 C#3.0规范(三)扩展函数 C#3.0(二)隐式类型化的本地变量 C#3.0规范(一)C#3.0概述 IIS不工作 Const n = 234234245# [导入]一个使用的小问题
C#3.0规范(六)重载决断
cp · 2006-05-15 · via 博客园 - cp
 

26.1.1 Overload resolution 重载决断

Lambda expressions in an argument list affect overload resolution in certain situations.

在特定情况中,lambda表达式在某个参数列表中会影响重载决断。

The following rule augments §7.4.2.3: Given a lambda expression L for which an inferred return type (§26.3.2) exists, an implicit conversion of L to a delegate type D1 is a better conversion than an implicit conversion of L to a delegate type D2 if D1 and D2 have identical parameter lists and the implicit conversion from L’s inferred return type to D1’s return type is a better conversion than the implicit conversion from L’s inferred return type to D2’s return type. If these conditions are not true, neither conversion is better.

下列规定的参数§7.4.2.3:给与一个lambda表达式L,存在某个推断的返回类型26.3.2)L的某个对于代理类型D1隐式转换比对于另一个代理类型D2的隐式转换来得好些,如果D1 D2 具有同一的参数列表并从对于D1的返回类型而言的L的推断类型而来的隐式转换比对于D2 的返回类型而言的L的推断类型而来的隐式转换要好些。如果相反的情况,则亦反之。

The following example illustrates the effect of this rule.

下列范例说明了这条规则。

class ItemList<T>: List<T>
{
public int Sum<T>(Func<T,int> selector) {
     int sum = 0;
     foreach (T item in this) sum += selector(item);
     return sum;
}

public double Sum<T>(Func<T,double> selector) {
     double sum = 0;
     foreach (T item in this) sum += selector(item);
     return sum;
}
}

The ItemList<T> class has two Sum methods. Each takes a selector argument, which extracts the value to sum over from a list item. The extracted value can be either an int or a double and the resulting sum is likewise either an int or a double.

ItemList<T>类具有两个Sum函数。每个都有一个selector参数,这从列表项里分离出总和的值。分离的值即可以是int或者double并且总和的结果不是int就是double

The Sum methods could for example be used to compute sums from a list of detail lines in an order.

范例中的Sum函数能够被用来计算某个定购中详细行的总和。

class Detail
{
public int UnitCount;
public double UnitPrice;
...
}

void ComputeSums() {
ItemList<Detail> orderDetails = GetOrderDetails(...);
int totalUnits = orderDetails.Sum(d => d.UnitCount);
double orderTotal = orderDetails.Sum(d => d.UnitPrice * d.UnitCount);
...
}

In the first invocation of orderDetails.Sum, both Sum methods are applicable because the lambda expression d => d.UnitCount is compatible with both Func<Detail,int> and Func<Detail,double>. However, overload resolution picks the first Sum method because the conversion to Func<Detail,int> is better than the conversion to Func<Detail,double>.

在首先对于derDetails.Sum的调用中,所有的Sum函数都是适用的,因为lambda表达式d => d.UnitCount对于Func<Detail,int>Func<Detail, double>都是兼容的。然而,重载决断会选择第一个Sum函数因为,Func<Detail, int>的转换要比Func<Detail, double>的转换来得好些。

In the second invocation of orderDetails.Sum, only the second Sum method is applicable because the lambda expression d => d.UnitPrice * d.UnitCount produces a value of type double. Thus, overload resolution picks the second Sum method for that invocation.

在第二个orderDetails.Sum的调用中,仅第二个Sum函数适用,因为,lambda表达式 d => d.UnitPrice * d.UnitCount 产生了一个类型为double的值。那么,重载决断为此调用选择第二个Sum函数。