C# Equals

來源:互聯網
上載者:User

標籤:style   blog   http   color   strong   os   

C# Equals

1、Object.Equals()

  The type of comparison between the current instance and the obj parameter depends on whether the current instance is a reference type or a value type. 

  If the current instance is a reference type, the Equals(Object) method tests for reference equality, and a call to the Equals(Object) method is equivalent to a call to the ReferenceEquals method. Reference equality means that the object variables that are compared refer to the same object.

using System;// Define a reference type that does not override Equals.public class Person{   private string personName;   public Person(string name)   {      this.personName = name;   }   public override string ToString()   {      return this.personName;   }}public class Example{   public static void Main()   {      Person person1a = new Person("John");      Person person1b = person1a;      Person person2 = new Person(person1a.ToString());      Console.WriteLine("Calling Equals:");       Console.WriteLine("person1a and person1b: {0}", person1a.Equals(person1b));                     Console.WriteLine("person1a and person2: {0}", person1a.Equals(person2));        Console.WriteLine("\nCasting to an Object and calling Equals:");      Console.WriteLine("person1a and person1b: {0}", ((object) person1a).Equals((object) person1b));      Console.WriteLine("person1a and person2: {0}", ((object) person1a).Equals((object) person2));    }}// The example displays the following output://       person1a and person1b: True//       person1a and person2: False//       //       Casting to an Object and calling Equals://       person1a and person1b: True//       person1a and person2: False
View Code

 

2、ValueType.Equals()

  比較兩個值是否相等。

參考:

1、http://msdn.microsoft.com/zh-cn/library/bsc2ak47(v=vs.110).aspx

2、http://msdn.microsoft.com/zh-cn/library/2dts52z7(v=vs.110).aspx

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.