|
ms
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Value equalityunderstand 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 On 2008-12-14, tshad <t**@dslextreme.com> wrote:
Show quoteHide quote > I was reading about referential equality vs value equality and don't Unless you have overridden Equals, then the default of implementation of> 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 Equals is to compare references... "Value Equality" is something you have to implement :) -- Tom Shelton did you try ReferenceEquals(obj1, obj2)?
Show quoteHide quote "tshad" <t**@dslextreme.com> wrote in message news:OgCJMlZXJHA.5108@TK2MSFTNGP04.phx.gbl... >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 > > > >
Show quote
Hide quote
"tshad" <t**@dslextreme.com> wrote in message Here is a class exemplifying Tom's answer.news:OgCJMlZXJHA.5108@TK2MSFTNGP04.phx.gbl... >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 > > > > 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(); } } -- Mike 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; //Be aware that you'd have to check Name for != null> return this.Name == ((MyClass2) obj).Name; > } > public override int GetHashCode() > { //in the object's constructor > return this.Name.GetHashCode(); > } > }
Other interesting topics
Array - Does it exist?
How to close a StreamWriter class when the program exits UserControl, GUI inside the application on runing time, PlugIn problem Bitmpa.Save & GZipStream - error linq --> changing a value? Filewatcher - what happens if a file is created while processing another one? validating xml file methods Connection string/Sql Server 2005/Windows authentication/ but not on domain Null dynamic data in .net 3.5 SP1 -- performance, multi-tier |
|||||||||||||||||||||||