Single.CompareTo() Method in C#The Single.CompareTo() method can be used for comparing the current instances to an object.It returns an integer showing whether the current instance's value is higher than, equivalent to, or less than that of the specified object or another Single instance. The return type is an integer that informs that the value of the current object instance is more than or equal to that of the provided object or of another Single instance. This method's overload list contains two methods, as follows:
CompareTo(Single) MethodThis method compares the current instance with the specified single-precision floating-point numbers. It returns an integer that indicates if the current instance's value is less than, equal to, or higher than the given single-precision floating-point integers. Syntax:It has the following syntax: In this case, a single-precision floating-point number is used for comparison. Return Value: It produces a 32-bit signed number indicating the values of the current instance and the value argument, which are as follows: Less than Zero: If the current instance is less than zero or is not a number (NaN), the value is a number. Zero: Zero is returned if the current instance equals the value or if both the present instance and the value are not integers (NaN), PositiveInfinity, or NegativeInfinity. Greater than zero: If the current instance exceeds the value or if the current instance is an integer but the value is not an integer (NaN). Example:Consider an example to implement the val.CompareTo() method in C#. Output: 16.5 is less than 22.6 Single.CompareTo(Object) MethodThis method is used for comparing the current instance to a specified object. It returns an integer that indicates if the value of the supplied object is more, equal to, or less compared to the value of the current instances. Syntax:It has the following syntax: Here, it compares the object to this instance or null. Return Value: The method will return a 32-bit integer value. Less than Zero: If the return value is less than zero. It returns a number. Zero: Zero is returned if the current instance equals the value or if both the current instance and the value are not numbers (NaN), PositiveInfinity, or NegativeInfinity. Greater than zero: If the current instance values are more or if the current instance is a number but the value is not a number (NaN). Exception: If the value is not a Single, it raises ArgumentException. Example 1:Let us take an example to implement the val.CompareTo() method in C#. Output: 18.5 is less than 20.6 Example 2:Let us take an example to implement the CompareTo() method in C#. Output: val2 must be a Single value The Exception Thrown is: System.ArgumentException Advantages of Single.CompareTo() Method in C#There are several advantages of the Single.CompareTo() method in C#. Some main advantages of this method are as follows:
Next TopicStack contains() method in C# |