title: C# IF statement gives incorrect result
body: <p>I seem to have stumbled on a circumstance where the C# IF statement gives an incorrect result. I was attempting to write a version of Equals() to deep compare two instances of a class.</p>
  
  <p>Here is a simple test case with some debug:</p>
  
  <pre><code>namespace IfTest
  {
  class MyClass
  {
      public string String1
      { get; set; }
      public int Int1
      { get; set; }
      public float Float1
      { get; set; }
      public bool Bool1
      { get; set; }
  
      public MyClass(string s, int i, float f, bool b)
      {
          String1 = s;
          Int1 = i;
          Float1 = f;
          Bool1 = b;
      }
  
      public override bool Equals(object otherInstance)
      {
          bool isEqual = true;
          MyClass other = (MyClass)otherInstance;
          int good = 0, bad = 0;
  
          // Compare everything in 'other' to 'this', using reflection
          Type sourceType = other.GetType();
          Type destinationType = this.GetType();
  
          foreach (PropertyInfo sourceProperty in sourceType.GetProperties())
          {
              PropertyInfo destinationProperty = destinationType.GetProperty(sourceProperty.Name);
              if (destinationProperty == null)
              {
                  Console.WriteLine("Destination {0} is null", sourceProperty.Name);
                  isEqual = false;
                  bad++;
              }
              else
              {
                  var x = sourceProperty.GetValue(other);
                  var y = destinationProperty.GetValue(this);
                  //if (sourceProperty.GetValue(other, null) != destinationProperty.GetValue(this, null))
  
                  if (x != y)
                  {
                      Console.WriteLine("Name {0}: {1} {2} different", sourceProperty.Name, x, y);
                      isEqual = false;
                      bad++;
                  }
                  else
                  {
                      Console.WriteLine("Name {0}: {1} {2} same", sourceProperty.Name, x, y);
                      good++;
                  }
              }
          }
  
          Console.WriteLine("Good: {0}. Bad {1}", good, bad);
          return isEqual;
      }
  }
  }
  
  
  using System;
  
  namespace IfTest
  {
  class Program
  {
      static void Main(string[] args)
      {
          MyClass a = new MyClass("abc", 23, 45.67F, false);
          MyClass b = new MyClass("abc", 23, 45.67F, false);
  
          // Test IF usually works with var's
          var i = a.Int1;
          var j = b.Int1;
          Console.WriteLine("Main test {0}",
              (i == j) ? "OK" : "Fail");
  
          a.Equals(b);
  
          Console.ReadLine();
      }
  
  }
  }
  </code></pre>
  
  <p>In MyClass.Equals() I have commented out the line 
      <code>if (sourceProperty.GetValue(other, null) != destinationProperty.GetValue(this, null))</code></p>
  
  <p>and put the 2 property values in temporary variables.</p>
  
  <p>Running this gives:</p>
  
  <pre><code>Main test OK
  Name String1: abc abc same
  Name Int1: 23 23 different
  Name Float1: 45.67 45.67 different
  Name Bool1: False False different
  Good: 1. Bad 3
  </code></pre>
  
  <p>which shows that the IF fails for numeric types. The same result occurs if I change the x and y assignments to:
      <code>var x = sourceProperty.GetValue(other);</code>
      <code>var y = sourceProperty.GetValue(other);</code></p>
  
  <p>I can work round it by adding something like:</p>
  
  <pre><code>if (x is int)
  {
      if ((int) x == (int)y)
          Console.WriteLine("INT Name {0}: {1} {2} same", sourceProperty.Name, x, y);
  }
  </code></pre>
  
  <p>but I would have to test every numeric type.</p>
  
  <p>Is this a C# problem or have I done something silly? I am using Visual Studio Express 2013 for Desktop version 12.0.31101.00 Update 4, .Net version 4.5.50938 and Visual C# 2013.</p>