0
C# basic concept problem
int a=4; int b =6; b = a++ console.write(++b) what will be the output??? and why??
3 odpowiedzi
+ 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
}
}
}
+ 3
`b=a++;`
is equivalent to
`b=a; a++;`
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.