Problem with byte Array compare

N

nobs

Hi

I have two byte arrays (each with a length of 8 bytes)
an I'd like to compare them if they are equal.

The operator == shouldn't work because its not a basic Data Type
so I used the method Equal, but it also isn't working.

right know I use
if(mykey.Length!= keytocheck.Length)
return false;
for (int i = 0; i < mykey.Length; i++)
{
if (mykey != keytocheck)
return false;
}

This is working, but why I cannot use the Equal method??
Can somebody explain it to me?

regards
Norbert
 
N

Nicholas Paldino [.NET/C# MVP]

Norbert,

Quite simply, the Equals method is not overridden to provide the
semantics you desire. You will have to perform this check, or wrap it in a
utility method if this is the result you desire.

Hope this helps.
 
J

Jon Skeet [C# MVP]

nobs said:
I have two byte arrays (each with a length of 8 bytes)
an I'd like to compare them if they are equal.

The operator == shouldn't work because its not a basic Data Type

Well, if == had been overloaded for byte arrays, that would still work
just fine.
so I used the method Equal, but it also isn't working.

right know I use
if(mykey.Length!= keytocheck.Length)
return false;
for (int i = 0; i < mykey.Length; i++)
{
if (mykey != keytocheck)
return false;
}

This is working, but why I cannot use the Equal method??


Because Equals hasn't been overridden to compare arrays. The above
looks fine though.
 

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