Skip to content

Clarify range comment in array slicing example to avoid confusion between indexes and values #53871

@antonymerle

Description

@antonymerle

Type of issue

Missing information

Description

In the following C# code snippet:

string second = names[1]; // 0-based index
string last = names[^1]; // ^1 is the last element
int[] smallNumbers = numbers[0..5]; // 0 to 4

the comment // 0 to 4 may be confusing because the numbers array values are also 1 through 10.

A reader could interpret the comment as referring to the values included in smallNumbers rather than the indexes used by the range expression.

Suggested improvement

Consider clarifying that the comment refers to indexes, for example:

int[] smallNumbers = numbers[0..5]; // indexes 0 through 4

or

int[] smallNumbers = numbers[0..5]; // elements at indexes 0-4

This would make the example easier to understand for readers who are new to range syntax.

Page URL

https://learn.microsoft.com/en-us/dotnet/csharp/tour-of-csharp/overview

Content source URL

https://github.com/dotnet/docs/blob/main/docs/csharp/tour-of-csharp/overview.md

Document Version Independent Id

ffae9ae1-d3f5-4192-0a18-ca475c712813

Platform Id

41a669fb-55ed-d212-8af3-4d212ac707a2

Article author

@BillWagner

Metadata

  • ID: 85b00578-37d2-1fd1-911d-3097aa73f212
  • PlatformId: 41a669fb-55ed-d212-8af3-4d212ac707a2
  • Service: dotnet-csharp
  • Sub-service: get-started

Related Issues

Metadata

Metadata

Assignees

No one assigned

    Labels

    dotnet-csharp/svcget-started/subsvcgood first issueIssue should be good for first-time contributors, with clear instructionshelp wantedGood for community contributors to help [up-for-grabs]

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions