C # equals

Source: Internet
Author: User

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 ()

Compare whether two values are equal.

Refer:

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

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.