I saw an article that explained why this happens, trying to find...does anyone know what I'm talking about:
Worse case Output:
0
2
2
4
4
6
6
8
8
10
Let me stress that this isn't an exact output everytime - it's worse case...but make the loop big enough and let it run enough times and you'll see it.
What I'm getting at is that occasionally you see these 'variable ghosts' when you code like this. I know not to do this because I've run into it before, but yesterday I had a co-worker who ran into a similiar variable where at the class level he had a boolean value that he wasn't explicitly setting to false when declaring it. This was on a asp code behind. This resulted in occasionally the variable still being set to true from a (recent) previous call to the page. He couldn't understand why this is happening; I know it has something to do with GC and finalization que, but I can't find the article for the life of me and he really wants to understand why this is happening, so if you know where it is or can give a better explanation that what I just did, let me know.
Thanks
C#:
int index = 1;
while(index < 10)
{
int i;
if(index % 2)
{
i = index;
}
Console.WriteLine(i);
index ++;
}
Worse case Output:
0
2
2
4
4
6
6
8
8
10
Let me stress that this isn't an exact output everytime - it's worse case...but make the loop big enough and let it run enough times and you'll see it.
What I'm getting at is that occasionally you see these 'variable ghosts' when you code like this. I know not to do this because I've run into it before, but yesterday I had a co-worker who ran into a similiar variable where at the class level he had a boolean value that he wasn't explicitly setting to false when declaring it. This was on a asp code behind. This resulted in occasionally the variable still being set to true from a (recent) previous call to the page. He couldn't understand why this is happening; I know it has something to do with GC and finalization que, but I can't find the article for the life of me and he really wants to understand why this is happening, so if you know where it is or can give a better explanation that what I just did, let me know.
Thanks