1

In C#, given that:

  • IEnumerable<T> declares Where(Func<T, bool>)
  • IQueryable<T> extends IEnumerable<T>
  • IQueryable<T> declares Where(Expression<Func<T, bool>>))

When using this:

var list = new List<int>();
var positives = list.AsQueryable().Where(x => x > 0);

How does the C# compiler distinguish between both methods?

Spiff
  • 2,266
  • 23
  • 36

1 Answers1

1

The overload of Where that accepts an Expression<Func<T, bool>> is an extension method of IQueryable<T> and since IQueryable<T> extends IEnumerable<T>, it will be chosen first due to the overload resolution mechanism in C#.

Please refer to @Jon Skeet's article on the subject for more information:

If there are two methods at different levels of the hierarchy, the "deeper" one will be chosen first ...

mm8
  • 163,881
  • 10
  • 57
  • 88