Is there a way in C# to have a method retain a variable across different calls? For example:
private void foo()
{
int j; //declare this so as it isn't destroyed with the stack frame.
int i = calculateSomeValue() + j;
j = i;
}
The normal way I would do this would be with a member variable like this:
private int j = 0;
private void foo()
{
int i = calculateSomeValue() + j;
j = i;
}
I don't like how the variable j can be accessed in all of the other methods of the class. Plus it just seems messy this way: defining a member variable when it will only be used in one method to keep track of the value of i when the method was last called.
Is this possible in a nice/clean way? Or should I just use the member variable way and forget about it?