.NET Core 3.1 v .NET 6.0

18.4k Views Asked by At

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.

1

There are 1 best solutions below

0
Hadi On

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))