title: Fastest way to compare the properties of two objects
body: <p>Someone suggested using reflection for this. The method I have works great, but over 800,000 iterations, I came to the obvious conclusion (that most already came to) that reflection just doesn't cut it.</p>
  
  <p>Here's part of my Helper class:</p>
  
  <pre><code>public static class Helper
  {
      public static string[] ignoredProperties =
      { 
          "EntityState", 
          "EntityKey", 
          "Prop1", 
          "Prop2", 
          "Whatever", 
      };
  
      /// &lt;summary&gt;
      /// Check if properties of two objects are the same. Bypasses specified properties.
      /// &lt;/summary&gt;
      /// &lt;typeparam name="T"&gt;&lt;/typeparam&gt;
      /// &lt;param name="first"&gt;&lt;/param&gt;
      /// &lt;param name="other"&gt;&lt;/param&gt;
      /// &lt;param name="ignoreProperties"&gt;&lt;/param&gt;
      /// &lt;returns&gt;&lt;/returns&gt;
      public static bool PropertiesEquals&lt;T&gt;(this T first, T other, string[] ignoreProperties)
      {
          var propertyInfos = first.GetType().GetProperties();
          foreach (PropertyInfo propertyInfo in propertyInfos)
          {
              //Faster with custom method ? Nah...
              //if (FindElementIndex(ignoreProperties, propertyInfo.Name) &lt; 0)
  
              //Probably faster if hardcoded.... Nah, not really either...
              //if (propertyInfo.Name != "EntityKey" &amp;&amp; propertyInfo.Name != "EntityState" &amp;&amp; propertyInfo.Name != "Group_ID" &amp;&amp; propertyInfo.Name != "Import_status")
  
              if (Array.IndexOf(ignoreProperties, propertyInfo.Name) &lt; 0)
                  if (!Equals(propertyInfo.GetValue(first, null), propertyInfo.GetValue(other, null)))
                      return false;
          }
          return true;
      }
  
      public static int FindElementIndex(string[] input, string value)
      {
          int arraySize = input.Length - 1;
          Type valueType = value.GetType();
  
          for (int x = 0; x &lt;= arraySize; x++)
          {
              if (input[x] == value)
                  return x;
          }
  
          return -1;
      }
  </code></pre>
  
  <p>The problem is that those objects, depending on the type, can have up to 50 properties to check. So I can't really do a bunch of <code>if</code>s there.</p>
  
  <p>Is there any way I could speed this up a bit?</p>
  
  <p>Thanks.</p>