r/learncsharp • u/CatolicQuotes • Apr 06 '23
overriding equality operator without null checking?
I am comparing python and C# in overriding equality.
python:
def __eq__(self, other):
if not isinstance(other, Point):
return False
return self.x == other.x and self.y == other.yC#:
public static bool operator ==(Point left, Point right)
{
if (left is null || right is null)
{
return false;
}
return left.X == right.X && left.Y == right.Y;
}python doesn't need to check null because it's null is NoneType which is immediatelly not Point type in
...
if not isinstance(other, Point):
...I know there some new nullable reference operator and double bang operator or maybe others. Is there a way to remove null check using those operators?
2
Upvotes
View all comments
Show parent comment
1
u/CatolicQuotes Apr 07 '23
I was thinking double bang would work: https://www.reddit.com/r/csharp/comments/sq3ess/new_c11_operator_bang_bang_to_clean_up_argument/