16

I got all excited on the release of Visual Studio 2022, C# 10 and .NET 6.0 and downloaded and installed the community edition and tested a project I am working on. I changed the target framework to 6.0 and performed a clean build. Great, everything built as expected.

So, onwards and upwards and ran the project. The very first test failed. I must say I was surprised. I started digging around and was really surprised to find a difference between .NET Core 3.1 and .NET 6.0.

Here is a sample program:

public class Program
{
    public static readonly string CTCPDelimiterString = "\x0001";
    public static readonly char CTCPDelimiterChar = '\x0001';

    public static void Main(string[] args)
    {
        string text = "!sampletext";

        Console.Write("  Using a char: ");
        if (text.StartsWith(CTCPDelimiterChar) && text.EndsWith(CTCPDelimiterChar))
        {
            Console.WriteLine("got CTCP delimiters");
        }
        else
        {
            Console.WriteLine("did not get CTCP delimiters");
        }

        Console.Write("Using a string: ");
        if (text.StartsWith(CTCPDelimiterString) && text.EndsWith(CTCPDelimiterString))
        {
            Console.WriteLine("got CTCP delimiters");
        }
        else
        {
            Console.WriteLine("did not get CTCP delimiters");
        }
    }
}

Using a target framework of 'netcoreapp3.1' I got the following output:

  Using a char: did not get CTCP delimiters
Using a string: did not get CTCP delimiters

Using a target framework of 'net6.0' I got the following output:

  Using a char: did not get CTCP delimiters
  Using a string: got CTCP delimiters

So, I can only assume that it is a unicode setting but I cannot find it anywhere (if there is one). My understanding is that all strings are UTF16 but why the difference between frameworks.

And yes, I can see the bug in my code, it should be a char anyway but it was working fine using 'netcoreapp3.1'.

Can anyone shed some light on this please.

Guru Stron
  • 102,774
  • 10
  • 95
  • 132
Muz
  • 289
  • 1
  • 3
  • 11
  • 12
    There has been, in fact, a breaking change introduced in .NET Core 5 (and by extension, remains in 6) which affects Unicode text handling. You can read about it here: https://learn.microsoft.com/en-us/dotnet/standard/base-types/string-comparison-net-5-plus – Alejandro Nov 20 '21 at 00:44
  • 4
    @Alejandro Thanks so much for the link. The scary part is that I added the recommended rule changes and now I have 138 errors and 1813 warnings! I guess I have a date for coffee and bugs. :) – Muz Nov 20 '21 at 01:28
  • Does this answer your question? [string.IndexOf returns different value in .NET 5.0](https://stackoverflow.com/questions/64833645/string-indexof-returns-different-value-in-net-5-0) – Guru Stron Nov 20 '21 at 20:19
  • Also [this](https://stackoverflow.com/questions/65574888/c-sharp-anystring-contains-0-stringcomparison-invariantculture-returns-tr) – Guru Stron Nov 20 '21 at 20:19
  • 2
    Very poor question title here. Would strongly recommend rewording to be more specific. Having to open the question to know what it is about is bad and wastes a lot of people's time. – julealgon Jul 07 '22 at 18:03

1 Answers1

2

After .Net Core 3, you must highlight your comparison mode by StringComparison code.

change

if (text.StartsWith(CTCPDelimiterString) && text.EndsWith(CTCPDelimiterString))

with

if (text.StartsWith(CTCPDelimiterString, StringComparison.Ordinal) && text.EndsWith(CTCPDelimiterString, StringComparison.Ordinal))
Hadi
  • 100
  • 1
  • 11