2

I was confronted with a StackOverflowException that made me discover that DependencyObject does not handle equality correctly ?!

When the DependencyProperty is of Type Object it will allways use Reference.Equals. This causes to fire PropertyChanged everytime for strings and valuetypes when the same value gets applied.

If you take a look at

DependencyObject.Equals(DependencyProperty dp, object value1, object value2)

https://referencesource.microsoft.com/#WindowsBase/Base/System/Windows/DependencyObject.cs,3453

/// <summary>
///     Helper method to compare two DP values
/// </summary>
private bool Equals(DependencyProperty dp, object value1, object value2)
{
    if (dp.IsValueType || dp.IsStringType)
    {
        // Use Object.Equals for Strings and ValueTypes
        return Object.Equals(value1, value2);
    }
    else
    {
        // Use Object.ReferenceEquals for all other ReferenceTypes
        return Object.ReferenceEquals(value1, value2);
    }
}

A simple way to reproduce:

public static readonly DependencyProperty ObjValueProperty = DependencyProperty.Register(nameof(ObjValue), typeof(object), typeof(MainWindow), new PropertyMetadata(default(object)));

public object ObjValue
{
    get
    {
        return GetValue(ObjValueProperty);
    }
    set
    {
         SetValue(ObjValueProperty, value);
    }
}

protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e)
{
    if(e.Property == ObjValueProperty) { /*Breakpoint here*/ }
}

...
ObjValue = 7;
ObjValue = 7;

So, is this desired behaviour or a bug?

CSharpie
  • 9,195
  • 4
  • 44
  • 71

1 Answers1

0

I don't think this is a bug. Its just the way dotnet handles objects. It is same same if you try it without the context of DependencyProperties:

I created a small console application:

object v1 = 7;
object v2 = 7;
if (v1 == v2) Console.Write("are the same"); else Console.Write("are different");

I read "are different" in console. So generally, if objects are evaluated for equality then always by ReferenceEquals (even if they are boxed value types).

gofal3
  • 1,213
  • 10
  • 16