[英]Why would someone write string someString = “” + Convert.toChar(0) instead of someString = “0”
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'
. Convert.ToChar(0)
与'0'
。 It is the same as '\\0'
(the null character or null terminator). 它与
'\\0'
( 空字符或空终止符)相同。 Why would one use the Convert
instead of '\\0'
? 为什么要使用
Convert
而不是'\\0'
? I see no reason why. 我没有理由。
Using "" +
before the character converts it to a string
instead of an char
. 在字符将字符转换为
string
而不是char
之前使用"" +
。 I prefer to use .ToString()
since that makes clear what actually happens. 我更喜欢使用
.ToString()
因为这样可以弄清楚实际发生了什么。 But converting here is absolutely useless since you can simply construct a string
at once. 但是在这里进行转换绝对没有用,因为您可以一次简单地构造一个
string
。 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. "0"
是由数字零和嵌入的字符串终止符组成的字符串文字。
I think "" + convertToChar(0)
has a string terminator due to ""
and another one due to convertToChar(0)
. 我认为
"" + convertToChar(0)
由于""
有一个字符串终止符,而另一个由于convertToChar(0)
而有一个字符串终止符。 So it's still a zero length string. 所以它仍然是零长度的字符串。
Abbreviating the latter as "\\0"
is more conventional. 通常将后者缩写为
"\\0"
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.