Skip to content

Incorrect code example #22157

Closed
Closed

Description

@ScottStabb

This code example has a small error

foreach (int element in fibNumbers)
{
count++;
Console.WriteLine($"Element #{count}: {element}");
}

The output displays Element #1: 0.
This is incorrect as lists are zero based, so this is element 0.

While this seems basic, it threw me as I'm just learning C#, and while I thought lists were zero based, I had to stop and spend time trying to understand why this appeared to show an IEnumerable that was not zero based.

Recommended change

foreach (int element in fibNumbers)
{
Console.WriteLine($"Element #{count}: {element}");
count++;
}


Document Details

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

Activity

  1. added a commit that references this issue on Jan 4, 2021
    3566bf5
  2. BillWagner commented on Jan 4, 2021

    @BillWagner
    Member

    Thanks for noting this @ScottStabb

    I just opened PR #22214 to fix this issue.

  3. added a commit that references this issue on Jan 4, 2021
    9c9a8ae
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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