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
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