<p>The following will cause infinite recursion on the == operator overload method</p>

<pre><code>    Foo foo1 = null;
    Foo foo2 = new Foo();
    Assert.IsFalse(foo1 == foo2);

    public static bool operator ==(Foo foo1, Foo foo2) {
        if (foo1 == null) return foo2 == null;
        return foo1.Equals(foo2);
    }
</code></pre>

<p>How do I check for nulls?</p>

[{'answer_id': 73729, 'author': 'Andrew Jones', 'author_id': 12767, 'author_profile': 'https://Stackoverflow.com/users/12767', 'pm_score': 4, 'selected': False, 'text': '<p>Cast to object in the overload method:</p>\n\n<pre><code>public static bool operator ==(Foo foo1, Foo foo2) {\n    if ((object) foo1 == null) return (object) foo2 == null;\n    return foo1.Equals(foo2);\n}\n</code></pre>\n'}, {'answer_id': 73732, 'author': 'Abe Heidebrecht', 'author_id': 9268, 'author_profile': 'https://Stackoverflow.com/users/9268', 'pm_score': 8, 'selected': True, 'text': '<p>Use <code>ReferenceEquals</code>:</p>\n\n<pre><code>Foo foo1 = null;\nFoo foo2 = new Foo();\nAssert.IsFalse(foo1 == foo2);\n\npublic static bool operator ==(Foo foo1, Foo foo2) {\n    if (object.ReferenceEquals(null, foo1))\n        return object.ReferenceEquals(null, foo2);\n    return foo1.Equals(foo2);\n}\n</code></pre>\n'}, {'answer_id': 73744, 'author': 'Santiago Palladino', 'author_id': 12791, 'author_profile': 'https://Stackoverflow.com/users/12791', 'pm_score': 2, 'selected': False, 'text': '<p>Try <code>Object.ReferenceEquals(foo1, null)</code></p>\n\n<p>Anyway, I wouldn\'t recommend overloading the <code>==</code>operator; it should be used for comparing references, and use <code>Equals</code> for "semantic" comparisons.</p>\n'}, {'answer_id': 73840, 'author': 'Jon Adams', 'author_id': 2291, 'author_profile': 'https://Stackoverflow.com/users/2291', 'pm_score': 3, 'selected': False, 'text': '<p>Use <code><a href="http://msdn.microsoft.com/en-us/library/system.object.referenceequals(VS.80).aspx" rel="noreferrer">ReferenceEquals</a></code>. From the <a href="http://social.msdn.microsoft.com/Forums/en-US/csharplanguage/thread/472d7dfe-9233-49d1-a545-de44069614b5/#ccef9e05-d609-4f01-9027-b84ea7f6a41d" rel="noreferrer">MSDN forums</a>:</p>\n\n<pre><code>public static bool operator ==(Foo foo1, Foo foo2) {\n    if (ReferenceEquals(foo1, null)) return ReferenceEquals(foo2, null);\n    if (ReferenceEquals(foo2, null)) return false;\n    return foo1.field1 == foo2.field2;\n}\n</code></pre>\n'}, {'answer_id': 73915, 'author': 'The Digital Gabeg', 'author_id': 12782, 'author_profile': 'https://Stackoverflow.com/users/12782', 'pm_score': -1, 'selected': False, 'text': '<p>You can try to use an object property and catch the resulting NullReferenceException. If the property you try is inherited or overridden from Object, then this works for any class.</p>\n\n<pre><code>public static bool operator ==(Foo foo1, Foo foo2)\n{\n    //  check if the left parameter is null\n    bool LeftNull = false;\n    try { Type temp = a_left.GetType(); }\n    catch { LeftNull = true; }\n\n    //  check if the right parameter is null\n    bool RightNull = false;\n    try { Type temp = a_right.GetType(); }\n    catch { RightNull = true; }\n\n    //  null checking results\n    if (LeftNull &amp;&amp; RightNull) return true;\n    else if (LeftNull || RightNull) return false;\n    else return foo1.field1 == foo2.field2;\n}\n</code></pre>\n'}, {'answer_id': 81750, 'author': 'Hallgrim', 'author_id': 15454, 'author_profile': 'https://Stackoverflow.com/users/15454', 'pm_score': 2, 'selected': False, 'text': '<p>If I have overridden <code>bool Equals(object obj)</code> and I want the operator <code>==</code> and <code>Foo.Equals(object obj)</code> to return the same value, I usually implement the <code>!=</code> operator like this:</p>\n<pre><code>public static bool operator ==(Foo foo1, Foo foo2) {\n  return object.Equals(foo1, foo2);\n}\npublic static bool operator !=(Foo foo1, Foo foo2) {\n  return !object.Equals(foo1, foo2);\n}\n</code></pre>\n<p>The operator <code>==</code> will then after doing all the null checks for me end up calling <code>foo1.Equals(foo2)</code> that I have overridden to do the actual check if the two are equal.</p>\n'}, {'answer_id': 13899647, 'author': 'nawfal', 'author_id': 661933, 'author_profile': 'https://Stackoverflow.com/users/661933', 'pm_score': 1, 'selected': False, 'text': '<p>My approach is to do </p>\n\n<pre><code>(object)item == null\n</code></pre>\n\n<p>upon which I\'m relying on <code>object</code>\'s own equality operator which can\'t go wrong. Or a custom extension method (and an overload):</p>\n\n<pre><code>public static bool IsNull&lt;T&gt;(this T obj) where T : class\n{\n    return (object)obj == null;\n}\n\npublic static bool IsNull&lt;T&gt;(this T? obj) where T : struct\n{\n    return !obj.HasValue;\n}\n</code></pre>\n\n<p>or to handle more cases, may be:</p>\n\n<pre><code>public static bool IsNull&lt;T&gt;(this T obj) where T : class\n{\n    return (object)obj == null || obj == DBNull.Value;\n}\n</code></pre>\n\n<p>The constraint prevents <code>IsNull</code> on value types. Now its as sweet as calling</p>\n\n<pre><code>object obj = new object();\nGuid? guid = null; \nbool b = obj.IsNull(); // false\nb = guid.IsNull(); // true\n2.IsNull(); // error\n</code></pre>\n\n<p>which means I have one consistent/not-error-prone style of checking for nulls throughout. I also have found <a href="https://stackoverflow.com/a/13899726/661933"><code>(object)item == null</code> is very very very slightly faster than <code>Object.ReferenceEquals(item, null)</code></a>, but only if it matters (I\'m currently working on something where I\'ve to micro-optimize everything!).</p>\n\n<p>To see a complete guide on implementing equality checks, see <a href="https://stackoverflow.com/questions/104158/what-is-best-practice-for-comparing-two-instances-of-a-reference-type">What is &quot;Best Practice&quot; For Comparing Two Instances of a Reference Type?</a></p>\n'}, {'answer_id': 40109060, 'author': 'Basheer AL-MOMANI', 'author_id': 4251431, 'author_profile': 'https://Stackoverflow.com/users/4251431', 'pm_score': -1, 'selected': False, 'text': '<blockquote>\n  <p>A common error in overloads of operator == is to use <code>(a == b)</code>, <code>(a ==null)</code>, or <code>(b == null)</code> to check for reference equality. This instead\n   <strong>results in</strong> a call to the overloaded operator ==, causing an <code>infinite loop</code>. Use <code>ReferenceEquals</code> or cast the type to Object, to avoid the\n   loop.</p>\n</blockquote>\n\n<p>check out this</p>\n\n<pre><code>// If both are null, or both are same instance, return true.\nif (System.Object.ReferenceEquals(a, b))// using ReferenceEquals\n{\n    return true;\n}\n\n// If one is null, but not both, return false.\nif (((object)a == null) || ((object)b == null))// using casting the type to Object\n{\n    return false;\n}\n</code></pre>\n\n<p>reference  <a href="https://msdn.microsoft.com/ru-ru/library/ms173147(v=vs.80).aspx#Anchor_1" rel="nofollow">Guidelines for Overloading Equals() and Operator ==</a></p>\n'}, {'answer_id': 44376857, 'author': 'jacekbe', 'author_id': 8116196, 'author_profile': 'https://Stackoverflow.com/users/8116196', 'pm_score': 3, 'selected': False, 'text': '<p>If you are using C# 7 or later you can use null constant pattern matching:</p>\n\n<pre><code>public static bool operator==(Foo foo1, Foo foo2)\n{\n    if (foo1 is null)\n        return foo2 is null;\n    return foo1.Equals(foo2);\n}\n</code></pre>\n\n<p>This gives you slightly neater code than the one calling object.ReferenceEquals(foo1, null)</p>\n'}, {'answer_id': 45465996, 'author': 'Zach Posten', 'author_id': 2517147, 'author_profile': 'https://Stackoverflow.com/users/2517147', 'pm_score': 0, 'selected': False, 'text': '<p><a href="https://msdn.microsoft.com/en-us/library/w4hkze5k%28v=vs.110%29.aspx?f=255&amp;MSPPError=-2147217396" rel="nofollow noreferrer">The static <code>Equals(Object,\u2002Object)</code> method</a> indicates whether two objects, <code>objA</code> and <code>objB</code>, are equal. It also enables you to test objects whose value is <code>null</code> for equality. It compares <code>objA</code> and <code>objB</code> for equality as follows:</p>\n\n<ul>\n<li>It determines whether the two objects represent the same object reference. If they do, the method returns <code>true</code>. This test is equivalent to calling the <code>ReferenceEquals</code> method. In addition, if both <code>objA</code> and <code>objB</code> are <code>null</code>, the method returns <code>true</code>.</li>\n<li>It determines whether either <code>objA</code> or <code>objB</code> is <code>null</code>. If so, it returns <code>false</code>.\nIf the two objects do not represent the same object reference and neither is <code>null</code>, it calls <code>objA.Equals(objB)</code> and returns the result. This means that if <code>objA</code> overrides the <code>Object.Equals(Object)</code> method, this override is called.</li>\n</ul>\n\n<p>.</p>\n\n<pre><code>public static bool operator ==(Foo objA, Foo objB) {\n    return Object.Equals(objA, objB);\n}\n</code></pre>\n'}, {'answer_id': 51354894, 'author': 'CCondron', 'author_id': 602408, 'author_profile': 'https://Stackoverflow.com/users/602408', 'pm_score': 0, 'selected': False, 'text': '<p>replying more to <a href="https://stackoverflow.com/questions/4219261/overriding-operator-how-to-compare-to-null">overriding operator how to compare to null</a> that redirects here as a duplicate.</p>\n\n<p>In the cases where this is being done to support Value Objects, I find the new notation to handy, and like to ensure there is only one place where the comparison is made. Also leveraging Object.Equals(A, B) simplifies the null checks.</p>\n\n<p>This will overload ==, !=, Equals, and GetHashCode</p>\n\n<pre><code>    public static bool operator !=(ValueObject self, ValueObject other) =&gt; !Equals(self, other);\n    public static bool operator ==(ValueObject self, ValueObject other) =&gt; Equals(self, other);\n    public override bool Equals(object other) =&gt; Equals(other as ValueObject );\n    public bool Equals(ValueObject other) {\n        return !(other is null) &amp;&amp; \n               // Value comparisons\n               _value == other._value;\n    }\n    public override int GetHashCode() =&gt; _value.GetHashCode();\n</code></pre>\n\n<p>For more complicated objects add additional comparisons in Equals and a richer GetHashCode.</p>\n'}, {'answer_id': 54576557, 'author': 'Reto Messerli', 'author_id': 9697734, 'author_profile': 'https://Stackoverflow.com/users/9697734', 'pm_score': 2, 'selected': False, 'text': "<p>There is actually a simpler way of checking against <code>null</code> in this case:</p>\n\n<pre><code>if (foo is null)\n</code></pre>\n\n<p>That's it! </p>\n\n<p>This feature was introduced in C# 7</p>\n"}, {'answer_id': 58195524, 'author': 'mr5', 'author_id': 2304737, 'author_profile': 'https://Stackoverflow.com/users/2304737', 'pm_score': 0, 'selected': False, 'text': '<p>For a modern and condensed syntax:</p>\n\n<pre><code>public static bool operator ==(Foo x, Foo y)\n{\n    return x is null ? y is null : x.Equals(y);\n}\n\npublic static bool operator !=(Foo x, Foo y)\n{\n    return x is null ? !(y is null) : !x.Equals(y);\n}\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/73713', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/12767/']