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"); ...
随机推荐
- 百度地图API秘钥生成步骤
百度API
- mysql用触发器同步表
一.先复制表 : and DATE = '2016-09-26' or DATE = '2016-09-27'; 二.创建插入数据时的[触发器] [在phpmyadmin 运行时记得要修改语句定界符 ...
- list.ForEach的用法
Templist.ForEach(o => { var isSel = ReviewerFileRelationService.Where(s => s.PackageFileId == ...
- JDBC 流程
转载地址:https://blog.csdn.net/suwu150/article/details/52744952 JDBC编程的六个步骤: 准备工作中导入ojdbc文件,然后右键选中添加路 ...
- Metaweblog博客分发体验
在8月份OpenLiveWriter 这篇文章使用博客客户端撰写做了metaweblog的个人服务,支持通过OpenLiveWriter发博客到本站(OurJS),然后再分发到其他博客平台(目前就os ...
- python-redis-pipe文件
redis导入数据比较头疼的事情,涉及几千万,导入还是很耗时,通过生成pipe文件的方式比较快捷. python3.6.1版本 在linux环境下运行 with open("data1&qu ...
- 双口RAM,值得研究
在FPGA设计过程中,使用好双口RAM,也是提高效率的一种方法. 官方将双口RAM分为简单双口RAM和真双口RAM. 简单双口RAM只有一个写端口,一个读端口. 真双口RAM分别有两个写端口和两个读端 ...
- python稀疏矩阵得到每列最大k项的值,对list内为类对象的排序(scipy.sparse.csr.csr_matrix)
print(train_set.tdm) print(type(train_set.tdm)) 输出得到: (0, 3200) 0.264940780338 (0, 1682) 0.356545827 ...
- Tool:Visual Studio Code
ylbtech-Tool:Visual Studio Code Microsoft在2015年4月30日Build 开发者大会上正式宣布了 Visual Studio Code 项目:一个运行于 Ma ...
- 杂项:大数据 (巨量数据集合(IT行业术语))
ylbtech-杂项:大数据 (巨量数据集合(IT行业术语)) 大数据(big data),指无法在一定时间范围内用常规软件工具进行捕捉.管理和处理的数据集合,是需要新处理模式才能具有更强的决策力.洞 ...