C# ==、Equals、ReferenceEquals

==
Object.Equals
Object.ReferenceEquals(Object, Object)

一.Equals

  1. 如果當前實例是引用類型,Equals(Object)方法等效于調用ReferenceEquals方法,比較對象引用是否相同。
  2. 如果當前實例是值類型:
  • 這兩個對象是否屬于同一類型,并且值是否相等。(值為12的 Byte 對象不等于值為12的 Int32 對象,因為這兩個對象具有不同的運行時類型.)
  • 兩個對象的公共字段和私有字段的值相等
using System;

// Define a value type that does not override Equals.
public struct Person
{
   private string personName;
   
   public Person(string name)
   {
      this.personName = name;
   }
   
   public override string ToString()
   {
      return this.personName;
   }
}

public struct Example
{
   public static void Main()
   {
      Person person1 = new Person("John");
      Person person2 = new Person("John");
      
      Console.WriteLine("Calling Equals:"); 
      Console.WriteLine(person1.Equals(person2)); 
      
      Console.WriteLine("\nCasting to an Object and calling Equals:");
      Console.WriteLine(((object) person1).Equals((object) person2));  
   }
}
// The example displays the following output:
//       Calling Equals:
//       True
//       
//       Casting to an Object and calling Equals:
//       True
  1. 在沒有重載的情況下,==運算符和Object.Equals方法效果一樣
byte value1 = 12;
int value2 = 12;

object object1 = value1;
object object2 = value2;

Console.WriteLine("{0} ({1}) = {2} ({3}): {4}",
                  object1, object1.GetType().Name,
                  object2, object2.GetType().Name,
                  object1.Equals(object2));

// The example displays the following output:
//        12 (Byte) = 12 (Int32): False
using System;

// Define a value type that does not override Equals.
public struct Person
{
   private string personName;
   
   public Person(string name)
   {
      this.personName = name;
   }
   
   public override string ToString()
   {
      return this.personName;
   }
}

public struct Example
{
   public static void Main()
   {
      Person person1 = new Person("John");
      Person person2 = new Person("John");
      
      Console.WriteLine("Calling Equals:"); 
      Console.WriteLine(person1.Equals(person2)); 
      
      Console.WriteLine("\nCasting to an Object and calling Equals:");
      Console.WriteLine(((object) person1).Equals((object) person2));  
   }
}
// The example displays the following output:
//       Calling Equals:
//       True
//       
//       Casting to an Object and calling Equals:
//   

二.ReferenceEquals

  1. Object.ReferenceEquals(ObjectA, ObjectB)
  • 比較引用
  • 方法不能被重寫, 因此,如果要測試兩個對象引用是否相等,并且不確定Equals方法的實現,可以ReferenceEquals調用方法。
  1. 如果objA和objB是值類型
  • 則在將其傳遞給方法之前將它們裝箱。 這意味著,如果和objA objB均表示值類型的同一個實例,則ReferenceEquals方法仍返回false,如:
int int1 = 3;
Console.WriteLine(Object.ReferenceEquals(int1, int1));
Console.WriteLine(int1.GetType().IsValueType);

// The example displays the following output:
//       False
//       True     
  1. 如果objA 和objB是字符串
  • String.IsInterned(String) :如果 str 在公共語言運行時的暫存池中,則返回對它的引用;否則返回 null。

    • 拘留池節省了字符串存儲。 如果將文本字符串常量分配給幾個變量,則每個變量將設置為引用拘留池中的同一常量,而不是引用具有相同值的 String 的多個不同實例。
  • ReferenceEquals不會執行值相等性測試

在下面的示例中s1 , s2和相等,因為它們是單個暫存字符串的兩個實例。 不過, s3和s4不相等,因為雖然它們具有相同的字符串值,但不會對該字符串進行暫留。

String s1 = "String1";
String s2 = "String1";
Console.WriteLine("s1 = s2: {0}", Object.ReferenceEquals(s1, s2));
Console.WriteLine("{0} interned: {1}", s1, 
                  String.IsNullOrEmpty(String.IsInterned(s1)) ? "No" : "Yes");

String suffix = "A";
String s3 = "String" + suffix;
String s4 = "String" + suffix;
Console.WriteLine("s3 = s4: {0}", Object.ReferenceEquals(s3, s4));
Console.WriteLine("{0} interned: {1}", s3, 
                  String.IsNullOrEmpty(String.IsInterned(s3)) ? "No" : "Yes");

// The example displays the following output:
//       s1 = s2: True
//       String1 interned: Yes
//       s3 = s4: False
//       StringA interned: No
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 這是16年5月份編輯的一份比較雜亂適合自己觀看的學習記錄文檔,今天18年5月份再次想寫文章,發現簡書還為我保存起的...
    Jenaral閱讀 2,831評論 2 9
  • 注:都是在百度搜索整理的答案,如有侵權和錯誤,希告知更改。 一、哪些情況下的對象會被垃圾回收機制處理掉 ?當對象對...
    Jenchar閱讀 3,240評論 3 2
  • 第5章 引用類型(返回首頁) 本章內容 使用對象 創建并操作數組 理解基本的JavaScript類型 使用基本類型...
    大學一百閱讀 3,268評論 0 4
  • 從網上復制的,看別人的比較全面,自己搬過來,方便以后查找。原鏈接:https://www.cnblogs.com/...
    lxtyp閱讀 1,357評論 0 9
  • 一、基本數據類型 注釋 單行注釋:// 區域注釋:/* */ 文檔注釋:/** */ 數值 對于byte類型而言...
    龍貓小爺閱讀 4,288評論 0 16