Skip to content

Latest commit

 

History

History
27 lines (16 loc) · 1.18 KB

enumerableemptylttresultgt-always-returns-cached-instance.md

File metadata and controls

27 lines (16 loc) · 1.18 KB

Enumerable.Empty<TResult> always returns cached instance

Details

Beginning in .NET Framework 4.5, xref:System.Linq.Enumerable.Empty%60%601 always returns a cached internal instance xref:System.Collections.Generic.IEnumerable%601.Previously, xref:System.Linq.Enumerable.Empty%60%601 would cache an empty xref:System.Collections.Generic.IEnumerable%601 at the time the API was called, meaning that in some conditions in which xref:System.Linq.Enumerable.Empty%60%601 was called rapidly and concurrently, different instances of the type could be returned for different calls to the API.

Suggestion

Because the previous behavior was non-deterministic, code is unlikely to depend on it. However, in the unlikely case that empty enumerables are being compared and expected to sometimes be unequal, explicit empty arrays should be created (new T[0]) instead of using xref:System.Linq.Enumerable.Empty%60%601.

Name Value
Scope Edge
Version 4.5
Type Runtime

Affected APIs

  • xref:System.Linq.Enumerable.Empty%60%601?displayProperty=nameWithType