tags:

views:

83

answers:

3

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 */
  }
}
+5  A: 

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.

Jon Skeet
I actually considered testing it like that but figured someone would just know off the cuff (and then SO went weird and I got obsessed with making it post). Thanks, I guess.
Peter Wone
+2  A: 

Reflector shows that Binary.Equals compares by real binary value, not by the reference.

skevar7
u beat me to it!
Abhijeet Patel
+2  A: 

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;

}

Abhijeet Patel