c# How to find if two objects are equal

前端 未结 6 1962
北荒
北荒 2020-12-08 02:40

I want to know the best way to compare two objects and to find out if they\'re equal. I\'m overriding both GethashCode and Equals. So a basic class looks like:



        
6条回答
  •  一向
    一向 (楼主)
    2020-12-08 03:34

    You could serialise the two objects to JSON, then compare the two strings to see if they are the same.

    For example

    JavaSriptSerializer serialiser = new JavaScriptSerializer();
    
    string t1String = serialiser.Serialize(t);
    string t2String = serialiser.Serialize(t2);
    
    if(t1String == t2String)
       return true; //they are equal
    else
       return false;
    

提交回复
热议问题