From 3d6fecc90202d4221e0e99594dbf24e32a9b39fb Mon Sep 17 00:00:00 2001 From: "Dailey, Alaric" Date: Tue, 20 Oct 2026 08:40:16 -0500 Subject: [PATCH] Make RemoveAt follow IList contract --- .../FbParameterCollectionTests.cs | 24 +++++++++++++++++++ .../FirebirdClient/FbParameterCollection.cs | 13 +++++++--- 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/src/FirebirdSql.Data.FirebirdClient.Tests/FbParameterCollectionTests.cs b/src/FirebirdSql.Data.FirebirdClient.Tests/FbParameterCollectionTests.cs index 9fa3617e7..756772dc5 100644 --- a/src/FirebirdSql.Data.FirebirdClient.Tests/FbParameterCollectionTests.cs +++ b/src/FirebirdSql.Data.FirebirdClient.Tests/FbParameterCollectionTests.cs @@ -26,6 +26,30 @@ namespace FirebirdSql.Data.FirebirdClient.Tests; [NoServerCategory] public class FbParameterCollectionTests { + [TestCase(-1)] + [TestCase(0)] + [TestCase(1)] + public void RemoveAtInvalidIndexThrowsArgumentOutOfRangeTest(int index) + { + // IList.RemoveAt: ArgumentOutOfRangeException when "index is not a valid index in the IList". + var command = new FbCommand(); + if (index == 1) + { + command.Parameters.Add("@p1", 1); + } + + var ex = Assert.Throws(() => command.Parameters.RemoveAt(index)); + Assert.AreEqual("index", ex.ParamName); + Assert.AreEqual(index == 1 ? 1 : 0, command.Parameters.Count); + } + + [Test] + public void RemoveAtUnknownNameThrowsIndexOutOfRangeTest() + { + var command = new FbCommand(); + Assert.Throws(() => command.Parameters.RemoveAt("@missing")); + } + [Test] public void AddTest() { diff --git a/src/FirebirdSql.Data.FirebirdClient/FirebirdClient/FbParameterCollection.cs b/src/FirebirdSql.Data.FirebirdClient/FirebirdClient/FbParameterCollection.cs index b159fa679..1499d95ca 100644 --- a/src/FirebirdSql.Data.FirebirdClient/FirebirdClient/FbParameterCollection.cs +++ b/src/FirebirdSql.Data.FirebirdClient/FirebirdClient/FbParameterCollection.cs @@ -254,9 +254,10 @@ public override void Remove(object value) public override void RemoveAt(int index) { - if (index < 0 || index > Count) + // IList.RemoveAt: ArgumentOutOfRangeException when "index is not a valid index in the IList". + if (index < 0 || index >= Count) { - throw new IndexOutOfRangeException("The specified index does not exist."); + throw new ArgumentOutOfRangeException(nameof(index), index, "The specified index does not exist."); } var parameter = this[index]; @@ -266,7 +267,13 @@ public override void RemoveAt(int index) public override void RemoveAt(string parameterName) { - RemoveAt(IndexOf(parameterName)); + var index = IndexOf(parameterName); + if (index < 0) + { + throw new IndexOutOfRangeException("The specified index does not exist."); + } + + RemoveAt(index); } public void CopyTo(FbParameter[] array, int index)