Skip to content

.NET 6 incorrect return of IndexOf #28218

@jonadv

Description

@jonadv

On this page it sais:

  • In previous versions of .NET on Windows, the snippet prints 6.
  • In .NET 5 and later versions on Windows 19H1 and later versions, the snippet prints -1.

but when I run that code snippet, in a new .NET 6 project in Visual Studio 2022, it still returns 6!

string s = "Hello\r\nworld!";
int idx = s.IndexOf("\n");
Console.WriteLine(idx);

When we create a project with .NET 5, it does return -1. But with NET 6 (a version later then 5) it returns 6! What is going on?

image

Not sure if helpful:
image


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