The difference between Equals and ==

Today, I saw an article about Equals and ==, something confuses me. http://forums.asp.net/t/1511559.aspx
So I do it myself.
 
   1:  static void Main(string[] args)
   2:  {
   3:      DoEQ();
   4:      Console.ReadLine();
   5:  }
   6:   
   7:  private static void DoEQ()
   8:  {
   9:      Console.WriteLine("For value datatype:");
  10:      int i = 6;
  11:      int j = 6;
  12:      int h = 8;
  13:      Console.WriteLine("6==6 {0}",i==j);
  14:      Console.WriteLine("6.Equals(6) {0}", i.Equals(j));
  15:      Console.WriteLine("6==8 {0}", i == h);
  16:      Console.WriteLine("6.Equals(8) {0}", i.Equals(h));
  17:   
  18:      Console.WriteLine();
  19:      Console.WriteLine("For reference datatype:");
  20:      StringBuilder iC = new StringBuilder("My");
  21:      StringBuilder jC = new StringBuilder("My");
  22:      StringBuilder hC = new StringBuilder("My Test");
  23:      Console.WriteLine("StringBuilder('My')==StringBuilder('My') {0}", iC == jC);
  24:      Console.WriteLine("StringBuilder('My').Equals(StringBuilder('My')) {0}", iC.Equals(jC));
  25:      Console.WriteLine("StringBuilder('My')==StringBuilder('My Test') {0}", iC == hC);
  26:      Console.WriteLine("StringBuilder('My').Equals(StringBuilder('My Test')) {0}", iC.Equals(hC));
  27:   
  28:      Console.WriteLine();
  29:      Console.WriteLine("For special reference datatype(string):");
  30:      string iStr = "Test";
  31:      string jStr = "Test";
  32:      string hStr = "Just a Test";
  33:      Console.WriteLine("\"Test\"==\"Test\" {0}", iStr == jStr);
  34:      Console.WriteLine("\"Test\".Equals(\"Test\") {0}", iStr.Equals(jStr));
  35:      Console.WriteLine("\"Test\"==\"Just a Test\" {0}", iStr == hStr);
  36:      Console.WriteLine("\"Test\".Equals(\"Just a Test\") {0}", iStr.Equals(hStr));
  37:   
  38:      Console.WriteLine();
  39:      Console.WriteLine("For null and  reference datatype:");
  40:      string iN = null;
  41:      string jN = null;
  42:      string hN = "Test";
  43:      Console.WriteLine("Null==Null {0}", iN == jN);
  44:      //Console.WriteLine("Null.Equals(Null) {0}", iN.Equals(jN));
  45:      Console.WriteLine("Null==\"Test\" {0}", iN == hN);
  46:      //Console.WriteLine("Null.Equals(\"Test\") {0}", iN.Equals(hN));
  47:      Console.WriteLine("\"Test\".Equals(Null) {0}", hN.Equals(iN));
  48:   
  49:      Console.WriteLine();
  50:      Console.WriteLine("For special value datatype(zero):");
  51:      int iZ = 0;
  52:      short jZ =0;
  53:      Console.WriteLine("0i==0s {0}", iZ == jZ);
  54:      Console.WriteLine("0i.Equals(0s) {0}", iZ.Equals(jZ));
  55:  }

image

Conclusion:

  1. By value type, there’s no difference between Equals and ==
  2. By reference type, Equals compare the value of the two objects, the == compare whether the two objects point the same address.
  3. For different value type, Equals and == work the same way.

Functions disassembled by Reflector:

   1:  [TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")]
   2:  public virtual bool Equals(object obj)
   3:  {
   4:      return RuntimeHelpers.Equals(this, obj);
   5:  }
   

 

   1:  [TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")]
   2:  public static bool Equals(object objA, object objB)
   3:  {
   4:      return ((objA == objB) || (((objA != null) && (objB != null)) && objA.Equals(objB)));
   5:  }
 
   
   1:  [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success), TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")]
   2:  public static bool ReferenceEquals(object objA, object objB)
   3:  {
   4:      return (objA == objB);
   5:  }
 
 

7 Comments

Comments have been disabled for this content.