Skip to content

Diagnostics.TagList does not support IEnumerable or CopyTo when Empty #122848

@samueleaton

Description

@samueleaton

Description

TagList throws exception when empty and consuming as in IEnumerable or when coping to an array. This is not the case with ActivityTagsCollection.

(Example in dotnet fsi using F#):

open System.Collections.Generic;;
open System.Diagnostics;;

let myArray: KeyValuePair<string,obj> array = [||]

// ActivityTagsCollection.CopyTo works as expected when empty
ActivityTagsCollection().CopyTo(myArray, 0);;

// TagList.CopyTo throws exception when empty
System.Diagnostics.TagList().CopyTo(myArray, 0);;
// System.ArgumentOutOfRangeException: arrayIndex ('0') must be less than '0'. (Parameter 'arrayIndex')
// Actual value was 0.
//    at System.ArgumentOutOfRangeException.ThrowGreaterEqual[T](T value, T other, String paramName)
//    at System.ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual[T](T value, T other, String paramName)
//    at System.Diagnostics.TagList.CopyTo(KeyValuePair`2[] array, Int32 arrayIndex)
// Stopped due to error


// Converting an empty ActivityTagsCollection IEnumerable to an array works
System.Diagnostics.ActivityTagsCollection() |> Seq.toArray;;
// KeyValuePair<string,obj> array = [||]

// Converting an empty TagList (as an IEnumerable) to an array throws exception because
// it uses CopyTo internally
TagList() |> Seq.toArray;;
// System.ArgumentOutOfRangeException: arrayIndex ('0') must be less than '0'. (Parameter 'arrayIndex')
// Actual value was 0.
//    at System.ArgumentOutOfRangeException.ThrowGreaterEqual[T](T value, T other, String paramName)
//    at System.ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual[T](T value, T other, String paramName)
//    at System.Diagnostics.TagList.CopyTo(KeyValuePair`2[] array, Int32 arrayIndex)
// Stopped due to error

Reproduction Steps

let arr: KeyValuePair<string,obj> array = [||]
TagList().CopyTo(arr, 0)

Expected behavior

Should do the same as the other CopyTo method. This does not throw an exception:

let arr: KeyValuePair<string,obj> array = [||]
TagList().CopyTo(arr)

Actual behavior

System.ArgumentOutOfRangeException: arrayIndex ('0') must be less than '0'. (Parameter 'arrayIndex')
Actual value was 0.
   at System.ArgumentOutOfRangeException.ThrowGreaterEqual[T](T value, T other, String paramName)
   at System.ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual[T](T value, T other, String paramName)
   at System.Diagnostics.TagList.CopyTo(KeyValuePair`2[] array, Int32 arrayIndex)

Regression?

No response

Known Workarounds

No response

Configuration

No response

Other information

Line where exception is thrown:

ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual((uint)arrayIndex, (uint)array.Length, nameof(arrayIndex));

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions