<br><br><div class="gmail_quote">2009/9/22 Masklinn <span dir="ltr"><<a href="mailto:masklinn@masklinn.net">masklinn@masklinn.net</a>></span><br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<div class="im">On 22 Sep 2009, at 22:37 , Michael Foord wrote:<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<br>
C# will not allow you to add strings to numbers, this is nonsense.<br>
</blockquote></div>
I fear you're wrong. In C#, the operation `"foo " + 3` is perfectly<br>
legal and returns `"foo 3"` (and the integer can, of course, be the<br>
left hand of the operator). No need for a ToString, that's implicit.<br>
<br></blockquote><div><br>Heh, you're right. How very odd.<br><br>Michael<br> </div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
In fact, this is quite clearly specified in the C# language<br>
specification §7.4.4. "The addition operator"<br>
(<a href="http://msdn.microsoft.com/en-us/library/aa691375.aspx" target="_blank">http://msdn.microsoft.com/en-us/library/aa691375.aspx</a>):<br>
<br>
> The binary + operator performs string concatenation when one or both<br>
> operands are of type string. If an operand of string concatenation is<br>
> null, an empty string is substituted. Otherwise, any non-string<br>
> argument is converted to its string representation by invoking the<br>
> virtual ToString method inherited from type object. If ToString<br>
> returns null, an empty string is substituted.<br>
><br>
>     using System;<br>
>     class Test<br>
>     {<br>
>        static void Main() {<br>
>           string s = null;<br>
>           Console.WriteLine("s = >" + s + "<"); // displays s = ><<br>
>           int i = 1;<br>
>           Console.WriteLine("i = " + i);        // displays i = 1<br>
>           float f = 1.2300E+15F;<br>
>           Console.WriteLine("f = " + f);        // displays f = 1.23+E15<br>
>           decimal d = 2.900m;<br>
>           Console.WriteLine("d = " + d);        // displays d = 2.900<div class="im"><br>
>        }<br>
>     }<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
If you<br>
call ToString() then this is an explicit operation to produce a string<br>
representation - and it is this string that you add. It is *not* at all the<br>
same as adding arbitrary objects on strings. You can't compile a C# program<br>
that attempts to add strings and numbers.<br>
</blockquote></div>
You might have wanted to test out this theory.<div class="im"><br>
<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
I'm also pretty sure you can't override the default behavior on integers etc<br>
(C# does have operator overloading but doesn't allow you to redefine<br>
operators on the fundamental types - Java doesn't even have operator<br>
overloading).<br>
</blockquote></div>
Java somewhat does, since "+" is overloaded on String. What Java doesn't<br>
provide is user-defined operator overloading.<div><div></div><div class="h5"><br>
<br>
_______________________________________________<br>
Python-ideas mailing list<br>
<a href="mailto:Python-ideas@python.org" target="_blank">Python-ideas@python.org</a><br>
<a href="http://mail.python.org/mailman/listinfo/python-ideas" target="_blank">http://mail.python.org/mailman/listinfo/python-ideas</a><br>
</div></div></blockquote></div><br><br clear="all"><br>-- <br><a href="http://www.ironpythoninaction.com/">http://www.ironpythoninaction.com/</a><br><br><br>