there’s two methods i was looking at @stackOverFlow , i guess there might be even more of them
somewhere else , my qusetion is what is the best performance wise
and second question , i have this code that simply takes a couple of byte[]
bool ArraysEqual(byte[] a1, byte[] a2)
{
if (a1 == a2)
return true;
if (a1 == null || a2 == null)
return false;
if (a1.Length != a2.Length)
return false;
for (int i = 0; i < a1.Length; i++)
{
if (a1[i] != a2[i])
return false;
}
return true;
}
and i couldn’t implement this one , not kowing the workaroud iguess i’ve used the wrong syntax
so if i have a helper method to read a byet[] file
public byte[] readByteArr(string FilePath)
{
return File.ReadAllBytes(FilePath);
}
i could make it simply via
void CompareIt(){
byte[] src = readByteArr(S.bar);
byte[] dest = readByteArr(D.bar);
if(ArraysEqual(src, dest))
DoSomthing
}
how do i make CompareIt(), with sets of parameters requierd in this Second code i allso try to implement, just to check which performs beeter
bool ArraysEqual<T>(T[] a1, T[] a2)
{
if (ReferenceEquals(a1,a2))
return true;
if (a1 == null || a2 == null)
return false;
if (a1.Length != a2.Length)
return false;
EqualityComparer<T> comparer = EqualityComparer<T>.Default;
for (int i = 0; i < a1.Length; i++)
{
if (!comparer.Equals(a1[i], a2[i]))
return false;
}
return true;
}
what is the right syntax to implement this last one, and if there’s a quicker way to compare two
byte[]
knowing they’re the same length .
and what do you think about this ?
ReEditing:

the first is unsafe code .
second is first code i wrote (bool ArraysEqual())
and third is
myArray.SequenceEqual(otherArray);
Now Take A good look at this
please Do allow me to announce, the winner is :
[DllImport("msvcrt.dll", CallingConvention = CallingConvention.Cdecl)]
static extern int memcmp(byte[] b1, byte[] b2, long count);
static bool ByteArrayCompare(byte[] b1, byte[] b2)
{
// Validate buffers are the same length.
// This also ensures that the count does not exceed the length of either buffer.
return b1.Length == b2.Length && memcmp(b1, b2, b1.Length) == 0;
}
@Chaos . Can u please post the implemetation of Your Random with my Pinvoke ?

if you use .NET > 3.0:
see MSDN – Enumerable.SequenceEqual
if you want to use a custom “EqualityComparer” you can use MSDN – Enumerable.SequenceEqual with IEqualityComparer
😉