为什么这两个比较结果不同? [英] Why do these two comparisons have different results?

查看:90
本文介绍了为什么这两个比较结果不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么此代码返回true:

  new Byte()== new Byte()//返回true 

但是此代码返回false:

 新字节[0] ==新字节[0] //返回假


解决方案

因为 new Byte()创建值类型,将其与值进行比较(默认情况下将返回字节,值 0 )。然后 new Byte [0] 创建一个数组,它是一个引用类型,并按引用进行比较(这两个数组实例将具有不同的引用)。



有关详细信息,请参见值类型和引用类型文章。 / p>

Why does this code return true:

new Byte() == new Byte()   // returns true

but this code returns false:

new Byte[0] == new Byte[0] // returns false

解决方案

Because new Byte() creates value type, which are compared by value (by default it will return byte with value 0). And new Byte[0] creates array, which is a reference type and compared by reference (and these two instances of array will have different references).

See Value Types and Reference Types article for details.

这篇关于为什么这两个比较结果不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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