Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a difference between !(a == b) and a != b

Tags:

c#

I have just seen code that used if(!(a == b)) instead of the more commonly seen if(a != b) in C#. I am wondering if there is a difference between the two in C#?

like image 462
Zigzagoon Avatar asked May 14 '19 18:05

Zigzagoon


People also ask

What difference between $a == $b and $A $B?

a=b is called assignment where the value of b is assigned to a. a==b is a condition checking or you can call it a comparison operator which checks whether the value of a and b are equal or not.

Is a == b and b == a same in Java?

In Java, A == B and B == A always have the same semantics. In C# (which has operator overloading), there can be a difference if, say, B is an instance of a subclass of the class of A . Note that A. equals(B) is not equivalent to A == B .

Whats the difference between != And !==?

!= will only check value regardless of operands type. but !== is used to compare both value & type of 2 operands that are being compared to each other.

Is the same as ==?

== is for value equality. It's used to know if two objects have the same value. is is for reference equality. It's used to know if two references refer (or point) to the same object, i.e if they're identical.


2 Answers

In most cases, they're the same - but they don't have to be. != and == can be overloaded separately, with different logic. Here's an example:

using System;  class Test {     // All this code is awful. PURELY FOR DEMONSTRATION PURPOSES.     public static bool operator==(Test lhs, Test rhs) => true;     public static bool operator!=(Test lhs, Test rhs) => true;             public override bool Equals(object other) => true;     public override int GetHashCode() => 0;      static void Main()     {         Test a = null;         Test b = null;         Console.WriteLine(a != b);    // True         Console.WriteLine(!(a == b)); // False     }     } 

In the vast majority of cases, a != b and !(a == b) will have exactly the same behavior, and a != b is almost always clearer. But it's worth being aware that they can differ.

It can get even more pathological - a != b and !(a == b) may even have different types. For example:

using System;  class Test {     // All this code is awful. PURELY FOR DEMONSTRATION PURPOSES.     public static Test operator==(Test lhs, Test rhs) => new Test();     public static Test operator!=(Test lhs, Test rhs) => new Test();     public static string operator!(Test lhs) => "Negated";     public override string ToString() => "Not negated";      public override bool Equals(object other) => true;     public override int GetHashCode() => 0;      static void Main()     {         Test a = null;         Test b = null;         Console.WriteLine(a != b);    // "Not negated"         Console.WriteLine(!(a == b)); // "Negated"     }     } 

Here a != b is of type Test, but !(a == b) is of type string. Yes, this is horrible and you're unlikely to run into it in real life - but it's the kind of thing a C# compiler needs to know about.

like image 199
Jon Skeet Avatar answered Sep 20 '22 20:09

Jon Skeet


Sure there's a difference. If ! and == and != are overloaded, then the first calls the first two operators, and the second calls the third. Those are permitted to do very different things, though it would be foolish to do so.

In fact it is common to implement overloaded == and != operators in terms of each other; you might say bool operator !=(C x, C y) => !(x == y); for example. In that case, x != y would be an infinite recursion, which is plainly different than calling !(x == y)!

like image 20
Eric Lippert Avatar answered Sep 18 '22 20:09

Eric Lippert