Re: Explanation of object equality.
- From: Jeff Louie <anonymous@xxxxxxxxxx>
- Date: Thu, 11 Sep 2008 22:08:22 -0700
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
object o3 = "hello";// How about with strings?
object o4 = "hello";
Console.WriteLine(o3 == o4); // returns true<<
*** Sent via Developersdex http://www.developersdex.com ***
.
- Follow-Ups:
- Re: Explanation of object equality.
- From: Peter Duniho
- Re: Explanation of object equality.
- References:
- Explanation of object equality.
- From: DamienS
- Explanation of object equality.
- Prev by Date: Re: When Overloading the Plus Operator, What are Valid Arguments Types?
- Next by Date: Re: Should you simplify this code? Write only code
- Previous by thread: Re: Explanation of object equality.
- Next by thread: Re: Explanation of object equality.
- Index(es):
Relevant Pages
|