As described in §20.6.4, type inference first occurs independently for each argument. In this initial phase, nothing is inferred from arguments that are lambda expressions. However, following the initial phase, additional inferences are made from lambda expressions using an iterative process. Specifically, inferences are made as long as one or more arguments exist for which all of the following are true:
- The argument is a lambda expression, in the following called L, from which no inferences have yet been made.
- The corresponding parameter's type, in the following called P, is a delegate type with a return type that involves one or more method type parameters.
- P and L have the same number of parameters, and each parameter in P has the same modifiers as the corresponding parameter in L, or no modifiers if L has an implicitly typed parameter list.
- P's parameter types involve no method type parameters or involve only method type parameters for which a consistent set of inferences have already been made.
- If L has an explicitly typed parameter list, when inferred types are substituted for method type parameters in P, each parameter in P has the same type as the corresponding parameter in L.
- If L has an implicitly typed parameter list, when inferred types are substituted for method type parameters in P and the resulting parameter types are given to the parameters of L, the body of L is a valid expression or statement block.
- A return type can be inferred for L, as described below.
For purposes of type inference and overload resolution, the inferred return type of a lambda expression L is determined as follows:
- If the body of L is an expression, the type of that expression is the inferred return type of L.
- If the body of L is a statement block, if the set formed by the types of the expressions in the block's return statements contains exactly one type to which each type in the set is implicitly convertible, and if that type is not the null type, then that type is the inferred return type of L.
- Otherwise, a return type cannot be inferred for L.
namespace System.Query { public static class Sequence { public static IEnumerableSelect( this IEnumerable source, Func selector) { foreach (T element in source) yield return selector(element); } } }
Listcustomers = GetCustomerList(); IEnumerable names = customers.Select(c => c.Name);
IEnumerablenames = Sequence.Select(customers, c => c.Name);
Sequence.Select(customers, (Customer c) => c.Name)
static Z F(X value, Func f1, Func f2) { return f2(f1(value)); }
double seconds = F("1:15:30", s => TimeSpan.Parse(s), t => t.TotalSeconds);
No comments:
Post a Comment