对象相等的解释。 [英] Explanation of object equality.

查看:68
本文介绍了对象相等的解释。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了节省我的头发,有人可以向我解释

下面发生了什么?当

作为对象投射时,为什么没有==比较两个int'?它们是同一种类型。


请注意它适用于字符串。


提前致谢,

Damien

---------------

使用系统;


命名空间ConsoleApplication1

{

class program

{

static void Main(string [] args)

{

test();

}


static public void test()

{

object o1 = 5;

object o2 = 5;

Console.WriteLine(o1 == o2); //返回false - 这是为什么?

Console.WriteLine(o1.Equals(o2)); //返回true


//检查它们是否属于同一类型

Console.WriteLine(o1.GetType()。FullName); //返回

System.Int32

Console.WriteLine(o2.GetType()。FullName); //返回

System.Int32

//尝试专门施放

Console.WriteLine(Convert.ToInt32(o1)= =

Convert.ToInt32(o2)); //返回true


//字符串怎么样?

object o3 =" hello";

object o4 = " hello";

Console.WriteLine(o3 == o4); //返回true

Console.WriteLine(o3.Equals(o4)); //返回true


Console.ReadLine();

}

}

}

In the interests of me saving hair, can someone please explain to me
what''s going on below? Why doesn''t == work in comparing two int''s when
cast as objects? They''re the same type.

Note that it worked for strings.

Thanks in advance,
Damien
---------------
using System;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
test();
}

static public void test()
{
object o1 = 5;
object o2 = 5;
Console.WriteLine(o1==o2); // returns false - why is this?
Console.WriteLine(o1.Equals(o2)); // returns true

// Check they''re the same type
Console.WriteLine(o1.GetType().FullName); // Returns
System.Int32
Console.WriteLine(o2.GetType().FullName); // Returns
System.Int32

// try specifically casting
Console.WriteLine(Convert.ToInt32(o1) ==
Convert.ToInt32(o2)); // returns true

// How about with strings?
object o3 = "hello";
object o4 = "hello";
Console.WriteLine(o3 == o4); // returns true
Console.WriteLine(o3.Equals(o4)); // returns true

Console.ReadLine();
}
}
}

推荐答案

2008年9月11日星期四21:24:24 -0700,DamienS< da ********** @ yahoo .com.au>

写道:
On Thu, 11 Sep 2008 21:24:24 -0700, DamienS <da**********@yahoo.com.au>
wrote:

为了节省我的头发,有人可以向我解释

下面发生了什么?当

作为对象投射时,为什么没有==比较两个int'?他们是同一类型。
In the interests of me saving hair, can someone please explain to me
what''s going on below? Why doesn''t == work in comparing two int''s when
cast as objects? They''re the same type.



但是Object类的==运算符并没有调用Object.Equals()。它只是一个直接的参考平等测试。

。由于每个变量都引用了一个不同的实例,因此引用不相等,因此测试失败。

But the Object class''s == operator doesn''t call Object.Equals(). It''s
just a straight reference equality test. Since each variables refers to a
different instance, the references aren''t equal and so the test fails.


注意它是有效的对于字符串。
Note that it worked for strings.



因为String类==运算符重载基本上调用了

Equals()方法。


Pete

Because the String class == operator overload does essentially call the
Equals() method.

Pete


Damien ......小心点。正如Jon在过去指出的那样,

重载

字符串相等运算符仅适用于类型为

string的引用变量。


对象a =" Hello";

对象b =" Hello" ;;

bool isSameReference =(a == b) ; //测试a和b是否包含引用



相同对象或没有对象(均为null)

bool isSameContent =( (字符串)a ==(字符串)b); //测试是否字符串

引用

和引用的字符串b具有相同的内容/值或者a和b都是

null


会话[" KEY"] =" VALUE" ;; //错误!左手操作数的类型为

object!

这是一个基于参考的比较。这样做:

(String)Session [" KEY"] =" VALUE" ;; //内容等价


问候,

杰夫
Damien... Be careful. As Jon has pointed out in the past, the
overloaded
string equality operator only works on reference variables of Type
string.

object a= "Hello";
object b= "Hello";
bool isSameReference= (a == b); // test if a and b contain references to
the
same object or to no object (are both null)
bool isSameContent= ((string)a == (string)b); // test if string
referenced by a
and string referenced b have the same content/value or a and b are both
null

Session["KEY"]="VALUE"; // Error! The left hand operand is of type
object!
This is a reference based comparison. Do this:
(String)Session["KEY"]="VALUE"; // content equivalence

Regards,
Jeff

>> //字符串怎么样?
>>// How about with strings?



object o3 =" hello";

object o4 =" hello";

Console.WriteLine(o3 == o4); //返回true<<

***通过Developersdex发送 http:/ /www.developersdex.com ***

object o3 = "hello";
object o4 = "hello";
Console.WriteLine(o3 == o4); // returns true<<
*** Sent via Developersdex http://www.developersdex.com ***


2008年9月11日星期四22:08:22 -0700,Jeff Louie< an *** ****@devdex.com>

写道:
On Thu, 11 Sep 2008 22:08:22 -0700, Jeff Louie <an*******@devdex.com>
wrote:

Damien ...小心。正如Jon在过去指出的那样,

重载

字符串相等运算符仅适用于类型

string的引用变量。
Damien... Be careful. As Jon has pointed out in the past, the
overloaded
string equality operator only works on reference variables of Type
string.



请注意,对于任何重载运算符都是如此。也就是说,

运算符是根据

操作数的静态(编译时)类型选择的。小心的警告是一个很好的警告,并且在使用_any_重载操作符时值得记住




Pete

Note that this is true for any overloaded operators. That is, the
operator is chosen according to the static (compile-time) type of the
operand(s). The warning to be careful is a good one, and worth keeping in
mind when using _any_ overloaded operators.

Pete


这篇关于对象相等的解释。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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