Search code examples
c#visual-studio-2013

C# visual studio 2013 premium debugging issue


During a debug session I am not able to get at variables that do not have any tangible assignment.

So for example:

public void SomeMethod()
{
      string TheString = "Hello World";
      Thread.sleep(1000); // Breakpoint here
} 

In the above method, if I set the breakpoint just after TheString gets its value assigned, and now since TheString is never actually used in any execution statement. During design time the variable is gray, and when you hover over it it says "Local variable TheString is never used"

All of that is fine, but during a debug session you are unable to get any reference to TheString via the immediate Window. It is like the variable does not even exist.

So for example:

?TheString The name 'TheString' does not exist in the current context

Now what does work is this:

 public void SomeMethod()
    {
          string TheString = "Hello World";
          Thread.sleep(1000); // Breakpoint here
          MessageBox.Show(TheString); 
    } 

It works because the value is used in an execution path at run time. At this stage I'm not sure if this is a new feature of Visual Studio 2013? I want regular debugging back if possible.


Solution

  • You are probably building in Release mode.

    When building in Release mode, the compiler does some code optimizations. One of them is removing variables that are never used.