字符串和整数,隐式和显式
有一位同事问我这个问题,在我的大脑困惑的状态下,我没有答案:
为什么你可以这样做:
string ham = "ham " + 4;
但不能:
string ham = 4;
当你连接<时,如果存在用于字符串转换的隐式强制转换/操作< /em>,为什么分配它作为字符串时不一样? (当然,不进行一些运算符重载)
Had a coworker ask me this, and in my brain befuddled state I didn't have an answer:
Why is it that you can do:
string ham = "ham " + 4;
But not:
string ham = 4;
If there's an implicit cast/operation for string conversion when you are concatenating, why not the same when assigning it as a string? (Without doing some operator overloading, of course)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
连接时,编译器会将语句
"ham" + 4
转换为对String.Concat
的调用,该调用采用两个object
参数,因此值4
被装箱,然后调用ToString
。对于赋值,没有从
int
到string
的隐式转换,因此您不能将4
分配给string
无需显式转换它。换句话说,编译器对这两个赋值的处理方式非常不同,尽管它们在 C# 中看起来非常相似。
When concatenating the compiler turns the statement
"ham" + 4
into a call toString.Concat
, which takes twoobject
parameters, so the value4
is boxed and thenToString
is called on that.For the assignment there is no implicit conversion from
int
tostring
, and thus you cannot assign4
to astring
without explicitly converting it.In other words the two assignments are handled very differently by the compiler, despite the fact that they look very similar in C#.
参考
参考
Reference
Reference
进行串联时不存在隐式转换。 字符串连接解析为 String.Concat 调用,该调用具有接受对象的重载。 正是这个重载执行了到字符串的(显式)转换。
There is no implicit conversion when doing concatenation. String concatenation resolves down to a String.Concat call, which has an overload which takes Objects. It is this overload which performs an (explicit) conversion to string.
第一个表达式右侧的值是字符串,而第二个表达式右侧的值不是字符串。 连接在第一个场景中提供了魔力,其中作业没有做任何特殊的事情。 在第二种情况下,任务继续表现得很愚蠢。
The value of the righthand side of the first expression is a string, while the value of the righthand side of the second expression is not. The concatonation is providing the magic in the first scenario, where the assignment isn't doing anything special. In the second scenario, the assignment continues to play dumb.
表达式
根据字符串类型和加法运算符的组合强制将 4 隐式转换为字符串。 具体来说,它是“+”运算符的质量,并且在进行运算符重载时,您可以手动实现相同类型的事物。
一个类似且不太明显的示例是:
long myNumber = Int64.MaxValue - 1;
在这种情况下,“1”应被计算为 32 位整数,但它会被隐式转换。 您可以查看 C# 语言规范第 6.1 节,了解编译器支持的隐式转换的详尽列表。
编辑:需要明确的是,我提到的语言规范部分列出了编译器支持的隐式转换,而像“+”这样的运算符可以有自己支持的转换。
The expression
Forces an implicit conversion of 4 to a string based on the combination of a string type and the addition operator. Specifically it's a quality of the "+" operator, and when doing operator overloading you can manually implement the same type of thing.
A similar and less obvious example would be:
long myNumber = Int64.MaxValue - 1;
In this case "1" should be evaluated as a 32-bit integer but it is implicitly converted. You can check the C# language spec section 6.1 for an exhaustive list of implicit conversions supported by the compiler.
edit: to be clear, the language spec section i referred to lists implicit conversions supported by the compiler, while operators like "+" can have their own supported conversions.