Variable does not exist in the current context while debugging

It's possible the local variables have been optimised away by the JIT compiler. Since you're using Visual Studio you might be able to switch the configuration to Debug and rebuild.

If not, you can configure the JIT compiler to disable optimisations and generate tracking information - see here on how to set the configuration. This should allow you to see local variable when you attach the debugger to the process.


I've encountered another scenario in VS2012 that causes variables to "disappear" while in debug mode:

make sure you don't have this:

if(false)
   {
   .
   }
else
   {
   //Code here will be optimized and variables will not be available.
   }

If you are trying to debug in a release build (release mode instead of debug mode), you'll get this error. Change your solution configuration to Debug (Any CPU) and you'll be able to see variable values in the immediate window.