码迷,mamicode.com
首页 > 其他好文 > 详细

C# Equals

时间:2014-07-19 15:04:20      阅读:258      评论:0      收藏:0      [点我收藏+]

标签: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.

bubuko.com,布布扣
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

 bubuko.com,布布扣

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

 

C# Equals,布布扣,bubuko.com

C# Equals

标签:style   blog   http   color   strong   os   

原文地址:http://www.cnblogs.com/tekkaman/p/3854160.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!