简体   繁体   中英

Setting property in Immediate Window not setting?

I have a problem in a project where I put a break point and start working with the properties of an object, and I can't set a value to a nullable int. I can set it to null, but if I set it to anything then immediately check the value, it returns 1.

I'm seeing this in the immediate window as well as the inspection window that pops up when you hover over a object when paused in debug mode. It also appears that this doesn't affect running code, just when you break and try to play with the values.

I was able to reproduce it in a new console project:

class Program
  {
    static void Main(string[] args)
    {
      var myValue = new TestItem()
      {
        NullableIntValue = null,
        StringValue = "My test value"
      };

      Console.WriteLine(string.Format("{0} is set to {1}", myValue.StringValue, myValue.NullableIntValue.HasValue ? myValue.NullableIntValue.Value.ToString() : "nothing"));

      myValue.NullableIntValue = 0;

      Console.WriteLine(string.Format("{0} is set to {1}", myValue.StringValue, myValue.NullableIntValue.Value));

      Console.ReadLine();

    }
  }

  public class TestItem
  {
    public int? NullableIntValue { get; set; }
    public string StringValue { get; set; }
  }

Paste that into a new console project, and put a break point at the "myValue.NullableIntValue = 0;" line. Then run it.

When the breakpoint hits, open the immediate window and set the value:

myValue.NullableIntValue = 123;
123

Then check the value:

?myValue.NullableIntValue
1

Try it with something else:

myValue.NullableIntValue = null
null
?myValue.NullableIntValue
null

Set it to anything other than null and it seems to always be 1.

You can also hover over the breakpoint line "myValue" and let VS pop open the inspection window where you see all the values. Go to NullableIntValue and set it to 100 (or anything) and hit enter and it immediately changes it to 1.

This only seems to behave this way for a property, ie if I change TestItem.NullableIntValue to a variable instead of a property it works fine. And if I assign a value to the NullableIntValue in code and let it run it seems to take it no problem, just not during a break in debug.

Note, also tried this in VS 2013 and it takes the assignment just fine, so it seems limited to VS 2015, and I tried both C# 6 and C# 5, happens on both.

Any ideas what could be causing this?

This is a known bug in Visual Studio 2015 RTM. We fixed it in Visual Studio Update 1. Essentially marshalling complex value types (like nullables) from the debugger to debuggee process was broken. If you don't want to install Update 1, you can work around it by going to Debug -> Options -> General and checking "Use the legacy C# and VB Expression Evaluators".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM