Definition - What does Comparison Operator mean?
In C#, a comparison operator is a binary operator that takes two operands whose values are being compared. Comparison operators are used in conditional statements, especially in loops, where the result of the comparison decides whether execution should proceed. They form the key to program flow control, known as conditional processing.
Comparison operators include:
- Equality operator (==), returns true for operands whose values are equal.
- Inequality operator (!=), returns false if two operands are equal.
- Less than relational operator (<), defined for all numeric and enumeration types and returns true if the first operand is less than the second operand.
- Greater than relational operator (>), defined for all numeric and enumeration types and returns true if the first operand is greater than the second operand.
- Less than or equal to relational operator (<=), defined for all numeric and enumeration types and returns true if the first operand is less than or equal to the second operand.
- Greater than or equal to relational operator (>=), defined for all numeric and enumeration types and returns true if the first operand is greater than or equal to the second operand.
Comparison operators are also known as relational operators.
Techopedia explains Comparison Operator
Comparison operators have the following features:
- Operate on one type of variable and return a value of bool type.
- Cannot be used directly for comparison of objects of user-defined type. If used to compare objects, a comparison operator only compares object references and not the data they contain.
- Can be overloaded in user-defined types by defining static member functions and using the keyword operator.
- Must be overloaded in pairs. If == is overloaded, != must be overloaded. The same rule applies to pairs < and > and <= and >=.
- Overloading the comparison operators < and > implicitly overloads their corresponding assignment operators (if any).
- If == and != are overloaded for a given type, the methods Equals() and GetHashCode() should be overridden.
- Cannot be used with structs until the operator is overloaded to implement the logic for comparison.
In the .NET Framework, the System.String class is used for actions related to strings, such as manipulation, comparison and concatenation. It overloads the == operator to check for equality of contents of string type operands and compares the reference of the operand(s), if they are not of string type. Version 4.0 of the .NET Framework provides the facility for dynamic typing, by which the compiler can perform all the appropriate conversion necessary for comparison purposes.
When comparing objects having nested classes, comparison can be based on a reference pointing to a nested object that has to be compared (deep comparison) or on the values of the objects. This decision should be fixed in the design stage of an application. For comparison between floating point numbers, values should be rounded to an acceptable level for the application.