Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Why Behavior changes when comparing strings on .NET 5+? #21575

Closed
Ali-YousefiTelori opened this issue Nov 15, 2020 · 1 comment
Closed

Why Behavior changes when comparing strings on .NET 5+? #21575

Ali-YousefiTelori opened this issue Nov 15, 2020 · 1 comment
Labels
dotnet/svc Pri1 High priority, do before Pri2 and Pri3 product-feedback Indicates issues that are related to product issues, not docs [org][type][category]

Comments

@Ali-YousefiTelori
Copy link

Hello,

When you change default values of the structure of .Net in version 5+ that means you are breaking the backward-compatible of c# and .net framework.

It's like you change the default value of bool to 'true'.
Why you should do this way and say to developers change all of your codes?
Why Developers have to change all of the default values of bool to false manually?
for example:
I'm using this code:

public bool Success {get;set;}

the default value of the Success property is false because of the default value of boolean in c# from version 1.0.
so when you change the default value to true in CLR, you have to tell thousands of developers to change this value to false manually like this way:

public bool Success {get;set;} = false;

Do you know how many codes have to change?
Is this design structure true? I want to see the .Net5 project manager !!
So I want to know why Microsoft is going to change default values in NET5+?
You are just wasting our time in this breaking change and you don't know how many codes we have to change for this. how many bugs we have to fix for this breaking changes?

examples to check:

            string s = "Hello\r\nworld!";
            int idx = s.IndexOf("\n");
            Console.WriteLine(idx);
            //Outoput is -1 in .Net5+
            //Outoput is 6 in .Net3.1-

example 2:

            string s = "Hello\r\nworld!";
            int idx = s.IndexOf('\n');
            Console.WriteLine(idx);
            //Outoput is 6 in Both .Net5+ and .Net3.1-

check this blow link pls:
https://docs.microsoft.com/en-us/dotnet/standard/base-types/string-comparison-net-5-plus


Document Details

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

@PRMerger6 PRMerger6 added dotnet/svc Pri1 High priority, do before Pri2 and Pri3 labels Nov 18, 2020
@adegeo adegeo added the product-feedback Indicates issues that are related to product issues, not docs [org][type][category] label Dec 7, 2020
@dotnet-bot dotnet-bot removed the ⌚ Not Triaged Not triaged label Dec 7, 2020
@adegeo
Copy link
Contributor

adegeo commented Dec 7, 2020

Greetings!

If you look at the Revert back to NLS behaviors section of the article, you'll see a link to .NET globalization and ICU and there is a section that shows you how to force the app into the old behavior. If you force it to the old behavior you will always have the possibility of having different values on windows/linux/mac for the same code.

As stated in the article, the reason for the forced change was to unify behavior across all platforms. Before this change you would get different results on different platforms for .IndexOf("\n") for example. Now you will get the same value regardless of if someone is running on linux vs windows. The code for .IndexOf(char) vs .IndexOf(string) uses different logic as the string version needs to process and compare in a different way. char always uses the ordinal by default.

Prior to this change, you would get different values for Windows and Linux:

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

// '6' when running on .NET Framework (Windows)
// '6' when running on .NET Core 2.x - 3.x (Windows)
// '-1' when running on .NET Core 2.x - 3.x or .NET 5 (non-Windows)
// '-1' when running on .NET 5 (Windows)

When you use the proper comparison, you see the intended result:

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

// '6' when running on .NET Framework (Windows)
// '6' when running on .NET Core 2.x - 3.x (Windows)
// '6' when running on .NET Core 2.x - 3.x or .NET 5 (non-Windows)
// '6' when running on .NET 5 (Windows)

This repo is just for the documentation and you can discuss design decisions with the product team on this subject in this thread in the dotnet/runtime repository.

Cheers!

@adegeo adegeo closed this as completed Dec 7, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
dotnet/svc Pri1 High priority, do before Pri2 and Pri3 product-feedback Indicates issues that are related to product issues, not docs [org][type][category]
Projects
None yet
Development

No branches or pull requests

4 participants