Quantcast
Channel: why can't a local variable be volatile in C#? - Stack Overflow
Viewing all articles
Browse latest Browse all 5

why can't a local variable be volatile in C#?

$
0
0
public void MyTest(){  bool eventFinished = false;  myEventRaiser.OnEvent += delegate { doStuff(); eventFinished = true; };  myEventRaiser.RaiseEventInSeperateThread()  while(!eventFinished) Thread.Sleep(1);  Assert.That(stuff);}

Why can't eventFinished be volatile and does it matter?

It would seem to me that in this case the compiler or runtime could become to smart for its own good and 'know' in the while loop that eventFinished can only be false. Especially when you consider the way a lifted variable gets generated as a member of a class and the delegate as a method of that same class and thereby depriving optimizations of the fact that eventFinished was once a local variable.


Viewing all articles
Browse latest Browse all 5

Latest Images

Trending Articles



Latest Images