Skip to content

Commit fef2e3a

Browse files
authored
Make RemoveAt follow the IList contract (#1291)
1 parent 9f78da8 commit fef2e3a

2 files changed

Lines changed: 33 additions & 3 deletions

File tree

‎src/FirebirdSql.Data.FirebirdClient.Tests/FbParameterCollectionTests.cs‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,29 @@ namespace FirebirdSql.Data.FirebirdClient.Tests;
2626
[NoServerCategory]
2727
public class FbParameterCollectionTests
2828
{
29+
[TestCase(-1)]
30+
[TestCase(0)]
31+
[TestCase(1)]
32+
public void RemoveAtInvalidIndexThrowsArgumentOutOfRangeTest(int index)
33+
{
34+
var command = new FbCommand();
35+
if (index == 1)
36+
{
37+
command.Parameters.Add("@p1", 1);
38+
}
39+
40+
var ex = Assert.Throws<ArgumentOutOfRangeException>(() => command.Parameters.RemoveAt(index));
41+
Assert.AreEqual("index", ex.ParamName);
42+
Assert.AreEqual(index == 1 ? 1 : 0, command.Parameters.Count);
43+
}
44+
45+
[Test]
46+
public void RemoveAtUnknownNameThrowsIndexOutOfRangeTest()
47+
{
48+
var command = new FbCommand();
49+
Assert.Throws<IndexOutOfRangeException>(() => command.Parameters.RemoveAt("@missing"));
50+
}
51+
2952
[Test]
3053
public void AddTest()
3154
{

‎src/FirebirdSql.Data.FirebirdClient/FirebirdClient/FbParameterCollection.cs‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -254,9 +254,10 @@ public override void Remove(object value)
254254

255255
public override void RemoveAt(int index)
256256
{
257-
if (index < 0 || index > Count)
257+
// IList.RemoveAt: ArgumentOutOfRangeException when "index is not a valid index in the IList".
258+
if (index < 0 || index >= Count)
258259
{
259-
throw new IndexOutOfRangeException("The specified index does not exist.");
260+
throw new ArgumentOutOfRangeException(nameof(index), index, "The specified index does not exist.");
260261
}
261262

262263
var parameter = this[index];
@@ -266,7 +267,13 @@ public override void RemoveAt(int index)
266267

267268
public override void RemoveAt(string parameterName)
268269
{
269-
RemoveAt(IndexOf(parameterName));
270+
var index = IndexOf(parameterName);
271+
if (index < 0)
272+
{
273+
throw new IndexOutOfRangeException("The specified index does not exist.");
274+
}
275+
276+
RemoveAt(index);
270277
}
271278

272279
public void CopyTo(FbParameter[] array, int index)

0 commit comments

Comments
 (0)