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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 这是16年5月份编辑的一份比较杂乱适合自己观看的学习记录文档,今天18年5月份再次想写文章,发现简书还为我保存起的...
    Jenaral阅读 3,179评论 2 9
  • 注:都是在百度搜索整理的答案,如有侵权和错误,希告知更改。 一、哪些情况下的对象会被垃圾回收机制处理掉  当对象对...
    Jenchar阅读 3,315评论 3 2
  • 第5章 引用类型(返回首页) 本章内容 使用对象 创建并操作数组 理解基本的JavaScript类型 使用基本类型...
    大学一百阅读 3,691评论 0 4
  • 从网上复制的,看别人的比较全面,自己搬过来,方便以后查找。原链接:https://www.cnblogs.com/...
    lxtyp阅读 1,442评论 0 9
  • 一、基本数据类型 注释 单行注释:// 区域注释:/* */ 文档注释:/** */ 数值 对于byte类型而言...
    龙猫小爷阅读 4,476评论 0 16

友情链接更多精彩内容