Skip to content

Range operators #32824

@ygoe

Description

@ygoe

I'm confused. What does range[2..4] give me? From 2 to 4 (inclusive), which is 3 items? Or from 2 to 4 (exclusive), which is 2 to 3? Or from 2 on 4 items, which is 2 to 5? The description has very much text but nothing to find that could answer this simple question. The C# code editor tooltips and dropdowns don't even know what I'm doing here, explaining me that I'm typing a single byte offset number.

My problem is that I have this code: Encoding.ASCII.GetString(responseBody.Span[index..(index + length - 1)]) which should read length bytes starting at index but gives me one character less than length. When I still used the non-span code with buffer/offset/count, it worked and was clearly readable. Following these modern style suggestions caused me a lot of work (the range/memory stuff is just as viral as async/await) and now this problem.


Document Details

Do not edit this section. It is required for learn.microsoft.com ➟ GitHub issue linking.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions