none
comparing 2 decimal with same value through reflection result false RRS feed

  • Question

  • .net 4.7.1

    i'm doing a function to compare 2 objects of same type, identifying data differences.

            public void GetDifference(object preChg, object postChg)
            {
                var preT = preChg.GetType();
                var postT = postChg.GetType();
    
                var postPList = postT.GetProperties();
                foreach(var postP in postPList)
                {
                    var preV = postP.GetValue(preChg);
                    var postV = postP.GetValue(postChg);
    
                    if (preV == postV) continue;

    when comparing a property of decimal, both objects are storing 1000, the last checking yield "false"!

    if i tried preV.Equals(postV), it gives true

    i don't understand why comparing a value type data for this yield false.


    • Edited by Kelmen Wednesday, November 15, 2017 8:14 AM
    Wednesday, November 15, 2017 8:13 AM

Answers

  • GetValue returns objects, and the next sample demonstrates the behavior of decimals after conversion to objects (“boxing”):

       decimal d = 1000;

       object o1 = d;

       object o2 = d;

       bool b = o1 == o2; // False

    It is False since we have two different new objects in the memory, and this ‘==’ compares the references, not the contents.

    You can also use another Equals:

       bool b = object.Equals( o1, o2 ); // True

    It is True since this Equals compares the contents of the objects, not only the references.





    Friday, November 17, 2017 11:13 AM

All replies

  • Hi,

    Thank you for posting here.

    For your question, the code you provided could not reproduced the error.

    The difference caused by the difference between == and equals. For the difference you could refer to the threads in SO. It gives a good explanation.

    https://stackoverflow.com/questions/814878/c-sharp-difference-between-and-equals

    https://stackoverflow.com/questions/144530/or-equals

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, November 17, 2017 9:50 AM
    Moderator
  • GetValue returns objects, and the next sample demonstrates the behavior of decimals after conversion to objects (“boxing”):

       decimal d = 1000;

       object o1 = d;

       object o2 = d;

       bool b = o1 == o2; // False

    It is False since we have two different new objects in the memory, and this ‘==’ compares the references, not the contents.

    You can also use another Equals:

       bool b = object.Equals( o1, o2 ); // True

    It is True since this Equals compares the contents of the objects, not only the references.





    Friday, November 17, 2017 11:13 AM
  • i understand comparing 2 reference type object using == only yield true if both referring same object/reference

    but decimal afaik, is a value type data

    so the reason about "comparing object", doesn't stand on it.

    to me, this looks more like a bug in .net.

    anyway, i'm using equal now.

    the coding difference using == compare to equal, in handling null comparison, but it's not a rocket science, i can deal with that.


    Monday, November 20, 2017 4:44 AM
  • if the property is string or int, the result is different compare to decimal
    Thursday, November 23, 2017 10:47 AM
  • Hi Kelmen,

    Are you sure that preV==postV is true when property is int?

    As for string type, how do you assign your string property? By this mean model.property = "string" or model.string = new string(char[])?

    Best Regards,

    Charles He


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Thursday, November 23, 2017 1:55 PM
  • i retest the int with same value, same result as decimal, == yield false even same value

    so i have to use Equal to compare the content

    Friday, November 24, 2017 5:58 AM