Implicit Type Inference only for lambda expressions ? Why ? Confused !
.net, .net-3.5, c#, lambda
Solution
It appears as if the inference fails on your second example because the compiler cannot perform overload resolution on SortInt.
This may be useful as a more thorough explanation:
http://blogs.msdn.com/ericlippert/archive/2007/11/05/c-3-0-return-type-inference-does-not-work-on-member-groups.aspx
Problem
I have the following sample code (used for C# 3.5 study purpose only !). I am making a call to the Sort function that accepts an IEnumerable and a sort function. If I call it using a lambda expression (Case A) the compiler can derive the return type TResult, however when I pass the func SortInt (Case B) the compiler throws an error ! I am unable to understand why the compiler cannot derive TResult in the second case ! I seem to be passing exactly the same information. Or is that not accurate ? Please Help ! ``` int[] intArray = { 1, 3, 2, 5, 1 }; IEnumerable<int> intArray2 = Sort(intArray, x => SortInt(x)); // <= CASE A - OK ! IEnumerable<int> nextIntArray = Sort(intArray, SortInt); // <= CASE B - Compile Error: Cannot Infer Type ! public static IEnumerable<TResult> Sort<T, TResult>(IEnumerable<T> toBeSorted, Func<IEnumerable<T>, IEnumerable<TResult>> sortFunc) { return sortFunc(toBeSorted); } public static IEnumerable<int> SortInt(IEnumerable<int> array) { return array.OrderByDescending(x => x); } ```