Home All Groups Group Topic Archive Search About
Author
14 Dec 2008 3:08 AM
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

Author
14 Dec 2008 3:18 AM
Tom Shelton
On 2008-12-14, tshad <t**@dslextreme.com> wrote:
Show quoteHide quote
> 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 :)

--
Tom Shelton
Are all your drivers up to date? click for free checkup

Author
14 Dec 2008 3:25 AM
Berryl Hesh
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
>
>
>
>
Author
14 Dec 2008 3:34 AM
Family Tree Mike
Show quote Hide 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
>
>
>
>


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();
  }
}



--
Mike
Author
14 Dec 2008 10:43 AM
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

>    return this.Name.GetHashCode();
>  }
> }





Bookmark and Share