0

I have the watches with the values written below and a property with the accessors written below. Although the values are boxed strings and their types are the same, and the actual text in the strings is exactly the same, the condition d.Value.Value != d.Value.DefaultValue is true.

I have put the watches below and I was simply surprised by their values.

internal object DefaultValue { get; set; } = null;

internal object _Value = null;
internal object Value
{
    get
    {
        return _Value;
    }
    set
    {
        if (_Value != value)
        {
            _Value = value;
            Changed?.Invoke(this, new SettingValueChangedEventArgs()
            {
                IsInitialization = FirstChangeIsInitialization
            });
        }
    }
}

Watches values when the breakpoint on _Value != value was hit:

  1. d.Value.Value != d.Value.DefaultValue : true
  2. d.Value.Value : "None" (object {string})
  3. d.Value.DefaultValue : "None" (object {string})
  4. "test" : "test" (string)
  5. ((object)"test").GetType() : object {string}
  6. d.Value.Value.GetType() : object {string}
  7. d.Value.DefaultValue.GetType() : object {string}
  8. d.Value.DefaultValue.Equals(d.Value.Value) : true

I expected that d.Value.Value != d.Value.DefaultValue will not be equal to d.Value.DefaultValue.Equals(d.Value.Value), but they are the same.

silviubogan
  • 3,343
  • 3
  • 31
  • 57

1 Answers1

1

My working code:

internal object _Value = null;
internal object Value
{
    get
    {
        return _Value;
    }
    set
    {
        if ((value == null && _Value != null) ||
            (value != null && _Value == null) ||
            (value != null && _Value != null && !_Value.Equals(value)))
        {
            _Value = value;
            Changed?.Invoke(this, new SettingValueChangedEventArgs()
            {
                IsInitialization = FirstChangeIsInitialization
            });
        }
    }
}
silviubogan
  • 3,343
  • 3
  • 31
  • 57
  • Obviously that works, however you could use the static method `object.Equals(_Value, value)` to get the same result with fewer of the null checks. – Mike Zboray Apr 30 '19 at 05:14