Why would someone write
string someString = "" + Convert.ToChar(0);
instead of
string someString = "0";
I saw this in some code related to smart cards. Is there a particular reason for this, from a technical standpoint?
Convert.ToChar(0)
is not the same as '0'
. It is the same as '\\0'
(the null character or null terminator). Why would one use the Convert
instead of '\\0'
? I see no reason why.
Using "" +
before the character converts it to a string
instead of an char
. I prefer to use .ToString()
since that makes clear what actually happens. But converting here is absolutely useless since you can simply construct a string
at once. So conclusion, the code can be written the best as:
"\0"
"0"
is a string literal consisting of the number zero and an embedded string terminator.
I think "" + convertToChar(0)
has a string terminator due to ""
and another one due to convertToChar(0)
. So it's still a zero length string.
Abbreviating the latter as "\\0"
is more conventional.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.