字符串和整数,隐式和显式

发布于 2024-07-12 12:30:46 字数 267 浏览 7 评论 0原文

有一位同事问我这个问题,在我的大脑困惑的状态下,我没有答案:

为什么你可以这样做:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

暮倦 2024-07-19 12:30:46

连接时,编译器会将语句 "ham" + 4 转换为对 String.Concat 的调用,该调用采用两个 object 参数,因此值4 被装箱,然后调用 ToString

对于赋值,没有从 intstring 的隐式转换,因此您不能将 4 分配给 string无需显式转换它。

换句话说,编译器对这两个赋值的处理方式非常不同,尽管它们在 C# 中看起来非常相似。

When concatenating the compiler turns the statement "ham" + 4 into a call to String.Concat, which takes two object parameters, so the value 4 is boxed and then ToString is called on that.

For the assignment there is no implicit conversion from int to string, and thus you cannot assign 4 to a string 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#.

蓝颜夕 2024-07-19 12:30:46

二元 + 运算符是预定义的
数字和字符串类型。 对于数字
类型,+ 计算其两个之和
操作数。 当一个或两个操作数
是字符串类型,+ 连接
的字符串表示
操作数。

参考

赋值运算符 (=) 存储
其右侧操作数的值
存储位置、属性或索引器
由其左侧操作数表示
返回值作为其结果。 这
操作数必须是同一类型(或
右侧操作数必须是
隐式转换为类型
左侧操作数)。

参考

Binary + operators are predefined for
numeric and string types. For numeric
types, + computes the sum of its two
operands. When one or both operands
are of type string, + concatenates the
string representations of the
operands.

Reference

The assignment operator (=) stores the
value of its right-hand operand in the
storage location, property, or indexer
denoted by its left-hand operand and
returns the value as its result. The
operands must be of the same type (or
the right-hand operand must be
implicitly convertible to the type of
the left-hand operand).

Reference

佞臣 2024-07-19 12:30:46

进行串联时不存在隐式转换。 字符串连接解析为 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.

最偏执的依靠 2024-07-19 12:30:46

第一个表达式右侧的值是字符串,而第二个表达式右侧的值不是字符串。 连接在第一个场景中提供了魔力,其中作业没有做任何特殊的事情。 在第二种情况下,任务继续表现得很愚蠢。

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.

书信已泛黄 2024-07-19 12:30:46

表达式

"ham " + 4 

根据字符串类型和加法运算符的组合强制将 4 隐式转换为字符串。 具体来说,它是“+”运算符的质量,并且在进行运算符重载时,您可以手动实现相同类型的事物。

一个类似且不太明显的示例是:

long myNumber = Int64.MaxValue - 1;

在这种情况下,“1”应被计算为 32 位整数,但它会被隐式转换。 您可以查看 C# 语言规范第 6.1 节,了解编译器支持的隐式转换的详尽列表。

编辑:需要明确的是,我提到的语言规范部分列出了编译器支持的隐式转换,而像“+”这样的运算符可以有自己支持的转换。

The expression

"ham " + 4 

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文