13

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
  • 42,843
  • 5
  • 48
  • 70
Muz
  • 201
  • 3
  • 8
  • 9
    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://docs.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

0 Answers0