What is the logic here(increment/decrement)?

Question: int a = b++; int b = 4; Console.Write(a); Does this give an error because int b isn't defined before int a? Because, otherwise, I think a equals 4 (unless the compiler works like "you can't assign a value to a variable through another variable unless the latter defined before the former") For example, does it work only when we write it like this? int a; int b = 4; int a = b++; Console.Write(a);

8/27/2019 3:44:40 PM


4 Answers

New Answer


okay, in and of itself a very good idea to learn the logic behind things :) but running the code yourself is very valuable, because the errors given will teach you what is wrong that said: yes, your first example will throw an error. you can’t use an int before declaring/defining it. otherwise in your second example you will get 4 as a result the increment operator in b++ will return the variable first and then increment it. print it again and you will get 5 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 b = 4; int a = b++; Console.WriteLine(a); Console.WriteLine(b); } } }


have you run it through the playground?


Brave Tea no, it came up as a question in the Challange, and I wanted to learn the logic behind it :)


Brave Tea Thanks for the kind advice and explanation :)