Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Why null + true = string

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "Why null + true = string". Friends who are interested might as well take a look. The method introduced in this paper is simple, fast and practical. Let the editor take you to learn "Why null + true = string"!

Consultation area

Javed Akram:

I would like to ask why null + true is a True of type string. The code is as follows:

Static void Main (string [] args)

{

String b = null + true

Console.WriteLine (b)

}

Who knows the principle behind this?

Answer area

JaredPar:

This is because once you introduce +, the C# operator binding rules work, and the compiler will help you choose the best overloading method for +, such as the following:

String operator + (string x, object y)

Obviously, the operator overload above is just enough to accommodate your null + true, and if you complete the code, it will be ((string) null) + true, so the end result is True.

By the way, this resolution is mentioned in section 7.7.4 of the C # language specification.

Comment area

In fact, this kind of question is quite boring, in real development, no one should write like this, if there is, it is either dazzling skills, or the abbreviation of StringBuilder, I will briefly talk about it, there are two main points.

Is it really operator?

I searched all the source code and couldn't find the so-called string operator + (string x, object y). Finally, I inadvertently found it in the shortcut prompt of VS.

It's a powerful VisualStudio, and it's obviously a grammatical sugar.

Decompilation of DLL looks at the underlying implementation logic

First, the program is written on .NET Core 3.1, and I decompiled it with ILSpy, as shown in the following figure:

Did you see that? The compiler has changed a simpler and rougher way to write it for you: string b = true.ToString ()? ""; is the intention of the code more obvious?

At this point, I believe you have a deeper understanding of "Why null + true = string". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report