0

C# basic concept problem

int a=4; int b =6; b = a++ console.write(++b) what will be the output??? and why??

26th Nov 2019, 11:41 AM
SR Shoruv
SR Shoruv - avatar
3 Réponses
+ 3
It will be 5. It is the position of the ++ that makes thr difference. using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SoloLearn { class Program { static void Main(string[] args) { int a = 4; // a is now 4 int b = 6; // b is now 6 b = a++; // b first gets the value of a, and will be 4, and after that, a increases to 5 Console.Write(++b); // b increases by 1 before it is printed, so it will be 5 } } }
26th Nov 2019, 1:08 PM
Paul
Paul - avatar
+ 3
`b=a++;` is equivalent to `b=a; a++;`
26th Nov 2019, 1:14 PM
o.gak
o.gak - avatar
0
I found some detail information here: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/expressions#prefix-increment-and-decrement-operators > The result of the operation is a value of the same type as the operand.
26th Nov 2019, 5:15 PM
o.gak
o.gak - avatar