What is wrong with this code:
using System;
namespace app1
{
static class Program
{
static int x = 0;
static void Main()
{
fn1();
}
static void fn1()
{
Console.WriteLine(x++);
fn1();
}
}
}
I compile this piece of code using this command:
csc /warn:0 /out:app4noex.exe app4.cs
When I double click on the exe, it doesn't seem to throw the exception (StackOverFlowException), and keep running forever.
Using visual studio command prompt 2010, but I also have vs 2012 installed on the system, all up to date.
Because the optimizer unrolls the tail recursion call into:
static void fn1()
{
START:
Console.WriteLine(x++);
GOTO START;
}
Rewrite to get exceptions like so:
static int y;
static void fn1()
{
Console.WriteLine(x++);
fn1();
Console.WriteLine(y++);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With