为什么我不需要的ToString()方法MessgeBox.Show内() [英] Why I don't need ToString() method inside a MessgeBox.Show ()
问题描述
我只注意到这个工程太:
I just noticed this works too:
MessageBox.Show("number of things in the report are " + myHashSetVariable.Count);
我是,我应该使用myHashSetVariable.Count.ToString()
I was under the impression that I should use myHashSetVariable.Count.ToString()
它是某种在VS2010编译器/解释改进措施呢?我使用VS2010临
Is it some sort of Compiler/Interpreter improvments in VS2010? I am using VS2010 Pro
推荐答案
首先,这无关与 MessageBox.Show
。这个拥有一切做的 +
运营商。字符串+对象的结果等于一个字符串。
First off, this has nothing to do with MessageBox.Show
. This has everything to do with the +
operator. The result of string + object equals a string.
有在语言中的许多重载的+运算符(你也可以添加自己的用户定义类型)。只有两个接受一个对象
作为一个参数,即运营商+(字符串对象)
和运算符+(对象,字符串)
。在这两种情况下,运营商执行的身体会调用的ToString
的对象
参数,然后使用 string.Concat
来产生结果。
There are a number of overloads to the + operator in the language (and you can also add your own for user defined types). There are only two that takes an object
as a parameter, that is operator+(string, object)
and operator+(object, string)
. In both cases the body of the implementation of the operator will call ToString
on the object
parameter and then use string.Concat
to produce the result.
由于您的变量是一个整数,它使用运营商+
与字符串
作为第一个参数,它将匹配运营商+(字符串对象)
的无其他候选人。
Since your variable is an integer and it is using operator+
with string
as the first parameter it will match operator+(string, object)
an no other candidates.
这篇关于为什么我不需要的ToString()方法MessgeBox.Show内()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!