Value equality

T

tshad

I was reading about referential equality vs value equality and don't
understand why I am always getting value equality wrong on 2 objects that I
just created. The values should be the same (the referential equalities
should be different).

If I have:

Type type1 = typeof(MyClass2);
//Create an instance of the type
object obj = Activator.CreateInstance(type1);
object obj2 = new MyClass2();
object obj3 = new MyClass2();

I would have expected all 3 of these objects to be equal value wise. But
they aren't (at least not as I can tell).

If I do this right afterword:

if (obj == obj2)
textBox1.Text += "obj Referentially equal obj2" +
Environment.NewLine;
else
textBox1.Text += "obj not Referentially equal obj2" +
Environment.NewLine;

if (obj.Equals(obj2))
textBox1.Text += "obj has value equality to obj2" +
Environment.NewLine;
else
textBox1.Text += "obj not have value equality to obj2" +
Environment.NewLine;

if (obj2.Equals(obj3))
textBox1.Text += "obj2 has value equality to obj3" +
Environment.NewLine;
else
textBox1.Text += "obj2 not have value equality to obj3" +
Environment.NewLine;

I get not equal for all of these.

I expect the 1st one not to be equal, but why would the second and third one
be equal. Especially the third one where they are created exactly the same
way???

The results I get are:

obj not Referentially equal obj2
obj not have value equality to obj2
obj2 not have value equality to obj3

Why is that????

Thanks,

Tom
 
T

Tom Shelton

I was reading about referential equality vs value equality and don't
understand why I am always getting value equality wrong on 2 objects that I
just created. The values should be the same (the referential equalities
should be different).

If I have:

Type type1 = typeof(MyClass2);
//Create an instance of the type
object obj = Activator.CreateInstance(type1);
object obj2 = new MyClass2();
object obj3 = new MyClass2();

I would have expected all 3 of these objects to be equal value wise. But
they aren't (at least not as I can tell).

If I do this right afterword:

if (obj == obj2)
textBox1.Text += "obj Referentially equal obj2" +
Environment.NewLine;
else
textBox1.Text += "obj not Referentially equal obj2" +
Environment.NewLine;

if (obj.Equals(obj2))
textBox1.Text += "obj has value equality to obj2" +
Environment.NewLine;
else
textBox1.Text += "obj not have value equality to obj2" +
Environment.NewLine;

if (obj2.Equals(obj3))
textBox1.Text += "obj2 has value equality to obj3" +
Environment.NewLine;
else
textBox1.Text += "obj2 not have value equality to obj3" +
Environment.NewLine;

I get not equal for all of these.

I expect the 1st one not to be equal, but why would the second and third one
be equal. Especially the third one where they are created exactly the same
way???

The results I get are:

obj not Referentially equal obj2
obj not have value equality to obj2
obj2 not have value equality to obj3

Why is that????

Thanks,

Tom

Unless you have overridden Equals, then the default of implementation of
Equals is to compare references... "Value Equality" is something you have to
implement :)
 
F

Family Tree Mike

tshad said:
I was reading about referential equality vs value equality and don't
understand why I am always getting value equality wrong on 2 objects that I
just created. The values should be the same (the referential equalities
should be different).

If I have:

Type type1 = typeof(MyClass2);
//Create an instance of the type
object obj = Activator.CreateInstance(type1);
object obj2 = new MyClass2();
object obj3 = new MyClass2();

I would have expected all 3 of these objects to be equal value wise. But
they aren't (at least not as I can tell).

If I do this right afterword:

if (obj == obj2)
textBox1.Text += "obj Referentially equal obj2" +
Environment.NewLine;
else
textBox1.Text += "obj not Referentially equal obj2" +
Environment.NewLine;

if (obj.Equals(obj2))
textBox1.Text += "obj has value equality to obj2" +
Environment.NewLine;
else
textBox1.Text += "obj not have value equality to obj2" +
Environment.NewLine;

if (obj2.Equals(obj3))
textBox1.Text += "obj2 has value equality to obj3" +
Environment.NewLine;
else
textBox1.Text += "obj2 not have value equality to obj3" +
Environment.NewLine;

I get not equal for all of these.

I expect the 1st one not to be equal, but why would the second and third
one be equal. Especially the third one where they are created exactly the
same way???

The results I get are:

obj not Referentially equal obj2
obj not have value equality to obj2
obj2 not have value equality to obj3

Why is that????

Thanks,

Tom


Here is a class exemplifying Tom's answer.

public class MyClass2
{
private string Name { get; set; }
public override bool Equals(object obj)
{
if (obj.GetType() != this.GetType()) return false;
return this.Name == ((MyClass2) obj).Name;
}
public override int GetHashCode()
{
return this.Name.GetHashCode();
}
}
 
P

Peter Morris

I would make the following changes...
public class MyClass2
{

public string Name { get; private set; }

public override bool Equals(object obj)
{

if (obj == null)
return false;
if (obj.GetType() != this.GetType()) return false;
return this.Name == ((MyClass2) obj).Name;
}
public override int GetHashCode()
{

//Be aware that you'd have to check Name for != null
//in the object's constructor
 

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