Search code examples
c#executionoverloading

Execution Order Of Overloaded Functions in C#


Just wondering how C# determines the order of execution on an object in Method Overload For instance I have a Method

bool SomeMethod(IQueryable source)
{
  //.... Do Work
}

bool SomeMethod(IEnumerable source)
{
   //.... Do Work
}

var Obj = Db.Table.Where(ID > 5);

SomeMethod(Obj);

When I run this the code goes to the IQueryable Overload, Now is that because I declared IQueryable First, is it because IQueryable is of Type IEnumerable or is there some other underlying reason why?

-Thanks


Solution

  • There is a betterness algorithm (section 7.4.3 of the C# spects) for deciding which method overload to use in the event that there are multiple applicable overloads. In this case an IQueryable is implicitly convertible to an IEnumerable, but an IEnumerable is not implicitly convertible to an IQueryable, making IQueryable the "better" overload.

    See Also
    7.4.2.3 Better conversion