The method Equals and == give different result for the same object

T

Tony Johansson

Hi!

In this example give the Equals(comparing references) method true because it
finds the string hello in the string pool. So when looking at s2 it find
that we already have a hello in the pool and the program is using that one.
thay have

Now to the second example using the ==. The statement give false but this is
really strange why does not the program find the hello in the string pool in
the second example

Can somebody explain what is happenin here ?

static void Main(string[] args)
{
String s1 = "hello";
String s2 = "hell";
s2 += "o";
bool b = ((object)s1).Equals((object)s2); //Evaluates to true
bool b2 = ((object)s1) == ((object)s2); //Evaluates to false
}

//Tony

//Tony
 
J

Jeroen Mostert

In this example give the Equals(comparing references) method true because it
finds the string hello in the string pool. So when looking at s2 it find
that we already have a hello in the pool and the program is using that one.

This is not what's happening. The string pool is not involved in this story
at all. You are in fact best off forgetting about the string pool
altogether, as most programs don't need to bother with it.
Now to the second example using the ==. The statement give false but this is
really strange why does not the program find the hello in the string pool in
the second example

Can somebody explain what is happenin here ?

static void Main(string[] args)
{
String s1 = "hello";
String s2 = "hell";
s2 += "o";
bool b = ((object)s1).Equals((object)s2); //Evaluates to true

This invokes Object.Equals(), which is a virtual method overridden by
String, so this performs a string comparison. The strings are equal as they
contain the same characters in the same order.
bool b2 = ((object)s1) == ((object)s2); //Evaluates to false

Comparing Object instances with == does not call any method, instead it
performs a reference comparison of the objects. s1 is not the same object as
s2. See http://msdn.microsoft.com/library/362314fe.

If you want to involve the string pool, try this:

String s1 = "hello";
String s2 = "hell";
s2 += "o";
s2 = String.Intern(s2);

Now both comparisons will yield true, because s2 is the same object as s1
after interning (the compile-time constant "hello" is already interned --
and so are "hell" and "o", incidentally).
 
J

Jackie

Hi!

In this example give the Equals(comparing references) method true because it
finds the string hello in the string pool. So when looking at s2 it find
that we already have a hello in the pool and the program is using that one.
thay have

Now to the second example using the ==. The statement give false but this is
really strange why does not the program find the hello in the string pool in
the second example

Can somebody explain what is happenin here ?

static void Main(string[] args)
{
String s1 = "hello";
String s2 = "hell";
s2 += "o";
bool b = ((object)s1).Equals((object)s2); //Evaluates to true
bool b2 = ((object)s1) == ((object)s2); //Evaluates to false
}

//Tony

//Tony

From http://msdn.microsoft.com/en-us/library/ms173147(VS.80).aspx ->
 
H

Harlan Messinger

Tony said:
Hi!

In this example give the Equals(comparing references) method true because it
finds the string hello in the string pool. So when looking at s2 it find
that we already have a hello in the pool and the program is using that one.
thay have

Now to the second example using the ==. The statement give false but this is
really strange why does not the program find the hello in the string pool in
the second example

Can somebody explain what is happenin here ?

static void Main(string[] args)
{
String s1 = "hello";
String s2 = "hell";
s2 += "o";
bool b = ((object)s1).Equals((object)s2); //Evaluates to true
bool b2 = ((object)s1) == ((object)s2); //Evaluates to false
}

The method Equals is a virtual method, so even though ((object)s1) is an
object reference, string.Equals is used. The method string.Equals tests
whether its argument is a string and, if so, tests for value equality.
Therefore, the b is set to true.

The equality operator is not a virtual method. You are invoking

object1 == object2

which is a reference equality test. The fact that the two object
references ((object)s1) and ((object)s2) reference strings doesn't come
into it, so the string pool is completely irrelevant.
 
A

Arne Vajhøj

Hi!

In this example give the Equals(comparing references) method true because it
finds the string hello in the string pool. So when looking at s2 it find
that we already have a hello in the pool and the program is using that one.
thay have

Now to the second example using the ==. The statement give false but this is
really strange why does not the program find the hello in the string pool in
the second example

Can somebody explain what is happenin here ?

static void Main(string[] args)
{
String s1 = "hello";
String s2 = "hell";
s2 += "o";
bool b = ((object)s1).Equals((object)s2); //Evaluates to true
bool b2 = ((object)s1) == ((object)s2); //Evaluates to false
}

You have "hello", "hell" and "o" in the string pool.

You are not comparing one object in the string pool with
one that is not.

Arne
 
A

Arne Vajhøj

Hi!

In this example give the Equals(comparing references) method true
because it
finds the string hello in the string pool. So when looking at s2 it find
that we already have a hello in the pool and the program is using that
one.
thay have

Now to the second example using the ==. The statement give false but
this is
really strange why does not the program find the hello in the string
pool in
the second example

Can somebody explain what is happenin here ?

static void Main(string[] args)
{
String s1 = "hello";
String s2 = "hell";
s2 += "o";
bool b = ((object)s1).Equals((object)s2); //Evaluates to true
bool b2 = ((object)s1) == ((object)s2); //Evaluates to false
}

You have "hello", "hell" and "o" in the string pool.

You are not comparing one object in the string pool with
one that is not.

Discard one of the not's.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top