How do I check for nulls in an '==' operator overload without infinite recursion?
Use ReferenceEquals
:
Foo foo1 = null;
Foo foo2 = new Foo();
Assert.IsFalse(foo1 == foo2);
public static bool operator ==(Foo foo1, Foo foo2) {
if (object.ReferenceEquals(null, foo1))
return object.ReferenceEquals(null, foo2);
return foo1.Equals(foo2);
}
Overloading == and != operator, need to determine if object null without infinite recursion
I think you should be able to use Object.ReferenceEquals
to do this:
if(Object.ReferenceEquals(x, null) && Object.ReferenceEquals(y, null))
{
return true;
}
return !Object.ReferenceEquals(x, null) && !Object.ReferenceEquals(y, null)
&& x.GetType() == y.GetType()
&& x.GetType()
.GetProperties()
.All(property => property.GetValue(x) == property.GetValue(y));
Infinite recursion when overloading ==
Use (object)person == null
to force it to use the == operator of Object (or use ReferenceEquals
). See http://msdn.microsoft.com/en-us/library/ms173147(v=vs.80).aspx.
C# operator == check for null
Found the anwser
public struct MyClass
{
private string Value;
public static bool operator ==(MyClass left, object right)
{
// Test if both are null or the same instance, then return true
if (ReferenceEquals(left, right))
return true;
// If only one of them null return false
if (((object)left == null) || ((object)right == null))
return false;
// Test value
return left.Equals(right);
}
}
How to perform a null check in an equality operator overload
public static bool operator == (XmlWrapper lhs, XmlWrapper rhs)
{
if (Object.ReferenceEquals(lhs, null) && Object.ReferenceEquals(rhs, null))
{
return true;
}
if (Object.ReferenceEquals(lhs, null) || Object.ReferenceEquals(rhs, null))
{
return false;
}
return lhs._element.Equals(rhs._element);
}
overloaded == operator throwing NullReferenceException with non-null operands
Actually, your overloaded equality operator is hit three times:
First, when called from Program.Main(string[])
with the line tc1 == tc2
, where left
=tc1
and right
=tc2
, which then calls TestClass.Equals(TestClass)
where other
=tc2
.
From there, other == null
now calls your overloaded equality operator a second time, where left
=tc2
and right
=null
.
Now, TestClass.Equals(TestClass)
is called also a second time, where other
=null
.
And finally, other == null
calls your overloaded equality operator for a third time, where both left
=null
and right
=null
. This now eventually causes the System.NullReferenceException because left was null.
To fix this coding error, replace other == null
with other is null
in TestClass.Equals(TestClass)
:
public bool Equals(TestClass other)
{
if (other is null)
return false;
else
return data == other.data;
}
Alternatively, as conditional expression (using expression body):
public bool Equals(TestClass other) => !(other is null) && data == other.data;
Overriding == operator. How to compare to null?
Use object.ReferenceEquals(person1, null)
or the new is operator instead of the ==
operator:
public static bool operator ==(Person person1, Person person2)
{
if (person1 is null)
{
return person2 is null;
}
return person1.Equals(person2);
}
Related Topics
Up, Down, Left and Right Arrow Keys Do Not Trigger Keydown Event
How to Get Text Formatting with Itextsharp
Detailed Explanation of Variable Capture in Closures
Lifetime of ASP.NET Static Variable
How to Create a Dynamic Button Click Event on a Dynamic Button
Can't View Designer When Coding a Form in C#
How to Deserialize a Child Object with Dynamic (Numeric) Key Names
What Are the Differences Between Delegates and Events
Differencebetween Nullable<T>.Hasvalue or Nullable<T> != Null
Loading Custom Configuration Files
Simplest Way to Do a Fire and Forget Method in C#
Globally Catch Exceptions in a Wpf Application
Remove Items from One List in Another
Windows Like Services Development in Linux Using Mono
Using JSONconvert.Deserializeobject to Deserialize JSON to a C# Poco Class
C# Linq Group by on Multiple Columns
Do Try/Catch Blocks Hurt Performance When Exceptions Are Not Thrown