我刚才注意到这个也有效:
MessageBox.Show("number of things in the report are " + myHashSetVariable.Count);
Run Code Online (Sandbox Code Playgroud)
我的印象是我应该使用myHashSetVariable.Count.ToString()
它是VS2010中的某种编译器/解释器改进吗?我正在使用VS2010 Pro
首先,这与此无关MessageBox.Show.这与+操作员有关.string + object的结果等于一个字符串.
语言中的+运算符有许多重载(您也可以为用户定义的类型添加自己的重载).只有两个object作为参数,即operator+(string, object)和operator+(object, string).在这两种情况下,运营商的执行机构将调用ToString的object参数,然后用string.Concat产生的结果.
由于您的变量是一个整数,并且它使用operator+with string作为第一个参数,它将匹配operator+(string, object)其他候选项.