Is there a way to compare array values without going through each element?

J

Jim H

I will have many 3 byte Byte arrays. Is there a way to compare the values
without iterating through and comparing each element in the code?

Example:
byte[] lTest1 = new byte[3] {0x1, 0x2, 0x3};
byte[] lTest2 = new byte[3] {0x1, 0x2, 0x3};
byte[] lTest3 = new byte[3] {0x1, 0x5, 0x3};
if(Comparer.Equals(lTest1, lTest2))
System.Diagnostics.Trace.WriteLine("Test1 and Test2 are the same",
"Debug");
else
System.Diagnostics.Trace.WriteLine("Test1 and Test2 are the different",
"Debug");
if(lTest1 == lTest2)
System.Diagnostics.Trace.WriteLine("Test1 and Test2 are the same",
"Debug");
else
System.Diagnostics.Trace.WriteLine("Test1 and Test2 are the different",
"Debug");if(lTest1 == lTest3)
if(lTest1 == lTest3)
System.Diagnostics.Trace.WriteLine("Test1 and Test3 are the same",
"Debug");
else
System.Diagnostics.Trace.WriteLine("Test1 and Test3 are the different",
"Debug");


These all print out TestX and TestY are different. Are there any classes or
methods for comparing arrays by their value?

Thanks,
jim
 
A

Adrian

Can't you put the elements behind each other in a string and then just
compare the strings?
 
J

Jon Skeet [C# MVP]

Adrian said:
Can't you put the elements behind each other in a string and then just
compare the strings?

If you're going to do that, you might as well save the extra memory etc
and just compare the byte arrays yourself.

There's nothing in the framework to compare the contents of arrays,
although it's easy to write yourself.
 
T

Tom Dacon

This may be a good occasion to develop your own type to hold the byte data,
rather than using the standard Array provided by C#. The Array class itself
can't be subclassed by applications, and things like ArrayLists may be too
heavyweight, but I'd bet that you could easily create a small and simple
class (or structure, if that suited how these things' lifetimes are managed)
that exposes a Compare method that does just what you're after. Perhaps the
class might contain an array to hold the actual data, and could then provide
an indexer to make it behave much like a simple array.

Regards,
Tom Dacon
Dacon Software Consulting
 
J

Jon Skeet [C# MVP]

Tom Dacon said:
This may be a good occasion to develop your own type to hold the byte data,
rather than using the standard Array provided by C#. The Array class itself
can't be subclassed by applications, and things like ArrayLists may be too
heavyweight, but I'd bet that you could easily create a small and simple
class (or structure, if that suited how these things' lifetimes are managed)
that exposes a Compare method that does just what you're after. Perhaps the
class might contain an array to hold the actual data, and could then provide
an indexer to make it behave much like a simple array.

For most purposes, I suspect it would be simpler to implement IComparer
in a way which can compare the byte arrays. It depends on exactly what
the requirements are, of course.
 
J

Jim H

Thanks everyone. I guess I'll have to do it myself. I know it's not hard
but I thought there might have been something equivelant to memcmp() in
C/C++. No big deal. I'm just getting used to the .NET framework and it
will take a while for me to get used to what's provided and what's not.

Thanks again,
jim
 

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