Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# 3.0 Func/OrderBy type inference

So odd situation that I ran into today with OrderBy:

Func<SomeClass, int> orderByNumber = 
  currentClass => 
   currentClass.SomeNumber;

Then:

someCollection.OrderBy(orderByNumber);

This is fine, but I was going to create a method instead because it might be usable somewhere else other than an orderBy.

private int ReturnNumber(SomeClass currentClass)
{
  return currentClass.SomeNumber;
}

Now when I try to plug that into the OrderBy:

someCollection.OrderBy(ReturnNumber);

It can't infer the type like it can if I use a Func. Seems like to me they should be the same since the method itself is "strongly typed" like the Func.

Side Note: I realize I can do this:

Func<SomeClass, int> orderByNumber = ReturnNumber;
like image 743
Programmin Tool Avatar asked Nov 06 '08 18:11

Programmin Tool


People also ask

What C is used for?

C is a powerful general-purpose programming language. It can be used to develop software like operating systems, databases, compilers, and so on.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Why is C named so?

Quote from wikipedia: "A successor to the programming language B, C was originally developed at Bell Labs by Dennis Ritchie between 1972 and 1973 to construct utilities running on Unix." The creators want that everyone "see" his language. So he named it "C".

What is main () in C?

Every C program has a primary (main) function that must be named main. If your code adheres to the Unicode programming model, you can use the wide-character version of main, wmain. The main function serves as the starting point for program execution.


1 Answers

This could also be related to "return-type type inference" not working on Method Groups.

Essentially, in cases (like Where's predicate) where the generic parameters are only in input positions, method group conversion works fine. But in cases where the generic parameter is a return type (like Select or OrderBy projections), the compiler won't infer the appropriate delegate conversion.

like image 125
Jacob Carpenter Avatar answered Sep 29 '22 17:09

Jacob Carpenter