Why: string st = "" + 12; work in c # without conversion?

This is super dumb, but I checked the links and checked the links and I just can't find the answer ... Why int or float etc. can be added as part of a line without conversion, but not on it? i.e:

so far this work is wonderful:

 string st = "" + 12;

this is not (of course):

 string st = 12;

Where is the magic here? I know this works. I just want to know WHY this works, and how I control HOW the conversion is done?

+3
source share
7 answers

+ , + . , . ( ToString()), .

, int string.

, , () .

+4

, BoltClock , + . # , :

, ToString, object.

+1

String concats .NET String.Concat . , , mutliple concatenations.

, , String.Concat object , int, float .. , Concat, object. Internally Concat .ToString() -, int .

string st = "" + 12;

, String.Concat(object). 12 st.

, , , , .

+1

.ToString() , - .

0

- + .

0

# .

- , .

, , , , toSting.

0

, String.Concat().

Inside, all operands are placed in a box (if necessary) and passed to the method String.Concat(which, of course, calls ToString()for all arguments).

0
source

Source: https://habr.com/ru/post/1781238/


All Articles