Closed
Description
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.
- ID: d710feeb-8f03-b211-a070-155938929d6b
- Version Independent ID: c980ea8e-ffb4-fbb1-1761-292858616cdd
- Content: C# foreach statement
- Content Source: docs/csharp/language-reference/keywords/foreach-in.md
- Product: dotnet-csharp
- Technology: csharp-language-reference
- GitHub Login: @BillWagner
- Microsoft Alias: wiwagn