ITADN

ArraySegment<T> segment = []; segment.GetEnumerator() fails with InvalidOperationException: The underlying array is null

#9919ClosedVasiliyNovikov 创建于 2026-01-07
untriaged
V
VasiliyNovikovcommented
### Description ```cs using System; using Microsoft.VisualStudio.TestTools.UnitTesting; namespace EmptyArraySegmentTests; [TestClass] public class EmptyArraySegmentTests { [TestMethod] public void ArraySegment_Empty_Static_GetEnumerator() { var segment = ArraySegment<byte>.Empty; using var enumerator = segment.GetEnumerator(); Assert.IsFalse(enumerator.MoveNext()); } [TestMethod] public void ArraySegment_Empty_Array_GetEnumerator() { var segment = new ArraySegment<byte>([]); using var enumerator = segment.GetEnumerator(); Assert.IsFalse(enumerator.MoveNext()); } // Fails with InvalidOperationException: The underlying array is null. [TestMethod] public void ArraySegment_Empty_Collection_Expression_GetEnumerator() { ArraySegment<byte> segment = []; using var enumerator = segment.GetEnumerator(); Assert.IsFalse(enumerator.MoveNext()); } } ``` ### Reproduction Steps The above test fails on .NET 8.0/10.0 ### Expected behavior I'd expect that `ArraySegment<T> segment = [];` would be a valid usable collection ### Actual behavior The following ```cs ArraySegment<T> segment = []; segment.GetEnumerator(); ``` fails with InvalidOperationException: The underlying array is null ### Regression? _No response_ ### Known Workarounds _No response_ ### Configuration .NET 8.0 / 10.0 ### Other information _No response_
关闭于 2026-01-07 11 条评论