Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
201 views
in Technique[技术] by (71.8m points)

.net core - NET 5.0 null char handled differently

When re-targeting code from .NET Core 3.1 to .NET 5.0 I discovered the following quirk in a .NET Core 3.x supplied USB DLL interface code.

.NET Core 3.x interprets "" as the string null character '' as in

String.IndexOf("")

, whilst .NET 5.0 does not.

This string null character usage also noted in Trim not working on null characters for String.Replace

Not sure if the above target difference Microsoft intended or not?

static void Main(string[] args)
        {            
            byte[] testBytes = { 0x41, 0x42, 0x43, 0x0, 0x0, 0x0, 0x0, 0x0 }; // ABC
            string testString = Encoding.ASCII.GetString(testBytes);

            int ns = testString.IndexOf("");  // .Net Core 3.x = 3 - .Net 5.0 = 0
            int nc = testString.IndexOf('');  // .Net Core 3.x = 3 - .Net 5.0 = 3

            Console.WriteLine(string.Format("testString={0}, IndexOf: null string = {1:d} null character = {2:d}", testString, ns, nc));
            Console.ReadKey();
        }
question from:https://stackoverflow.com/questions/65837857/net-5-0-null-char-handled-differently

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...