Skip to content

Span<T>'s Slice method do not throw if Length property is supplied as start parameter #28611

@Gnbrkm41

Description

@Gnbrkm41

From MS Docs page on Span<T>.Slice(int) method: ArgumentOutOfRangeException is thrown if index(probably start?) is less than zero or greater than or equal to Length.

However, span.Slice(span.Length) does not seem to throw; The example code below executes with no problem.

"asd".ToCharArray().AsSpan().Slice(3)

It appears to be an incorrect document rather than a problem with the implementation, as the documentation for ReadOnlySpan<T>.Slice(int) says that ArgumentOutOfRangeException is thrown if start is greater than the number of items in the read-only span.

Is this behaviour expected? I also have found that XML documentations on these methods (Span.Slice, ReadOnlySpan.Slice) says so as well.

/// <exception cref="System.ArgumentOutOfRangeException">
/// Thrown when the specified <paramref name="start"/> or end index is not in range (&lt;0 or &gt;=Length).
/// </exception>

<0 or >=Length seems to imply that it should throw if I pass Length as the parameter.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-System.MemorydocumentationDocumentation bug or enhancement, does not impact product or test codehelp wanted[up-for-grabs] Good issue for external contributors

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions