Repository navigation
Description
Description
OleDbParameterCollection.RemoveAt(int) throws IndexOutOfRangeException when the index is not valid. OleDbParameterCollection implements IList (through DbParameterCollection), and the documentation for IList.RemoveAt(int) lists ArgumentOutOfRangeException as the exception for an index that is not valid. IndexOutOfRangeException is not a subclass of it, so catch (ArgumentOutOfRangeException) written against the documented contract does not catch it.
The behavior comes from RangeCheck in OleDbParameterCollectionHelper.cs, which throws ADP.ParametersMappingIndex(...) (an IndexOutOfRangeException). The same check backs the indexer getter and RemoveAt. Insert(-1, ...) in the same class already throws ArgumentOutOfRangeException (from the underlying List<T>), so the type is inconsistent within one collection.
I am not asking for this to be changed blindly. I know it is long-standing behavior inherited from .NET Framework, and SqlParameterCollection has the same pattern, so changing it is a compatibility question. I'd like to know whether you would take a fix, prefer a documentation change, or consider it by design.
Reproduction Steps
No database or provider is needed. Reproduced with System.Data.OleDb 10.0.0.14 on net10.0-windows:
using System;
using System.Collections;
using System.Data.OleDb;
var parameters = new OleDbCommand().Parameters;
Try("RemoveAt(0) on an empty collection", () => parameters.RemoveAt(0));
Try("RemoveAt(-1)", () => parameters.RemoveAt(-1));
Try("IList.RemoveAt(0)", () => ((IList)parameters).RemoveAt(0));
Try("Insert(-1, p)", () => parameters.Insert(-1, new OleDbParameter()));
static void Try(string name, Action action)
{
try { action(); Console.WriteLine($"{name}: no exception"); }
catch (Exception e) { Console.WriteLine($"{name}: {e.GetType().FullName}: {e.Message}"); }
}Expected behavior
RemoveAt with an index that is not valid throws ArgumentOutOfRangeException, as documented for IList.RemoveAt (Exceptions: ArgumentOutOfRangeException, "index is not a valid index in the IList").
Actual behavior
RemoveAt(0) on an empty collection: System.IndexOutOfRangeException: Invalid index 0 for this OleDbParameterCollection with Count=0.
RemoveAt(-1): System.IndexOutOfRangeException: Invalid index -1 for this OleDbParameterCollection with Count=0.
IList.RemoveAt(0): System.IndexOutOfRangeException: Invalid index 0 for this OleDbParameterCollection with Count=0.
Insert(-1, p): System.ArgumentOutOfRangeException: Index must be within the bounds of the List. (Parameter 'index')
Regression?
I don't believe so. RangeCheck throws this type in the sources I read on main, and the behavior also appears in the 8.0.1 package. I have not compared against .NET Framework.
Known Workarounds
Catch IndexOutOfRangeException as well, or check index >= 0 && index < parameters.Count before calling.
Configuration
System.Data.OleDb10.0.0.14, .NET 10, Windows 10 x64. The behavior needs no database or OLE DB provider.
Other information
Related documentation: the DbParameterCollection.RemoveAt page names no exception for an invalid index, so the contract comes from IList.RemoveAt.
Found with the pengdows.AdoNetApiTest.conformance suite, an xUnit conformance suite for ADO.NET providers (forked from mysql-net/AdoNetApiTest) that checks the documented System.Data.Common contract. The failing tests are ParameterCollection_RemoveAt_zero_throws_when_empty and ParameterCollection_RemoveAt_negative_one_throws.
If maintainers agree this should change, I'm happy to send a PR with tests.
Activity
dotnet-policy-service commentedon Nov 5, 2026
Tagging subscribers to this area: @dotnet/area-system-collections
See info in area-owners.md if you want to be subscribed.
dotnet-policy-service commentedon Nov 5, 2026
Tagging subscribers to this area: @SamMonoRT, @dotnet/efteam
See info in area-owners.md if you want to be subscribed.