Hi all, I'm having problems comparing array objects. Take a look at this:

int[] array1 = new int[]{1};
int[] array2 = new int[]{1};
Console.Writeln(array1.Equals(array2));

One would expect the above expression to return true as both arrays are
identically the same but it keeps returning false. Any info on how to solve
this problem will be appreciated.



--
Regards,
Raphael Iloh (www.ilohraphael.net)
MCP, MCAD (C#)
This Posting is provided "AS IS", with no warranties and confers no rights.

Re: Comparing Array Objects by Alvin

Alvin
Sun Feb 22 10:56:50 CST 2004

No the objects are not equal. Equals implements reference equality not value
equality. You are essentially asking whether or not object 1 and object 2
live at the same address. They don't. They possibly live across the street
from each other but not in the same house. What you are searching for is
value equality. This method can be overridden by a derived class.

--
Regards,
Alvin Bruney [ASP.NET MVP]
Got tidbits? Get it here...
http://tinyurl.com/3he3b
"Raphael Iloh" <ilohraphael@NOSPAMhotmail.com> wrote in message
news:OeGEW7V%23DHA.3176@TK2MSFTNGP11.phx.gbl...
> Hi all, I'm having problems comparing array objects. Take a look at this:
>
> int[] array1 = new int[]{1};
> int[] array2 = new int[]{1};
> Console.Writeln(array1.Equals(array2));
>
> One would expect the above expression to return true as both arrays are
> identically the same but it keeps returning false. Any info on how to
solve
> this problem will be appreciated.
>
>
>
> --
> Regards,
> Raphael Iloh (www.ilohraphael.net)
> MCP, MCAD (C#)
> This Posting is provided "AS IS", with no warranties and confers no
rights.
>
>



Re: Comparing Array Objects by Raphael

Raphael
Mon Feb 23 05:16:16 CST 2004

...thanks Alvin but your explanation falls short given the following
example:

DateTime date1 = new DateTime(2004, 02, 24);
DateTime date2 = new DateTime(2004, 02, 24);
Console.WriteLine(date1.Equals(date2));

DateTime is a value object and date1 and date2 were instantiated
exclusively, yet the outcome is always true.The same applies to integers,
booleans, etc; all value objects. Rather the problem I'd raised earlier on
is regarding Array objects specifically and we both know Array objects are
reference objects.

Regards, Raphael.


"Alvin Bruney [MVP]" <vapor at steaming post office> wrote in message
news:eVsmlTW%23DHA.4088@tk2msftngp13.phx.gbl...
> No the objects are not equal. Equals implements reference equality not
value
> equality. You are essentially asking whether or not object 1 and object 2
> live at the same address. They don't. They possibly live across the street
> from each other but not in the same house. What you are searching for is
> value equality. This method can be overridden by a derived class.
>
> --
> Regards,
> Alvin Bruney [ASP.NET MVP]
> Got tidbits? Get it here...
> http://tinyurl.com/3he3b
> "Raphael Iloh" <ilohraphael@NOSPAMhotmail.com> wrote in message
> news:OeGEW7V%23DHA.3176@TK2MSFTNGP11.phx.gbl...
> > Hi all, I'm having problems comparing array objects. Take a look at
this:
> >
> > int[] array1 = new int[]{1};
> > int[] array2 = new int[]{1};
> > Console.Writeln(array1.Equals(array2));
> >
> > One would expect the above expression to return true as both arrays are
> > identically the same but it keeps returning false. Any info on how to
> solve
> > this problem will be appreciated.
> >
> >
> >
> > --
> > Regards,
> > Raphael Iloh (www.ilohraphael.net)
> > MCP, MCAD (C#)
> > This Posting is provided "AS IS", with no warranties and confers no
> rights.
> >
> >
>
>



Re: Comparing Array Objects by Jon

Jon
Mon Feb 23 05:46:34 CST 2004

Raphael Iloh <ilohraphael_NOSPAM@hotmail.com> wrote:
> ...thanks Alvin but your explanation falls short given the following
> example:
>
> DateTime date1 = new DateTime(2004, 02, 24);
> DateTime date2 = new DateTime(2004, 02, 24);
> Console.WriteLine(date1.Equals(date2));
>
> DateTime is a value object and date1 and date2 were instantiated
> exclusively, yet the outcome is always true.The same applies to integers,
> booleans, etc; all value objects. Rather the problem I'd raised earlier on
> is regarding Array objects specifically and we both know Array objects are
> reference objects.

I don't think there's much benefit in bringing value types into the
equation here - they have equality tests in a slightly different way
(IIRC, something examines the fields with reflection).

The real reason is that Array doesn't override the Equals method
(unlike, say, String).

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Re: Comparing Array Objects by Michael

Michael
Tue Feb 24 04:31:54 CST 2004


"Jon Skeet [C# MVP]" <skeet@pobox.com> wrote in message
news:MPG.1aa3f876e1ef241798a1d6@msnews.microsoft.com...
> Raphael Iloh <ilohraphael_NOSPAM@hotmail.com> wrote:
> > ...thanks Alvin but your explanation falls short given the following
> > example:

> The real reason is that Array doesn't override the Equals method
> (unlike, say, String).

This is the real explaination. One should never assume that comparison are
value-based. Hence that two different arrays are not equal, is working as
designed.

However, on a side-note, when realizing a ValueType class, like a struct in
C#; forgetting to override Equals, GetHashCode and ToString should be a
criminal offence. =)

- Michael S

ps.
Keep up the good work Skeet. I like you posts....



Re: Comparing Array Objects by Alvin

Alvin
Tue Feb 24 12:29:24 CST 2004

> However, on a side-note, when realizing a ValueType class, like a struct
in
> C#; forgetting to override Equals, GetHashCode and ToString should be a
> criminal offence. =)

No. not true at all.

An application even as gracious as the framework cannot uncover every
programmer's intent. In this regard, overriding must be a recommendation
albeit a strong one. But it cannot be manditory as in a criminal offense
which is what you are implying. Throughout the programming life cycle there
may be times when overriding is not needed even for valuetype classes. A
framework needs to be as flexible as possible which is why overriding is
always optional.

--
Regards,
Alvin Bruney [ASP.NET MVP]
Got tidbits? Get it here...
http://tinyurl.com/3he3b
"Michael S" <a@b.c> wrote in message
news:uWFhtFs%23DHA.2524@tk2msftngp13.phx.gbl...
>
> "Jon Skeet [C# MVP]" <skeet@pobox.com> wrote in message
> news:MPG.1aa3f876e1ef241798a1d6@msnews.microsoft.com...
> > Raphael Iloh <ilohraphael_NOSPAM@hotmail.com> wrote:
> > > ...thanks Alvin but your explanation falls short given the following
> > > example:
>
> > The real reason is that Array doesn't override the Equals method
> > (unlike, say, String).
>
> This is the real explaination. One should never assume that comparison are
> value-based. Hence that two different arrays are not equal, is working as
> designed.
>
> However, on a side-note, when realizing a ValueType class, like a struct
in
> C#; forgetting to override Equals, GetHashCode and ToString should be a
> criminal offence. =)
>
> - Michael S
>
> ps.
> Keep up the good work Skeet. I like you posts....
>
>