From 20611a573ef2df6ac32eba9ccf856d3529c5df3d Mon Sep 17 00:00:00 2001 From: prozolic <42107886+prozolic@users.noreply.github.com> Date: Fri, 15 Aug 2025 01:23:58 +0900 Subject: [PATCH] Optimize ImmutableArray.ElementAtOrDefault --- .../src/System/Linq/ImmutableArrayExtensions.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/libraries/System.Collections.Immutable/src/System/Linq/ImmutableArrayExtensions.cs b/src/libraries/System.Collections.Immutable/src/System/Linq/ImmutableArrayExtensions.cs index 91b061d835a162..48bca1390fe842 100644 --- a/src/libraries/System.Collections.Immutable/src/System/Linq/ImmutableArrayExtensions.cs +++ b/src/libraries/System.Collections.Immutable/src/System/Linq/ImmutableArrayExtensions.cs @@ -1,4 +1,4 @@ -// Licensed to the .NET Foundation under one or more agreements. +// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. using System.Collections.Generic; @@ -315,12 +315,12 @@ public static T ElementAt(this ImmutableArray immutableArray, int index) /// The type of element contained by the collection. public static T? ElementAtOrDefault(this ImmutableArray immutableArray, int index) { - if (index < 0 || index >= immutableArray.Length) + if ((uint)index < (uint)immutableArray.Length) { - return default; + return immutableArray[index]; } - return immutableArray[index]; + return default; } ///