Int64.CompareTo Method in C# with Examples

CsharpServer Side ProgrammingProgramming

The Int64.CompareTo() method in C# is used to compare this instance to a specified object or Int64 and returns an indication of their relative values.

Syntax

Following is the syntax −

public int CompareTo (long val);
public int CompareTo (object val);

Above, in the 1st syntax, the value Val is an integer to compare. The Val in the 2nd syntax is an object to compare.

Example

Let us now see an example to implement the Int64.CompareTo() method −

using System;
public class Demo {
   public static void Main(){
      long val1 = 20;
      long val2 = 18;
      Console.WriteLine("Value 1 = "+val1);
      Console.WriteLine("Value 2 = "+val2);
      Console.WriteLine("Return value (comparison) = "+val1.CompareTo(val2));
   }
}

Output

This will produce the following output −

Value 1 = 20
Value 2 = 18
Return value (comparison) = 1

Example

Let us now see another example to implement the Int64.CompareTo() method −

using System;
public class Demo {
   public static void Main(){
      long val1 = 20;
      object val2 = (long)20;
      Console.WriteLine("Value 1 = "+val1);
      Console.WriteLine("Value 2 = "+val2);
      Console.WriteLine("Return value (comparison) = "+val1.CompareTo(val2));
   }
}

Output

This will produce the following output−

Value 1 = 20
Value 2 = 20
Return value (comparison) = 0
raja
Published on 13-Nov-2019 10:20:12
Advertisements