public class ModelInfo
{
public int AssignedCount { get; set; }
public int UnassignedCount { get; set; }
public int TotalCount { get { return UnassignedCount + TotalCount; } }
}
*Edit:* I realized when I put this code in SO that the TotalCount property is adding the UnassignedCount + the TotalCount (I meant to add the other two counts together). Can someone please provide a throughough explanation on why the SO error occurs though? I mean, the low-level stuff.
Stackoverflowing!
you are in an infinite recursion, a property calling itself
There is a mechanism called the "stack," used to track nested calls. When you call a method or function, the current "stack frame" (the address that control will transfer to when the method you called returns, as well as any parameters and method locals in your method) are pushed onto the stack. When control returns to your method, this stack frame is popped off and the CPU registers are restored to their prior state so that your method can continue executing.
The stack is a fixed allocation of memory, and therefore you can only call so many levels deep before you run out of places to store the information that is necessary to restore the state of the CPU registers when your function is returned to. At this point, a stack overflow error happens because, well, you overflowed the stack. You made so many nested calls that you ran out of memory to record them in.
That's why the exception happens. You recurse infinitely: your property getter calls itself over and over and over until there is no more room on the stack.
Sure: in order to calculate
TotalCount
, the compiler generates code like this:UnassignedCount
TotalCount
UnassignedCount
toTotalCount
When calling the property getter of
TotalCount
(remember, a getter is a regular no-argument function that uses a special syntax), the runtime places the return address on the stack. The second step finds us back at the beginning, with one extra return address on the stack; the third step does it again, then the forth, the fifth, and so on. Each invocation deposits another return address on the stack. This continues all the way to the limit of the stack, at which point an exception is thrown.Because the call stack would get (theoretically) infinitely deep. And you don't have an infinite stack to keep all the data you would need.
Because you are referring to
TotalCount
within theTotalCount
getter. An infinite loop occurs until a stack overflow is reached.You should be able to write this: You should note that TotalCount can never be set to so it would have no value. Did you perhaps mean to do this: