简体   繁体   English

.NET 核心 3.1 v .NET 6.0

[英].NET Core 3.1 v .NET 6.0

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.我对 Visual Studio 2022、C# 10 和 .NET 6.0 的发布感到非常兴奋,并下载并安装了社区版并测试了我正在处理的项目。 I changed the target framework to 6.0 and performed a clean build.我将目标框架更改为 6.0 并执行了干净的构建。 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.我开始四处挖掘,并惊讶地发现 .NET Core 3.1 和 .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:使用“netcoreapp3.1”的目标框架,我得到了以下 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:使用“net6.0”的目标框架,我得到了以下 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).所以,我只能假设它是一个 unicode 设置,但我在任何地方都找不到它(如果有的话)。 My understanding is that all strings are UTF16 but why the difference between frameworks.我的理解是所有字符串都是 UTF16 但为什么框架之间的差异。

And yes, I can see the bug in my code, it should be a char anyway but it was working fine using 'netcoreapp3.1'.是的,我可以在我的代码中看到错误,无论如何它应该是一个字符,但使用“netcoreapp3.1”可以正常工作。

Can anyone shed some light on this please.任何人都可以对此有所了解。

After.Net Core 3, you must highlight your comparison mode by StringComparison code.在.Net Core 3 之后,您必须通过StringComparison代码突出显示您的比较模式。

change改变

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

with

if (text.StartsWith(CTCPDelimiterString, StringComparison.Ordinal) && text.EndsWith(CTCPDelimiterString, StringComparison.Ordinal))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM