none
How to compare the contents of clone object with the original one correctly? RRS feed

  • Question

  • Dear All,

    How to compare the contents of clone object with the original one correctly?

    Here is my test, I cannot get the "True" by the following statements.

    Where is wrong?

                TreeNode tn1 = new TreeNode("treeNode1");
                TreeNode tn2 = (TreeNode)tn1.Clone();
                
                Debug.WriteLine("Object.Equals = {0}", tn1.Equals(tn2));

    Here is the snapshot of output

    Thanks and Best regards,

    E-John

    Tuesday, July 3, 2018 2:12 PM

Answers

  • Reference types (of which TreeNode is one) are compared by underlying reference. Unless they are the same exact object then they won't compare the same. This is by design and the CLR is built on this behavior.

    object obj1 = new object();
    object obj2 = obj1;
    
    var willBeTrue = obj1 == obj2;

    There are a few cases where a reference type won't follow reference semantics (strings come to mind) but this is the exception, not the norm. So there should really never be a case where a cloned object would be equal to the original, they are different objects.

    Only the individual types know whether they are equal or not so any type that supports the concept of comparison will implement either IComparable<T> and/or IEquatable. For reference types this is rarely done but for value types it almost always is. Given an arbitrary object there is no way to determine equality outside the above interfaces.

    For a very specific case you can create your own rules and that is what the IEqualityComparer interface is for. You can define custom comparison rules for types based upon your needs. In the case of a TreeNode for example you might consider them to be equal if they have the same Text. But in another case you might consider them equal if they have the same Text, Parent and Tag. It really depends upon your needs. If you're associating data with the nodes then I would recommend that you instead compare the Tags themselves for equality.


    Michael Taylor http://www.michaeltaylorp3.net

    • Marked as answer by E-John Wednesday, July 4, 2018 1:38 AM
    Tuesday, July 3, 2018 5:14 PM
    Moderator
  • Hi,

    I wrote a series of post expalining how equality works for primitive types, value types and reference types. The following is the link to first part:

    Equality in .NET

    You might want to read those.

    Hope it helps!



    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered "Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]


    Blog | LinkedIn | Stack Overflow | Facebook
    profile for Ehsan Sajjad on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by E-John Friday, July 6, 2018 2:36 PM
    Friday, July 6, 2018 2:28 PM

All replies

  • Reference types (of which TreeNode is one) are compared by underlying reference. Unless they are the same exact object then they won't compare the same. This is by design and the CLR is built on this behavior.

    object obj1 = new object();
    object obj2 = obj1;
    
    var willBeTrue = obj1 == obj2;

    There are a few cases where a reference type won't follow reference semantics (strings come to mind) but this is the exception, not the norm. So there should really never be a case where a cloned object would be equal to the original, they are different objects.

    Only the individual types know whether they are equal or not so any type that supports the concept of comparison will implement either IComparable<T> and/or IEquatable. For reference types this is rarely done but for value types it almost always is. Given an arbitrary object there is no way to determine equality outside the above interfaces.

    For a very specific case you can create your own rules and that is what the IEqualityComparer interface is for. You can define custom comparison rules for types based upon your needs. In the case of a TreeNode for example you might consider them to be equal if they have the same Text. But in another case you might consider them equal if they have the same Text, Parent and Tag. It really depends upon your needs. If you're associating data with the nodes then I would recommend that you instead compare the Tags themselves for equality.


    Michael Taylor http://www.michaeltaylorp3.net

    • Marked as answer by E-John Wednesday, July 4, 2018 1:38 AM
    Tuesday, July 3, 2018 5:14 PM
    Moderator
  • Hi CoolDadTx,

    Now I see it.

    For customers' application,

    1. Define the equality for clone object and original object based on customers' needs.

    2. Implement deep clone method and the custom comparison rules for types based on customer's needs.

    3. Apply these methods(clone and equality) in application.

    Thanks for your detailed explanations.

    It is very helpful. Thank you very much.

    Best regards,

    E-John

    Wednesday, July 4, 2018 1:38 AM
  • Hi,

    I wrote a series of post expalining how equality works for primitive types, value types and reference types. The following is the link to first part:

    Equality in .NET

    You might want to read those.

    Hope it helps!



    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered "Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]


    Blog | LinkedIn | Stack Overflow | Facebook
    profile for Ehsan Sajjad on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by E-John Friday, July 6, 2018 2:36 PM
    Friday, July 6, 2018 2:28 PM
  • Hi Ehsan Sajjad,

    Yes, thanks for your sharing.

    Best regards,

    E-John

    Friday, July 6, 2018 2:36 PM