C# How can false == true ? See Picture
I just ran into one of the most mind boggling errors ever. false == true What information would you guys need to confirm/debug this behavior? I've never seen anything l开发者_如何学JAVAike it.
- VS2008 sp1
- Debug Mode | Any Cpu
- IIS 7.5
Edit: I did a clean->rebuild and still the same.
Here's the assembly and registers. I don't know how to read this, but maybe it could help someone else.
I suppose your PDB files are not in phase and you have differences in what's really executed and what Visual Studio sees as a line number. Try rebuilding. We all know that it is impossible to have true = false
, or the world as we know it may change :-)
Does it actually throw the error? The debugger can often highlight the wrong lines if you feed it the wrong pdb, so this could be a false lead. It is also trivial to reproduce using the "immediate" pane to change the value after the test.
If result
was a field or a captured variable, it could also be set by external code (perhaps on another thread).
If result
wasn't a bool
but your own custom type, you could just override ==
, or provide a custom true
/false
operator.
Probably the source does not correspond to the running version or there's a bug in the debugger.
Part of the problem is that you're assuming the debugger is 100% correct. It in fact is not and is subject to a number of situations where values can have incorrect or misleading displays. The most common causes of this are ...
- Mismatched PDB files. This will usually lead to at least a warning dialog in the debugger about mismatched source files but not always
- Simple data inspection or display error by the underlying expression evaluator. Not likely in this case as it's a simple local and a primitive type.
- Optimizations causing the data to be displayed incorrectly.
But it in fact is almost certainly not false. The easiest way to verify this is to use a Debug.WriteLine
call to print the value out to the output window.
Are you sure that's the exception being thrown? My hunch is that your method isContextSignatureValid is actually throwing an exception, but the Visual Studio debugger can get ahead of itself sometimes and highlight a line that is not actually throwing the exception.
Maybe you moved the current instruction pointer (yellow arrow) with your mouse inadvertently while in break mode... It happened to me once and I flipped out. :-)
Just to add a little suggestion:
If you ever get confusing results from the debugger, stick a Console.WriteLine() in there and get the code itself to tell you what is going on. This can often clear up confusion.
(You can also get an effect like this when debugging release code, but you said it was a debug build which eliminates that suspect)
I've seen this sort of thing before. A co-worker was convinced he'd uncovered a bug in the .Net Framework or CLR. In the end it was just an old assembly or pdb synch problem.
精彩评论