The + operator has different overloads:
int + int = int
int + string = string
string + int = string
In the following expression:
string str = 30 + 20 + 10 + "ddd"; Console.WriteLine(str);
The first 30 + 20 received the estimates as integers, so the operator output will be an integer of 50 .
Then 50 + 10 will be evaluated, which are both integers again, so an integer that is 60 will be displayed.
Then 60 + "ddd" , which is an integer + string , the operator in this case displays a string, so 60 + "ddd" will output 60ddd .
In the following expression:
string str = "ddd" + 30 + 20 + 10; Console.WriteLine(str);
The first "ddd" + 30 received grades in which the string + integer operation is performed, so the output will be ddd30 .
Then ddd30 + 20 will be evaluated, in which the string + integer operation is performed again, so the output will be ddd3020 .
Then ddd3020 + 10 will be evaluated, in which the string + integer operation is performed again, so the output will be ddd302010 .
Muhammad ramzan
source share