From 705442380446f6567124d9385dcd8d53c406f255 Mon Sep 17 00:00:00 2001 From: KaliCZ Date: Thu, 12 Oct 2023 15:20:30 +0200 Subject: [PATCH] Disallowed changing the items within NonEmptyEnumerable. --- .../Collections/NonEmptyEnumerable.cs | 20 +++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/src/FuncSharp/Collections/NonEmptyEnumerable.cs b/src/FuncSharp/Collections/NonEmptyEnumerable.cs index edf6b62..80544e5 100644 --- a/src/FuncSharp/Collections/NonEmptyEnumerable.cs +++ b/src/FuncSharp/Collections/NonEmptyEnumerable.cs @@ -76,11 +76,23 @@ private NonEmptyEnumerable(T head, IReadOnlyList tail) public int Count { get; } - public T this[int index] => index switch + public T this[int index] { - 0 => Head, - _ => Tail[index - 1] - }; + get + { + if (index < 0 || index >= Count) + { + throw new ArgumentOutOfRangeException(nameof(index)); + } + + return index switch + { + 0 => Head, + _ => Tail[index - 1] + }; + } + set => throw new NotSupportedException("Cannot modify items within NonEmptyEnumerable."); + } public IEnumerator GetEnumerator() {