==
Object.Equals
Object.ReferenceEquals(Object, Object)
一.Equals
- 如果當前實例是引用類型,Equals(Object)方法等效于調用ReferenceEquals方法,比較對象引用是否相同。
- 如果當前實例是值類型:
- 這兩個對象是否屬于同一類型,并且值是否相等。(值為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
- 在沒有重載的情況下,==運算符和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
- Object.ReferenceEquals(ObjectA, ObjectB)
- 比較引用
- 方法不能被重寫, 因此,如果要測試兩個對象引用是否相等,并且不確定Equals方法的實現,可以ReferenceEquals調用方法。
- 如果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
- 如果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