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?
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With