Michael Chai

I am SErVice-oRienTed !

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
 

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函数。

posted on 2006-05-15 21:00  cp  阅读(1059)  评论(0编辑  收藏  举报