1

Given the following class:

public class Employee
{
    private string _birthday;
    public string Birthday
    {
        get
        {
            if (DateTime.TryParse(_birthday, out DateTime dateOfBirth))
                return dateOfBirth.AddYears(2000 - dateOfBirth.Year).ToString("MMM dd yyyy");
            else
                return null;
        }
        set
        {
            _birthday = value;
        }
    }
}

And the following code:

static void Main()
{
    var obj1 = new Employee() { Birthday = "Aug 02 2000" };
    var obj2 = new Employee() { Birthday = "Aug 02 2000" };

    var propA = typeof(Employee).GetProperty("Birthday");

    object obj1_propA_object = propA.GetValue(obj1);
    object obj2_propA_object = propA.GetValue(obj2);
    dynamic obj1_propA_dynamic = propA.GetValue(obj1);
    dynamic obj2_propA_dynamic = propA.GetValue(obj2);

    if (obj1.Birthday == obj2.Birthday)
        Console.WriteLine("EQUALS");
    else
        Console.WriteLine("NOT EQUALS");
    // Writes EQUALS

    if (obj1_propA_object == obj2_propA_object)
        Console.WriteLine("EQUALS");
    else
        Console.WriteLine("NOT EQUALS");
    // Writes NOT EQUALS

    if (obj1_propA_dynamic == obj2_propA_dynamic)
        Console.WriteLine("EQUALS");
    else
        Console.WriteLine("NOT EQUALS");
    // Writes EQUALS    
}

I can't understand why the property values when treated as "objects" are not considered equal, but they are seen as equal when comparing them as strings or dynamic. It seems to have something to do with the fact that the values are modified in the getter, since if I change the getter to return just "_birthday" without modifying it the equality comparison as an object works.

I'm happy to use "dynamic" instead of "object", but I would simply like to understand why.

Brian Campbell
  • 161
  • 1
  • 12
  • A `dynamic` is just an `object` with magical powers. Those powers include being able to behave a lot like a Javascript object (hey, I don't know what my properties are, but try as and find out) or a Typescript `any`. If you call `object.Equals`, the `object` class knows nothing about the nature of the underlying object, so it basically does an `object.ReferenceEquals`; are the two object variables referring to the exact same object. I imagine that `dynamic.Equals` overrides that behavior and looks to see if the have the same properties, of the same type, and those props are equal – Flydog57 Aug 03 '20 at 01:28

1 Answers1

0

There are 2 types of equality to keep in mind:

  • Reference Equality
  • Value Equality

Below I linked a great in-depth explanation but the basic idea is:

  • Reference Equality: "Are these 2 Objects the same Object?"
  • Value Equality: "Do these 2 Objects have the same Value"?

https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/equality-operators

When you compare using the Object type it's looking if they are the same object. It returns false because they aren't the same object even though the values are the same. When you use the other variable types to compare you are checking the values against each other.

  • Comments are not for extended discussion; this conversation has been [moved to chat](https://chat.stackoverflow.com/rooms/219217/discussion-on-answer-by-brandon-kuenzi-unexpected-behavior-when-checking-equalit). – Machavity Aug 04 '20 at 18:14