tags:

views:

129

answers:

2

I'm using the CLR debugger (version 8, from "Visual Studio 2005") to debug some C# code. I can step through my code, but for many variables, the debugger won't show me its value. That is, if I type it in the "Immediate" frame or add it to the "Watch" frame, it says "Unable to evaluate the expression.".

It seems to do this a lot for var variables, but I'm also seeing it for parameters and properties, too. I have not been able to figure out what the pattern is.

Is there a rule for when it can display the value of a variable? Is there something I need to do to my code, or in my build script, to make the debugger able to view variables? Or is there a hidden debugger setting?

A: 

The variable must be in scope at the point at which you hit a breakpoint. That is, if you break in a method, you should be able to see parameters of that method, local variables, and static variables in the containing class.

With respect to var, that "compiler trick" was introduced in conjunction with Visual Studio 2008, so although you can use it in .NET 2.0 projects, I don't think VS2005 can compile it, so I doubt it can debug it.

Jay
`var` just tells the compiler to infer the type from the usage. There's no trace of `var` once the code is compiled, so I cannot see how this could affect debugging in any way.
Brian Rasmussen
Brian: yes, that was my thinking as well. Additionally, I'm seeing "Unable to evaluate the expression" for expressions that obviously have very explicit types, like parameters and properties. `var` was just an idea, since they seemed to be the most common case I was seeing.
Ken
+2  A: 

There's some crucial info missing from your question. Reverse-engineering: the "var" keyword didn't become available until C# version 3, shipped with Visual Studio 2008. Using an old debugger isn't a great idea, although it probably isn't the real problem.

Another hint is that you use a stand-alone debugger instead of the one that's built into Visual Studio. Making it very likely that you are debugging release code, not code built in the Debug configuration. Not getting info on local variables and properties is quite normal in that case, the JIT compiler optimizes them away.

Hans Passant