C# Equals
【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
C# Equals的更多相关文章
- equals变量在前面或者在后面有什么区别吗?这是一个坑点
我就不废话那么多,直接上代码: package sf.com.mainTest; public class Test { public static void main(String[] args) ...
- How to implement equals() and hashCode() methods in Java[reproduced]
Part I:equals() (javadoc) must define an equivalence relation (it must be reflexive, symmetric, and ...
- 【特种兵系列】String中的==和equals()
1. 小样示例 public static void main(String[] args) { String a = "a" + "b" + 123; Str ...
- (转)浅谈Java中的equals和==
原文地址: http://www.cnblogs.com/dolphin0520/p/3592500.html 在初学Java时,可能会经常碰到下面的代码: 1 String str1 = new S ...
- 浅谈Java中的equals和==(转)
浅谈Java中的equals和== 在初学Java时,可能会经常碰到下面的代码: 1 String str1 = new String("hello"); 2 String str ...
- List<T>Find方法,FindAll方法,Contains方法,Equals方法
假如传入的T是一个类, List<MessageInfos> MessageInfos = new List<MessageInfos>(); MessageInfos= Me ...
- 让代码重构渐行渐远系列(3)——string.Equals取代直接比较与非比较
重构背景及原因 最近由于项目组的人员在不断扩充,导致项目中代码风格各异,大有百花齐放甚至怒放之势.考虑到团队的生存与发展,经过众人多次舌战之后,最终决定项目组根据业务分成几个小分队,以加强团队管理与提 ...
- [java] 更好的书写equals方法-汇率换算器的实现(4)
[java] 更好的书写equals方法-汇率换算器的实现(4) // */ // ]]> [java] 更好的书写equals方法-汇率换算器的实现(4) Table of Content ...
- Equals和ReferenceEquals
稍微分析下一下两个方法的区别: public static bool Equals(object objA, object objB); public static bool ReferenceEqu ...
- 【原创】Java和C#下String类型中的==和equals的原理与区别
一.Java下 1.几个例子 public static void main(String[] arge) { String str1 = new String("1234"); ...
随机推荐
- 【DUBBO】dubbo的registry配置
[一]:配置项 注册中心地址:zookeeper://ip:端口 <dubbo:registry address="注册中心的地址" check="启动时检查注册中 ...
- Junit+ant+JaCoCo集成使用
接上文,本文主要介绍Junit+ant+JaCoCo集成使用 1.工具的下载上传 需要的工具有: apache-ant-1.10.5-bin.tarapache-tomcat-8.0.50.tarja ...
- MDK警告 warning: #111-D: statement is unreachable
解析: "statement is unreachable"这句一般是说编译器认为程序执行不到这里. 因为本人运行程序的时候,再向前有一个While(1)循环, 理论上说除非你里面 ...
- kubernetes简单示例
1. 安装 yum install -y etcd kubernetes 2. 启动 systemctl start etcd systemctl start docker systemctl sta ...
- centos65安装docker遇到的问题
1.安装docker后启动显示内核太低(升级内核): 网上太多方案 2.升级内核后还是启动不了docker:执行下面语句 yum install device-mapper-event-libs 步骤 ...
- ASP.NET-自定义HttpModule与HttpHandler介绍
ASP.NET对请求处理的过程:当请求一个*.aspx文件的时候,这个请求会被inetinfo.exe进程截获,它判断文件的后缀(aspx)之后,将这个请求转交给 ASPNET_ISAPI.dll,A ...
- C++ 命名空间解释
using关键字 如果在程序中需要多次引用某个命名空间的成员,那么按照之前的说法,我们每次都要使用范围解析符来指定该命名空间,这是一件很麻烦的事情.为了解决这个问题,人们引入了using关键字.usi ...
- Kossel的一种滑块位置计算方法
做了一个小激光雕刻机之后,研究了一下这款3D打印机的结构和工作原理,一下就对这个运动过程很感兴趣,这三个杆是怎么联动使得喷头保持在一个平面上运动呢?打算先做一个架构,然后把激光器放在上面不是可以方便雕 ...
- java代码-------Runnable的用法
总结:主要是实现Runnable接口就必须重写run()方法,然后需要创建Thread类的对象,再调用start()方法 package com.s.x; public class testRunna ...
- grep 小技巧
转自:http://www.cnblogs.com/itech/archive/2012/10/18/2729944.html 1) grep命令加- E参数,这一扩展允许使用扩展模式匹配.例如,要抽 ...