ALERT

[FREE DEMO] Deploy Your Enterprise Cloud in Minutes

Comparison Operator

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.

This definition was written in the context of C#

Techopedia Deals

Connect with us

Techopedia on Linkedin
Techopedia on Linkedin
Tweat cdn.techopedia.com
"Techopedia" on Twitter


'@Techopedia'
Sign up for Techopedia's Free Newsletter!

Email Newsletter

Join thousands of others with our weekly newsletter

Resources
The 4th Era of IT Infrastructure: Superconverged Systems
The 4th Era of IT Infrastructure: Superconverged Systems:
Learn the benefits and limitations of the 3 generations of IT infrastructure – siloed, converged and hyperconverged – and discover how the 4th...
Approaches and Benefits of Network Virtualization
Approaches and Benefits of Network Virtualization:
Businesses today aspire to achieve a software-defined datacenter (SDDC) to enhance business agility and reduce operational complexity. However, the...
Free E-Book: Public Cloud Guide
Free E-Book: Public Cloud Guide:
This white paper is for leaders of Operations, Engineering, or Infrastructure teams who are creating or executing an IT roadmap.
Free Tool: Virtual Health Monitor
Free Tool: Virtual Health Monitor:
Virtual Health Monitor is a free virtualization monitoring and reporting tool for VMware, Hyper-V, RHEV, and XenServer environments.
Free 30 Day Trial – Turbonomic
Free 30 Day Trial – Turbonomic:
Turbonomic delivers an autonomic platform where virtual and cloud environments self-manage in real-time to assure application performance.