谁可以给我解释一下这个? [英] Can someone explain this to me?

查看:76
本文介绍了谁可以给我解释一下这个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对象a = 5;

对象b = 5;

if(a!= b)

抛出新的InvalidOperationException(" a!= b");


为什么抛出异常?我猜这是与拳击有关的事情

因为以下不会抛出异常


对象a = 5;

对象b = 5;

if(!a.Equals(b))

抛出新的InvalidOperationException(" a!= b");


但是为什么盒装对象不使用它所包含的对象的Equals方法?

object a = 5;
object b = 5;
if (a != b)
throw new InvalidOperationException("a != b");

Why is the exception thrown? I guessed it is something to do with boxing
because the following does not throw an exception

object a = 5;
object b = 5;
if (!a.Equals(b))
throw new InvalidOperationException("a != b");

but why doesn''t the boxed object use the Equals method of the object it
contains?

推荐答案

Peter,


它抛出第一个的原因是你正在进行

参考比较。拳击在

堆上创建了两个具有相同盒装值的独立对象。


在第二个示例中,Equals的实现必须检查<装箱价值中的
值(与== /!=相对)。


希望这会有所帮助。

-

- Nicholas Paldino [.NET / C#MVP]

- mv * @ spam。 guard.caspershouse.com


" Peter Morris [Droopy eyes software]" < pe ** @ droopyeyes.no.com.spamwrote在

消息新闻:OQ ************** @ TK2MSFTNGP04.phx.gbl ...
Peter,

The reason that it throws in the first one is that you are performing a
reference comparison. The boxing has created two separate objects on the
heap which have the same boxed value.

In the second example, the implementation of Equals must check the
values in the boxed values (as opposed to ==/!=).

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Peter Morris [Droopy eyes software]" <pe**@droopyeyes.no.com.spamwrote in
message news:OQ**************@TK2MSFTNGP04.phx.gbl...

object a = 5;

object b = 5;

if(a!= b)

抛出新的InvalidOperationException(" a!= b");


为什么抛出异常?我猜这是与拳击有关的事情

因为以下不会抛出异常


对象a = 5;

对象b = 5;

if(!a.Equals(b))

抛出新的InvalidOperationException(" a!= b");


但是为什么盒装对象不使用它所包含的对象的Equals方法?
object a = 5;
object b = 5;
if (a != b)
throw new InvalidOperationException("a != b");

Why is the exception thrown? I guessed it is something to do with boxing
because the following does not throw an exception

object a = 5;
object b = 5;
if (!a.Equals(b))
throw new InvalidOperationException("a != b");

but why doesn''t the boxed object use the Equals method of the object it
contains?





Peter Morris [Droopy eyes software]写道:

Peter Morris [Droopy eyes software] wrote:

object a = 5;

对象b = 5;

if(a!= b)

抛出新的InvalidOperationException(" a!= b");


为什么抛出异常?我猜这是与拳击有关的事情

因为以下不会抛出异常


对象a = 5;

对象b = 5;

if(!a.Equals(b))

抛出新的InvalidOperationException(" a!= b");


但是为什么盒装对象不使用它b / b
包含的对象的Equals方法?
object a = 5;
object b = 5;
if (a != b)
throw new InvalidOperationException("a != b");

Why is the exception thrown? I guessed it is something to do with boxing
because the following does not throw an exception

object a = 5;
object b = 5;
if (!a.Equals(b))
throw new InvalidOperationException("a != b");

but why doesn''t the boxed object use the Equals method of the object it
contains?



在第一个例子中,代码检查对象

引用是否相同(即变量是否指向相同的

对象?),它们不是。在第二个例子中,代码是

检查对象值是否相同,它们是什么。


John

In the first example, the code is checking to see if the object
references are the same (i.e. do the variables refer to the same
object?), which they are not. In the second example, the code is
checking to see if the object values are the same, which they are.

John


Peter Morris [Droopy眼睛软件]< pe ** @ droopyeyes.no.com.spam>

写道:
Peter Morris [Droopy eyes software] <pe**@droopyeyes.no.com.spam>
wrote:

对象a = 5;

对象b = 5;

if(a!= b)

抛出新的InvalidOperationException(" a!= b");


为什么抛出异常?我猜这是与拳击有关的事情

因为以下不会抛出异常


对象a = 5;

对象b = 5;

if(!a.Equals(b))

抛出新的InvalidOperationException(" a!= b");


但是为什么盒装对象不使用它b / b
包含的对象的Equals方法?
object a = 5;
object b = 5;
if (a != b)
throw new InvalidOperationException("a != b");

Why is the exception thrown? I guessed it is something to do with boxing
because the following does not throw an exception

object a = 5;
object b = 5;
if (!a.Equals(b))
throw new InvalidOperationException("a != b");

but why doesn''t the boxed object use the Equals method of the object it
contains?



除非它被重载,否则==运算符只是比较参考类型的对象

引用。在这里,你已经引用了两个

不同的对象,所以a!= b。但是,他们有价值平等,所以

a.Equals(b)。


-

Jon Skeet - < sk***@pobox.com>
http://www.pobox。 com / ~siget 博客: http://www.msmvps.com/ jon.skeet

如果回复小组,请不要给我发邮件

Unless it is overloaded, the == operator just compares object
references for reference types. Here, you''ve got references to two
different objects, so a != b. However, they have value equality, so
a.Equals(b).

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too


这篇关于谁可以给我解释一下这个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆