class Program
{
public void test(int i) {
if (i > 0)
{
test(--i);
}
Console.WriteLine(i);
}
static void Main(string[] args)
{
Program p = new Program();
p.test(5);
Console.ReadKey();
}
}
The output is 0 0 1 2 3 4
I dont quite understand.
I thought that the Console.WriteLine(i); wont execute until i <= 0
and why is that the 0 comes first?
Correct me if im wrong im just new to programming.
Is it because that when a function is called is uses the stack?
so the while the condition is true it keeps calling its self and the first output was actually 4 (because of the pre --yy) but the instruction was on the lowest part of the stack thats why it came out last.
4 3 2 1 0 0 then pops out like 0 0 1 2 3 4
Is that how stack work?
is there a threading happening here?