Wouter Schut
Wouter Schut

Reputation: 927

Visual Studio debugger shows wrong value (null where there should be a value)

I'm stuck, why does this happen? The code literally says that entity != null but the debugger thinks it's null. What is going on?

visual studio screenshot - debugging

I already changed to x86, recompiled, cleaned, even restarted the pc. And the code was always like this, there is no version where the if statement was different. And if I continue I don't get a null-pointer exception.

Upvotes: 15

Views: 3308

Answers (2)

Artur Krajewski
Artur Krajewski

Reputation: 589

I experienced the same problem when there was variable with same name declared earlier in the method. In that case debugger would only 'notice' first variable. It has no impact on actual code execution.

Upvotes: 6

Xaltos
Xaltos

Reputation: 29

I am not sure, but I think it is an effect of the foreach loop in combination with the ChangeTracker.Entries() function.

The foreach function is collection some data on demand. So it is not getting all the data from ChangeTracker.Entries() and afterwards starting the loop with each value.

It is more getting the first value from the function --> execution the value --> getting second value --> ...

So the value is null, because you haven't used the object yet.

Change your ChangeTracker.Entries() into ChangeTracker.Entries().ToList() and I think you will see a change.

Take a look into the yield command for an deeper understanding how the iterator function works. (The logic is a bit confusing :) )

Upvotes: -1

Related Questions