What are comparison operators in C Sharp?
In C #, a comparison operator is a binary operator that uses two operands whose values are compared. Comparison operators are used in conditional statements, especially in loops, where the result of the comparison determines whether execution should continue. They are the key to program flow control, known as conditional processing.
Comparison operators include:
Equal operator (==), returns true for operands whose values are equal.
Inequality operator (! =), Returns false if two operands are equal.
Less than relational operator (<), der für alle numerischen Typen und Aufzählungstypen definiert ist und Wahr zurückgibt, wenn der erste Operand kleiner als der zweite Operand ist. Größer als relationaler Operator (>), which is defined for all numeric types and enumerated types and returns the value true if the first operand is greater than the second operand.
Less than or equal to the comparison operator (<=), der für alle numerischen Typen und Aufzählungstypen definiert ist, und gibt true zurück, wenn der erste Operand kleiner oder gleich dem zweiten Operanden ist. Größer als oder gleich dem Vergleichsoperator (> =), which is defined for all numeric and enumerated 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.
Comparison operators have the following characteristics:
Work with a variable type and return a value of type bool.
Cannot be used directly to compare custom type objects. When a comparison operator is used to compare objects, it only compares object references, 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 couples
Overloading the comparison operators
If == and! = For a given type are overloaded, the Equals () and GetHashCode () methods should be overridden.
Cannot be used with structs until the operator is overloaded to implement the logic for the comparison.
In the .NET Framework, the System.String class is used for actions related to strings, such as: B. on manipulation, comparison and concatenation. It overloads the == operator to check the equality of the contents of operands of type string, and compares the references of the operands if they are not of type string. Version 4.0 of the .NET Framework provides dynamic typing that allows the compiler to perform the conversion required for comparison purposes.
When comparing objects with nested classes, the comparison can be based on a reference that points to a nested object to be compared (deep comparison) or on the values of the objects. This decision should be made during the drafting phase of an application. To compare floating point numbers, values should be rounded to an acceptable level for the application.