Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Equals method on Binary object

Tags:

c#

.net

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 */
  }
}
like image 244
Peter Wone Avatar asked Dec 07 '25 08:12

Peter Wone


1 Answers

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.

like image 151
Jon Skeet Avatar answered Dec 12 '25 10:12

Jon Skeet



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!