博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
方法Equals和操作符==的区别
阅读量:5150 次
发布时间:2019-06-13

本文共 1748 字,大约阅读时间需要 5 分钟。

http://www.codeproject.com/Articles/584128/What-is-the-difference-between-equalsequals-and-Eq

 

When we create any object there are two parts to the object one is the content and the other is reference to that content.
So for example if you create an object as shown in below code:-
  1. “.NET interview questions” is the content.
  2. “o” is the reference to that content.
object o = ".NET Interview questions";

 

 
 
“==” compares if the object references are same while “.Equals()” compares if the contents are same.
 
So if you run the below code both “==” and “.Equals()” returns true because content as well as references are same.
 
 
object o = ".NET Interview questions";object o1 = o;Console.WriteLine(o == o1);Console.WriteLine(o.Equals(o1));Console.ReadLine();

 

True

True

Now consider the below code where we have same content but they point towards different instances. So if you run the below code both “==” will return false and “.Equals()” will return true.

 

object o = ".NET Interview questions";object o1 = new string(".NET Interview questions".ToCharArray());Console.WriteLine(o == o1);Console.WriteLine(o.Equals(o1));Console.ReadLine();

 

False

True

When you are using string data type it always does content comparison. In other words you either use “.Equals()” or “==” it always do content comparison.

You can also watch the following video of the above explanation at C# interview questions and answers :- Difference between "==" and ".Equals()" ?

<OBJECT type="application/x-shockwave-flash" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=3,0,0,0" WIDTH="640" HEIGHT="360" data="http://www.youtube.com/v/3IReFdq5d7o?version=3&feature=player_detailpage"></OBJECT>

转载于:https://www.cnblogs.com/chucklu/p/4533309.html

你可能感兴趣的文章
处理程序“PageHandlerFactory-Integrated”在其模块列表中有一个错误模块“Manag
查看>>
01: socket模块
查看>>
mysql触发器
查看>>
淌淌淌
查看>>
web页面实现指定区域打印功能
查看>>
win10每次开机都显示“你的硬件设置已更改,请重启电脑……”的解决办法
查看>>
C++有关 const & 内敛 & 友元&静态成员那些事
查看>>
函数积累
查看>>
Swift 入门之简单语法(六)
查看>>
shim和polyfill有什么区别
查看>>
〖Python〗-- IO多路复用
查看>>
栈(括号匹配)
查看>>
Java学习 · 初识 面向对象深入一
查看>>
源代码如何管理
查看>>
vue怎么将一个组件引入另一个组件?
查看>>
bzoj1040: [ZJOI2008]骑士
查看>>
LeetCode 74. Search a 2D Matrix(搜索二维矩阵)
查看>>
利用SignalR来同步更新Winfrom
查看>>
反射机制
查看>>
CocoaPod
查看>>