LINQ Interview Question #13

What are LINQ extension methods?

LINQ Fundamentals, Syntax, IEnumerable & IQueryable Junior Beginner

Quick Interview Answer

LINQ operators such as Where, Select, OrderBy, and Any are implemented as extension methods. Extension methods allow static methods to be called with instance-method syntax when the target type is compatible with the method's first parameter.

Detailed Explanation

LINQ operators such as Where, Select, OrderBy, and Any are implemented as extension methods. Extension methods allow static methods to be called with instance-method syntax when the target type is compatible with the method's first parameter.

The Enumerable class contains LINQ-to-Objects operators, while Queryable contains operators designed for IQueryable<T> expression trees.

Code Example

var result = numbers.Where(n => n > 5);

// Conceptually equivalent to:
var sameResult =
    Enumerable.Where(numbers, n => n > 5);