From 2268e6bd12b5e470f2ca319be0a49a30f534a241 Mon Sep 17 00:00:00 2001 From: Stephen Toub Date: Mon, 26 Feb 2024 20:29:38 -0500 Subject: [PATCH] Special-case arrays in Enumerable.DefaultIfEmpty Arrays are fixed-length, and DefaultIfEmpty only applies special behaviors on top of the input enumerable if it's empty. Thus, we can special-case arrays, such that if the input is a non-empty array, we just return the original input rather than wrapping it in a new enumerable. --- src/libraries/System.Linq/src/System/Linq/DefaultIfEmpty.cs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/libraries/System.Linq/src/System/Linq/DefaultIfEmpty.cs b/src/libraries/System.Linq/src/System/Linq/DefaultIfEmpty.cs index 593a6b8a67b3d1..b4bef955c0784f 100644 --- a/src/libraries/System.Linq/src/System/Linq/DefaultIfEmpty.cs +++ b/src/libraries/System.Linq/src/System/Linq/DefaultIfEmpty.cs @@ -18,6 +18,11 @@ public static IEnumerable DefaultIfEmpty(this IEnumerable 0 }) + { + return source; + } + return new DefaultIfEmptyIterator(source, defaultValue); }