LINQ Interview Question #85

What is the difference between ToList and ToArray?

Deferred Execution, Immediate Execution & Query Behavior Mid-Level Intermediate

Quick Interview Answer

Both enumerate the source immediately and materialize the results. ToList returns List<T>, while ToArray returns T[].

Detailed Explanation

Both enumerate the source immediately and materialize the results. ToList returns List<T>, while ToArray returns T[].

Choose based on how the result will be consumed. A list provides list operations and can grow, while an array has a fixed length and can be suitable for APIs or scenarios that naturally expect arrays.

Code Example

List<int> list = numbers.ToList();
int[] array = numbers.ToArray();