You're getting a stack overflow because of this section:
public int val
{
get
{
return val == 10 ? 1 : 0;
}
set
{
_val = value;
}
}
Your
get
method is referencing the property name and not the private variable name. This means when you get the value of
val
, your code goes into a never ending loop as the result references itself. In .Net this will eventually cause a stack overflow exception.