Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You certainly can't do operator overloading with extension methods. The reason it doesn't work for the <code>Equals</code> method is that if <em>any</em> method is applicable without using extension methods, that method will be chosen before extension methods are even examined.</p> <p>Even though your <code>Equals</code> method is "better" in terms of converting the argument types to the formal parameter types, the compiler always prefers "normal" methods. You'll have to give your method a different name.</p> <p>However, you can always use the <code>Enumerable.SequenceEquals</code> method. I don't believe that short-circuits the length check though (even though it could, for <code>ICollection&lt;T&gt;</code> implementations). You could always implement a more efficient version yourself though. Indeed, if you just change your existing array implementation to be called <code>SequenceEquals</code> or even <code>ArrayEquals</code>, that would be fine:</p> <pre><code>public static bool ArrayEquals(this byte[] array, byte[] bytes) { // I'd personally use braces in all of this, but it's your call if (array.Length != bytes.Length) return false; for (int i = 0; i &lt; array.Length; i++) if (array[i] != bytes[i]) return false; return true; } </code></pre> <p>Note that it would be quite nice to make it generic, but that would certainly cost a bit of performance as the comparison couldn't be inlined:</p> <pre><code>public static bool ArrayEquals&lt;T&gt;(this T[] first, T[] second) { // Reference equality and nullity checks for safety and efficiency if (first == second) { return true; } if (first == null || second == null) { return false; } if (first.Length != second.Length) { return false; } EqualityComparer&lt;T&gt; comparer = EqualityComparer&lt;T&gt;.Default; for (int i = 0; i &lt; first.Length; i++) { if (!comparer.Equals(first[i], second[i])) { return false; } } return true; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload