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的更多相关文章

  1. equals变量在前面或者在后面有什么区别吗?这是一个坑点

    我就不废话那么多,直接上代码: package sf.com.mainTest; public class Test { public static void main(String[] args) ...

  2. 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 ...

  3. 【特种兵系列】String中的==和equals()

    1. 小样示例 public static void main(String[] args) { String a = "a" + "b" + 123; Str ...

  4. (转)浅谈Java中的equals和==

    原文地址: http://www.cnblogs.com/dolphin0520/p/3592500.html 在初学Java时,可能会经常碰到下面的代码: 1 String str1 = new S ...

  5. 浅谈Java中的equals和==(转)

    浅谈Java中的equals和== 在初学Java时,可能会经常碰到下面的代码: 1 String str1 = new String("hello"); 2 String str ...

  6. List<T>Find方法,FindAll方法,Contains方法,Equals方法

    假如传入的T是一个类, List<MessageInfos> MessageInfos = new List<MessageInfos>(); MessageInfos= Me ...

  7. 让代码重构渐行渐远系列(3)——string.Equals取代直接比较与非比较

    重构背景及原因 最近由于项目组的人员在不断扩充,导致项目中代码风格各异,大有百花齐放甚至怒放之势.考虑到团队的生存与发展,经过众人多次舌战之后,最终决定项目组根据业务分成几个小分队,以加强团队管理与提 ...

  8. [java] 更好的书写equals方法-汇率换算器的实现(4)

    [java] 更好的书写equals方法-汇率换算器的实现(4) // */ // ]]>   [java] 更好的书写equals方法-汇率换算器的实现(4) Table of Content ...

  9. Equals和ReferenceEquals

    稍微分析下一下两个方法的区别: public static bool Equals(object objA, object objB); public static bool ReferenceEqu ...

  10. 【原创】Java和C#下String类型中的==和equals的原理与区别

    一.Java下 1.几个例子 public static void main(String[] arge) { String str1 = new String("1234"); ...

随机推荐

  1. Sprint第一个冲刺(第三天)

    一.Sprint介绍 今天我们完成了简单登录界面及美化的任务,完成了此次整个Sprint的百分之十五. 下面是实验截图: 二.Sprint周期 看板: 燃尽图:

  2. 连接linux

    mac下连接linux 1 iTerm2 方式一: 在iTerm2的命令行下,输入: //-p端口号(linux默认是22),用户名@linux的IPssh -p 22 username@ip//按e ...

  3. IIS6自带FTP安装及配置方法

    参考:http://v.huweishen.com/video/9.html ·IIS自带的FTP服务是最安全的,不会像Serv-U那样存在各种漏洞:但其配置过程也比较复杂. ·本节将以虚拟目录方式, ...

  4. 教你30分钟学会XAML

    1.狂妄的WPF 相对传统的Windows图形编程,需要做很多复杂的工作,引用许多不同的API.例如:WinForm(带控件表单).GDI+(2D图形).DirectX API(3D图形)以及流媒体和 ...

  5. 安装QConfig备忘

    下载wget https://github.com/Qihoo360/QConf/archive/1.2.1.tar.gz 解压tar -zxf 1.2.1.tar.gz进入目录cd QConf-1. ...

  6. Mock&Spring集成

    Mock&Spring集成 常规Mock单元测试 请参考上一篇文档Mock mock框架的功能性对比 http://jmockit.github.io/MockingToolkitCompar ...

  7. 紫金桥OPC接口使用技巧

    OPC接口使用技巧 OPC接口是由OPC基金会制定的,基于DCOM技术的,用于控制系统软件之间进行数据通讯的接口规范.由于其开放性和高效性,现在已被广泛应用于自动化控制领域及生产信息管理中.紫金桥软件 ...

  8. s=a+aa+aaa+aaaa+aa...a的值,其中a是一个数字。例如2+22+222+2222+22222(此时共有5个数相加),几个数相加由用户控制。

    package com.hanqi; import java.util.*; public class yonghukongzhi { public static void main(String[] ...

  9. 简单的PL/SQl链接远程ORACLE数据库方法

    简单的PL/SQl链接远程ORACLE数据库方法 PLSQL Developer新手使用教程 pasting

  10. Git回版本回退

    这里我们使用命令行的方式对已经提交的版本进行强行回退操作~~~ 一.将git的安装目录bin放到path路径中, 如下图所示: 二.进入cmd界面,依次输入下面内容即可(git 远程仓库 回退到指定版 ...