Implicit string cast when attempting to add int to string #5667
-
I was demonstrating static typing to a friend who is trying to learn C#. When I tried to add a string to an integer, it seems to implicitly cast the int to a string and perform a string concatenation: Was this added to C# recently, or is it a bug? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
Yes. Though 'recent' only relative to the age of the universe. This functionality was available in C#1.0
It is not a bug. (Practically) all types in .Net extend from
Note: C# is statically typed. And in this scenario static typing is in effect. Static typing does not mean that strings cannot be concatenated to non-string values. |
Beta Was this translation helpful? Give feedback.
-
The |
Beta Was this translation helpful? Give feedback.
Yes. Though 'recent' only relative to the age of the universe. This functionality was available in C#1.0
It is not a bug. (Practically) all types in .Net extend from
System.Object
and thus have the ability to convert themselves to strings with.ToString()
. String concatenation is thus supported in C# for all of these types through the use of combining the actual strings, and the stringified values into one final string.Note: C# is statically typed. And in this scenario static typing is in effect. Static typing does not mean that strings cannot be concatenated to no…