Skip to content

Incorrect code example #22157

Closed
Closed
@ScottStabb

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.

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