From 239d45fc8c83c6e7f341ad8937ebd88b120495fd Mon Sep 17 00:00:00 2001 From: mlemanczyk Date: Sat, 1 Apr 2023 16:38:53 +0200 Subject: [PATCH] RecyclableArrayList.IndexOf improved to not raise exception when the list is empty. --- Recyclable.Collections/RecyclableArrayList.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Recyclable.Collections/RecyclableArrayList.cs b/Recyclable.Collections/RecyclableArrayList.cs index 348cfb1..1d4bce4 100644 --- a/Recyclable.Collections/RecyclableArrayList.cs +++ b/Recyclable.Collections/RecyclableArrayList.cs @@ -6,6 +6,7 @@ namespace Recyclable.Collections { public class RecyclableArrayList : IEnumerable, IList, IDisposable { + private const int ItemNotFound = -1; private static readonly ArrayPool _arrayPool = ArrayPool.Create(); private static readonly IEqualityComparer _equalityComparer = EqualityComparer.Default; @@ -388,7 +389,7 @@ namespace Recyclable.Collections } } - public int IndexOf(T itemToFind) => Array.IndexOf(_memoryBlock, itemToFind, 0, _count); + public int IndexOf(T itemToFind) => _count > 0 ? Array.IndexOf(_memoryBlock, itemToFind, 0, _count) : ItemNotFound; public void Insert(int index, T item) {