Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
206 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 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...