Comparison Operator

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.

Advertisements

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.

Advertisements

Related Terms

Latest DevOps Terms

Related Reading

Margaret Rouse

Margaret Rouse is an award-winning technical writer and teacher known for her ability to explain complex technical subjects to a non-technical, business audience. Over the past twenty years her explanations have appeared on TechTarget websites and she's been cited as an authority in articles by the New York Times, Time Magazine, USA Today, ZDNet, PC Magazine and Discovery Magazine.Margaret's idea of a fun day is helping IT and business professionals learn to speak each other’s highly specialized languages. If you have a suggestion for a new definition or how to improve a technical explanation, please email Margaret or contact her…