The inequality operator (Documentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
!=) is a binary comparison operator that evaluates to true if its operands are not equal, and false otherwise. It functions as the strict logical negation of the equality operator (==).
Evaluation Mechanics by Type
The behavior of the!= operator depends strictly on the type of the operands being evaluated:
- Predefined Value Types (
int,bool,double, etc.): Evaluates value inequality. It compares the underlying data of the operands. - User-Defined Value Types (
struct): The!=operator is not predefined for customstructtypes. Attempting to use!=on a standard customstructwithout an explicit operator overload results in a compiler error (CS0019). - Nullable Value Types (
Nullable<T>): Utilizes “lifted” operators. If both operands arenull,!=evaluates tofalse. If exactly one operand isnull, it evaluates totrue. If both operands possess values, the operator unwraps them and applies the underlying type’s!=operator. - Reference Types (
class,object): Evaluates reference inequality. It returnstrueif the operands point to different memory addresses on the heap, even if the data within those objects is identical. - Strings (
string): Althoughstringis a reference type, the!=operator is overloaded to perform an ordinal, character-by-character value comparison rather than a reference comparison. - Tuples (
ValueTuple): As of C# 7.3, tuples have compiler-synthesized support for the!=operator. It evaluates totrueif any corresponding elements in the tuples are not equal, performing a short-circuiting, element-wise comparison. - Delegates: Evaluates to
trueif the delegates have different target methods, different target object instances, or different invocation lists. Two delegates pointing to the exact same method but bound to different object instances are considered unequal. - Floating-Point Types (
float,double): Follows IEEE 754 standards. Notably,NaN(Not a Number) is never equal to anything, including itself. Therefore,Double.NaN != Double.NaNevaluates totrue.
Operator Overloading
User-defined types can overload the!= operator to provide custom inequality logic (e.g., forcing a class to evaluate by value instead of reference, or enabling != for a struct). C# enforces a strict pairing constraint: if a type overloads !=, it must simultaneously overload ==. Furthermore, it is highly recommended to override Object.Equals() and Object.GetHashCode() to maintain consistent evaluation across the .NET framework and prevent compiler warnings (CS0660 and CS0661).
Compiler-Synthesized Inequality
Forrecord types (both record class and record struct), the compiler automatically synthesizes the != operator. The synthesized operator performs a shallow member-wise inequality check of all properties and fields. It does not perform a recursive value-based check. If a record contains a standard reference type (such as an array or a List<T>), the synthesized operator will perform reference inequality on that specific member.
Boxing Constraints
When value types are boxed intoobject references, the != operator reverts to reference comparison. It evaluates the memory addresses of the boxed instances on the heap, not the unboxed values.
Master C# with Deep Grasping Methodology!Learn More





