Hello,
I'm playing around with recursive loops in order to better understand how to iterate though directories. I've used the .Net methods 'GetDirectories("*.*", SearchOption.AllDirectories)' in 'foreach' loops to iterate through the file system but many examples i've seen use recursion techniques to do the same thing. So knowing nothing of recursion I wrote the following method which should simply count to 10 to see how it works.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
static int value;
static int total;
private void Form1_Load(object sender, EventArgs e)
{
value = 1;
total = Recursion(value);
textBox1.Text += "The Total = " + total;
}
static int Recursion(int x)
{
if (x <= 10)
{
x++;
Recursion(x);
}
return x;
}
}
I assumed that the method 'Recursion(int x)' would return the value '10' to 'total' but it always returns a value of '2'. I stepped through with the debugger in VS2008 express and all goes well until 'x=10' but the method continues to loop and actually counts down before returning '2'.
Can anyone please explain what i'm doing wrong here as i've spent several hours scratching my head over this one.
Cheers
SteveH