Determine if all the properties in two classes are equal

I have method that updates the properties of a class and then updates that object in the database using a web service. I want to verify that the update succeeded by pulling the new object and comparing it to an object in memory.

Do I need to compare each property or will this determine if they have the same values for all the properties?

var areEqual = objectThatHasChanges.Equals(objectSavedToDatabase);

You will need to compare each property. If these are two reference-type object the equals method will just use default equal implementation which will check the objects are the same instances: https://msdn.microsoft.com/en-us/library/bsc2ak47(v=vs.110).aspx

If the current instance is a reference type, the Equals(Object) method tests for reference equality, and a call to the Equals(Object) method is equivalent to a call to the ReferenceEquals method. Reference equality means that the object variables that are compared refer to the same object. The following example illustrates the result of such a comparison. It defines a Person class, which is a reference type, and calls the Person class constructor to instantiate two new Person objects, person1a and person2, which have the same value. It also assigns person1a to another object variable, person1b. As the output from the example shows, person1a and person1b are equal because they reference the same object. However, person1a and person2 are not equal, although they have the same value.

You should either overload the "Equals" method and write your own which compare each or key properties (like last modify date, revision etc..) or you should write a method which will take these two objects and compare them.

If you have a lot of fields in different objects you can write generic method which uses reflection to iterate through all properties in object and compares with the other. Of course checking first if both objects are of the same type.

Object Equality in JavaScript, Equality is one of the most initially confusing aspects of JavaScript. You might suppose that if two objects have the same properties and all of their That comparison by reference basically checks to see if the objects given refer to the same� Different handles are always not equal. The order of handle values is purely arbitrary and has no connection to the state of the handle objects being compared. If the input arrays belong to different classes (including the case where one input array belongs to a non-handle class such as double) then the comparison is always false.

If the property names are the same, this is more or less trivial:

private static bool IsEqual(Model1 model, Model2 model2)
{
  long changes = 0;

  foreach (var pi in typeof(Model1).GetProperties(BindingFlags.Instance | BindingFlags.Public))
  {
    var secondModelPi = typeof(Model2).GetProperty(pi.Name, BindingFlags.Instance | BindingFlags.Public);
    if (secondModelPi != null)
    {
      if (!pi.GetValue(model).Equals(secondModelPi.GetValue(model2)))
      {
        changes++;
      }
    }
  }

  return changes == 0;
}

Object.Equals Method (System), Equals method calls the GetType method to determine whether the run-time types of the two objects are identical. If the method used a check of the form obj is � All their properties have the exactly same content, however the Assert.Equal (or Assert.AreEqual if you are using NUnit) will simply not state that they are equal. An example of that would be the

If you really want to check if update is successful then do something in this way:

string query = @"UPDATE [Entities] SET [Value] = [Value] + 1";

// SQL initialization

return (sqlCommand.ExecuteNonQuery() > 0);

ExecuteNonQuery returns the number of affected \ inserted rows. Thus, positive result means that update was successful.

If you, for some reasons, want to get entity from server after update then use the following approach:

string query = @"UPDATE [Entities] SET [Value] = [Value] + 1; 
SELECT * FROM [Entities] WHERE [Id] = SCOPE_IDENTITY()"

// SQL initialization

var reader = sqlCommand.ExecuteReader();
while (reader.Read())
{
    // Object initialization
}

These ways are better in terms of convenience and performance.

Object.is(), The Object.is() method determines whether two values are the same to both sides (if they are not the same Type) before testing for equality� As part of the refactoring I was doing to the load code for crawler projects I needed a way of verifying that new code was loading data correctly. As it would be extremely time consuming to manually compare the objects, I used Reflection to compare the different objects and their properties. This article briefly describes the process and provides a complete helper function you can use in your

To expound upon @zaitsman's answer, if you want to reuse this for multiple different types you would need to use generic types. I also converted the class to an extension method, as in my mind this should work similar to a LINQ query or .Equals.

//'this' Makes this an extension method if you do not want this to be an extension method just remove the 'this' keyword before model
private static bool IsSame<T,T2>(this T model, T2 model2)
    {
        long changes = 0;

        foreach (var pi in typeof(T).GetProperties(BindingFlags.Instance | BindingFlags.Public))
        {
            var secondModelPi = typeof(T2).GetProperty(pi.Name, BindingFlags.Instance | BindingFlags.Public);
            if (secondModelPi != null)
            {
                if (!pi.GetValue(model).Equals(secondModelPi.GetValue(model2)))
                {
                    changes++;
                }
            }
        }

        return changes == 0;
    }

Example Usage

model.IsSame(model2);

Equality comparisons and sameness, If the values have different types, the values are considered unequal. case: determining whether two values are functionally identical in all Internally, when an immutable property is redefined, the newly-specified value is� In Visual Basic, two variable references are considered identical if their pointers are the same, that is, if both variables point to the same class instance in memory. For example, in a Windows Forms application, you might want to make a comparison to determine whether the current instance ( Me) is the same as a particular instance, such as Form2. Visual Basic provides two operators to compare pointers.

equals(), compareTo(), and compare(), You want to know if two references are to the same object All Java classes that have a natural ordering implement this (String, Double, BigInteger, ). Equality by value- i.e. determining if two objects are equal by comparing their content. In Akka.NETall message classes are supposed to be immutable, which means a reference to a message is useless as soon as someone modifies it (because it creates a copy.)

How to check if two instances of a class have same field values?, In those kind of cases if we want to check whether all fields for two instances of the Object o2 = m.invoke(t2); if (!Objects.equals(o1, o2)) { return false; } } return true; } } <artifactId>compare-properties-example</artifactId>� As listed below. The rhombus has the following properties: All the properties of a parallelogram apply (the ones that matter here are parallel sides, opposite angles are congruent, and consecutive angles are supplementary). All sides are congruent by definition. The diagonals bisect the angles. Properties of Rhombus : Opposite sides are parallel. 2 Opposites angles are equal. Adjacent angles

Equality in Kotlin ('==', '===' and 'equals'), Let's figure out what are the types of checks available in Kotlin. Structural Equality ('=='). == operator is used to compare the data of two variables. The Ignore extension properties (IGNORE_EXTENSION_PROPERTIES in Python) option refers to additional information added to a feature class or table. For example, the features of two annotation feature classes can be identical but the feature classes may have different extension properties, such as different symbols in the symbol collection and

Comments
  • You have to compare classes or objects? it sound like you are comparing two objects
  • Object.Equals docs, its really your decision as to how much you need to compare to decide what is enough. Are you sure you need to do this at all? It sounds like it might be very expensive
  • The objects are entities, I was thinking just go through each property and compare each one but if I can do it with one line and it does the same thing then why not.
  • Check also this post here it shows the reflection based generic method to compare any two objects which I mentioned in my answer.