C# Tricks – Evaluate “1”+2+4 and 2+5+”8″

What does “1”+2+4  evaluate to?

Wait! Wait! do not get in to conclusion that’s it’s an aptitude, number series question. It actually a question about the result of this operation, if you consider in C#.

“1” a string is added with 2 and 4 which are integers. So what would be the result if you include it in a console writeline code.

 

Console.WriteLine("1" + 2 + 4);

Guess what would be the result. 124. “1” is a string. So anything following by a string will be considered as a string. so 2 and 4 are considered as string even though they are actually integer.

ANS: 124

What does 2+5+”8″ evaluate to?

Guess? It would be 78, because after 2+5 operation we are adding string “8” to it. So C# will convert the result of 2+5 in to string and will add it with “8”. So the 2+5 = 7 => “7” + “8” = 78

ANS: 78

Tricky! right. We will think it is a number series questions and will go for evaluate the series, ha ha ha. This a concept, something every C# programmer should know. So in some interview it’s been asked. do not get confused.
Happy Coding!!!!