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

 

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

最新文章

  1. Android 开发一定要看的15个实战项目
  2. eclipse建立springMVC 简单项目
  3. sql例子
  4. Hibernate SQL方言 (hibernate.dialect) Spring配置文件applicationContext.xml
  5. NetworkComms V3 之同步收发数据
  6. C/C++类型转换
  7. ICBC中的路由设置
  8. spring-data-redis工程
  9. iptables防火墙详解
  10. Hadoop学习笔记四
  11. linux之文件权限+用户权限
  12. 编辑器之神---vim
  13. 【OpenStack】network相关知识学习
  14. 《吃了么》Alpha版本发布说明
  15. 【每日一题】 UVA - 340 阅读理解+模拟
  16. python-demo实例
  17. 【BZOJ2683】简单题
  18. Android Studio 使用感受 错误解决
  19. Spark2.0机器学习系列之8:多类分类问题(方法归总和分类结果评估)
  20. 解决VS2010中工具箱里没有WPM

热门文章

  1. numpy 排序, 查询功能
  2. Jacoco在eclipse上的集成使用
  3. 【转】Linux 静态库与共享库的使用
  4. win10下安装并启动zookeeper
  5. DeepLearning4J 环境搭建【转】
  6. 小程序picker组件当range是objectArray时的使用
  7. Tomcat的文件列表服务
  8. oracle建立用户与授权(转载)
  9. nginx与tomcat整合
  10. qcow2磁盘加密及libvirt访问