freak
freak

Reputation: 483

passing Func<TSource, TKey> keySelector error

static void Main()
        {
string[] a = { "a", "asd", "bdfsd", "we" };
            a = a.OrderBy(fun).ToArray();
}

 private static int fun(string s)
        {
            return s.Length;
        }

its is giving compile time error . I know that we can do this with Lambda expression like this. a.OrderBy(s=>s.Length).ToArray(); but i want to this do by defining different function . What mistake i have done?

Upvotes: 3

Views: 7651

Answers (3)

Eric Lippert
Eric Lippert

Reputation: 660189

Here's what happened. When I first implemented the method type inference algorithm for C# 3 I reasoned as SLaks suggests: method groups have no type, nothing was inferred from them in C# 2, and overload resolution needs to pick the method out of the method group by knowing the types of the arguments, which is precisely what we are trying to infer; this is a chicken and egg problem. I blogged about that in November of 2007:

http://blogs.msdn.com/ericlippert/archive/2007/11/05/c-3-0-return-type-inference-does-not-work-on-member-groups.aspx

There was so much pushback on this that we decided to revisit the issue and change the type inference algorithm so that we made inferences from method groups provided enough inferences had already been done that overload resolution could proceed on the method group.

Unfortunately that change came too late in the cycle and did not make it into C# 3. We postponed it to C# 4, and there you go.

I blogged about that in 2008:

http://blogs.msdn.com/ericlippert/archive/2008/05/28/method-type-inference-changes-part-zero.aspx

Upvotes: 3

Andrew Hare
Andrew Hare

Reputation: 351536

SLaks is correct in that the C# 3 compiler will not allow this but it is important to point out that the C# 4 compiler will compile your example without issue.

Upvotes: 4

SLaks
SLaks

Reputation: 887547

The expression fun is an untyped expression called a method group.
Since a method group has no type, the compiler cannot infer the type parameters of the generic OrderBy method.

You need to explicitly pass the type parameters, like this:

a = a.OrderBy<string, int>(fun).ToArray();

Or,

a = a.OrderBy(new Func<string, int>(fun)).ToArray();

Upvotes: 6

Related Questions