Reputation: 18805
The Microsoft documentation for
public bool Binary.Equals(Binary other)
gives no indication as to whether this tests equality of reference as with objects in general or equality of value as with strings.
Can anyone clarify?
John Skeet's answer inspired me to expand it to this:
using System;
using System.Data.Linq;
public class Program
{
static void Main(string[] args)
{
Binary a = new Binary(new byte[] { 1, 2, 3 });
Binary b = new Binary(new byte[] { 1, 2, 3 });
Console.WriteLine("a.Equals(b) >>> {0}", a.Equals(b));
Console.WriteLine("a {0} == b {1} >>> {2}", a, b, a == b);
b = new Binary(new byte[] { 1, 2, 3, 4 });
Console.WriteLine("a {0} == b {1} >>> {2}",a,b, a == b);
/* a < b is not supported */
}
}
Upvotes: 2
Views: 732
Reputation: 6878
It's a value comparison per Reflector...
private bool EqualsTo(Binary binary)
{
if (this != binary)
{
if (binary == null)
{
return false;
}
if (this.bytes.Length != binary.bytes.Length)
{
return false;
}
if (this.hashCode != binary.hashCode)
{
return false;
}
int index = 0;
int length = this.bytes.Length;
while (index < length)
{
if (this.bytes[index] != binary.bytes[index])
{
return false;
}
index++;
}
}
return true;
}
Upvotes: 3
Reputation: 1005
Reflector shows that Binary.Equals compares by real binary value, not by the reference.
Upvotes: 2
Reputation: 1503140
Well, a simple test suggests it is value equality:
using System;
using System.Data.Linq;
class Program {
static void Main(string[] args)
{
Binary a = new Binary(new byte[] { 1, 2, 3 });
Binary b = new Binary(new byte[] { 1, 2, 3 });
Console.WriteLine(a.Equals(b)); // Prints True
}
}
The fact that they've bothered to implement IEquatable<Binary>
and override Equals(object)
to start with suggests value equality semantics too... but I agree that the docs should make this clear.
Upvotes: 6