字符串“+”运营商转换 [英] String "+" operator conversion

查看:79
本文介绍了字符串“+”运营商转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很惊讶地发现当与+连接时,c#会自动将整数转换为

字符串。运营商。我认为c#应该对类型非常严格。

。难道似乎c#打破了自己的

规则吗?


这个有效:

int b = 32;

string a =" ABC" + b;


结果:a =ABC 32


但此行会导致错误:

a = b;

I am surprised to discover that c# automatically converts an integer to a
string when concatenating with the "+" operator. I thought c# was supposed
to be very strict about types. Doesn''t it seem like c# is breaking its own
rules?

This works:
int b = 32;
string a = "ABC " + b;

Result: a = "ABC 32"

but this line will cause an error:
a = b;

推荐答案

" Elena" < El *** @ discussion.microsoft.comha scritto nel messaggio

news:9E ************************ ********** @ microsof t.com ...
"Elena" <El***@discussions.microsoft.comha scritto nel messaggio
news:9E**********************************@microsof t.com...

>我很惊讶地发现c#会自动将整数转换为<当与+连接时,
字符串运营商。我认为c#是

假设

对类型非常严格。难道似乎c#打破它的

自己

规则?


这样有效:

int b = 32;

string a =" ABC" + b;


结果:a =ABC 32


但此行会导致错误:

a = b;
>I am surprised to discover that c# automatically converts an integer to a
string when concatenating with the "+" operator. I thought c# was
supposed
to be very strict about types. Doesn''t it seem like c# is breaking its
own
rules?

This works:
int b = 32;
string a = "ABC " + b;

Result: a = "ABC 32"

but this line will cause an error:
a = b;



mmm ...我看了一下IL产生的结果。

C#编译器连接字符串调用字符串。 Concat()。

由于没有string.Concat(string,int),被调用的重载是

string.Concat(object,object)。

如果你看看里面(我使用了Reflector)你可以看到


return(arg0.ToString()+ arg1.ToString());


所以这是发现的秘密:)

mmm... I took a look to the IL produced.
C# compiler to concatenate strings call string.Concat().
Since there is not string.Concat(string, int) the called overload is
string.Concat(object, object).
If you look inside it (I used Reflector) you can see

return (arg0.ToString() + arg1.ToString());

So this is the discovered secret :)


Fabio,


这就是为什么编译器决定如此。

如果1 + 1,则确保它不是字符串连接。或new Object()+

1"。

如果一个或多个操作数是字符串,则为字符串连接。

这是适用于C#和Java。


Thi
http://thith.blogspot.com


Fabio写道:
Fabio,

The thing is why the compiler decide so.
Sure that it is not string concatenation if "1 + 1" or "new Object() +
1".
If one or more operands are string, it is string concatenation.
This is true for both C# and Java.

Thi
http://thith.blogspot.com

Fabio wrote:

C#编译器连接字符串调用string.Concat()。

由于没有string.Concat(string,int),被调用的重载是

string.Concat(object,object)。

如果你看里面(我使用Reflector)你可以看到


return(arg0.ToString()+ arg1.ToString());


所以这是发现的秘密:)
C# compiler to concatenate strings call string.Concat().
Since there is not string.Concat(string, int) the called overload is
string.Concat(object, object).
If you look inside it (I used Reflector) you can see

return (arg0.ToString() + arg1.ToString());

So this is the discovered secret :)


你好!

如果一个或多个操作数是字符串,则为字符串连接。
If one or more operands are string, it is string concatenation.



正确。此行为也在C#语言规范中指定。


来自C#语言规范1.2,§7.7.4,加法运算符:


----------------

"字符串连接:二元+运算符执行字符串连接

当一个或两个操作数都是类型字符串。如果字符串

连接的操作数为null,则替换空字符串。否则,通过从类型对象继承的虚拟ToString方法调用

,将任何

非字符串参数转换为其字符串表示形式。如果ToString返回

null,则替换空字符串。 [...] System.OutOfMemoryException
如果没有足够的可用内存来分配

结果字符串,可能会抛出


----------------


规格如下:

http://msdn2.microsoft.com/en-us/library/ms228593.aspx


-

问候,


Jani J?rvinen先生

C#MVP

芬兰赫尔辛基
ja *** @ removethis.dystopia.fi
http://www.saunalahti.fi/janij/

Correct. This behavior is also specified in the C# language specification.

From the C# Language Specification 1.2, §7.7.4, Addition operator:

----------------
"String concatenation: The binary + operator performs string concatenation
when one or both operands are of type string. If an operand of string
concatenation is null, an empty string is substituted. Otherwise, any
non-string argument is converted to its string representation by invoking
the virtual ToString method inherited from type object. If ToString returns
null, an empty string is substituted. [...] A System.OutOfMemoryException
may be thrown if there is not enough memory available to allocate the
resulting string."
----------------

The specification is available here:

http://msdn2.microsoft.com/en-us/library/ms228593.aspx

--
Regards,

Mr. Jani J?rvinen
C# MVP
Helsinki, Finland
ja***@removethis.dystopia.fi
http://www.saunalahti.fi/janij/


这篇关于字符串“+”运营商转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆