I have declared an int e.g. int i;
I have a method that is looped:
public static void NumberUp(int i)
{
i++;
System.Console.WriteLine(i);
...
}
although each time the number is returned, it is always 1. Not 1,2,3 and so on..
I would have imagined that “i” increases by one which each run of the method?
You can pass the variable by reference.
This, however is bad design, as you now have a method with a side effect on the passed in parameter (which the method name doesn’t indicate) – something that can catch other programmers by surprise.
It would be a better design to return the incremented value: