[API Proposal]: DataTable.LoadDataRow with array length greater than the number of columns
- Dominant language
- C#
- Stars
- 18.3k
- Forks
- 5.6k
- PR merge metrics
- PR metrics pending
Description
### Background and motivation
`LoadDataRow` into `DataTable`, or more precisely, into `NewRecordFromArray` the [check](https://github.com/dotnet/runtime/blob/2a3cec1e2f89d0cbf445832d1af1b5d01bde1883/src/libraries/System.Data.Common/src/System/Data/DataTable.cs#L3390) is performed to ensure that the length of the array containing the row values does not exceed the number of columns:
```
if (colCount < value.Length)
{
throw ExceptionBuilder.ValueArrayLength();
}
```
Because of this, we cannot use built-in solutions like `ArrayPool.Shared` to pass row values, since the pool does not guarantee the exact length of the returned array.
In this case, we are forced to either perform explicit allocations without using a pool, or implement a custom pool that returns arrays with exact lengths. However, in scenarios where the number of columns and consequently, the required array length varies dynamically, the number of such pools becomes unbounded.
### API Proposal
```csharp
public DataRow LoadDataRow(ReadOnlySpan values, bool fAcceptChanges)
```
This will allow to use arrays of any origin. The methods in the chain must also accept `ReadOnlySpan`.
### API Usage
```csharp
DataTable dt = new
DataTable
();
dt.Columns.Add(nameof(TVP.Property1), typeof(int));
dt.Columns.Add(nameof(TVP.Property2), typeof(int));
dt.Columns.Add(nameof(TVP.Property3), typeof(int));
ReadOnlySpan values = ArrayPool.Shared.Rent(10).AsSpan().Slice(0, 3);
dt.LoadDataRow(values, false);
```
### Alternative Designs
Or simply skip validating the length of the input array (as described above) and instead base logic on the number of columns.
### Risks
_No response_
Contributor guide
Assessment
This issue has not been assessed yet.