Int32. Equals Method in C# with Examples


The Int32.Equals() method in C# is used to return a value indicating whether this instance is equal to a specified object or Int32.

Syntax

Following is the syntax −

public bool Equals (int ob);
public override bool Equals (object ob);

Above, the parameter ob in the 1st syntax is an Int32 value to compare to this instance. The 2nd syntax ob parameter is an object to compare to this instance.

Example

Let us now see an example to implement the Int32.Equals() method −

using System;
public class Demo {
   public static void Main(){
      int val1 = 299;
      int val2 = 450;
      Console.WriteLine("Value1 = "+val1);
      Console.WriteLine("Value2 = "+val2);
      Console.WriteLine("Are they equal? = "+val1.Equals(val2));
   }
}

Output

This will produce the following output −

Value1 = 299
Value2 = 450
Are they equal? = False

Example

Let us now see another example to implement the Int32.Equals() method −

using System;
public class Demo {
   public static void Main(){
      int val1 = Int32.MinValue;
      int val2 = 0;
      Console.WriteLine("Value1 = "+val1);
      Console.WriteLine("Value2 = "+val2);
      Console.WriteLine("Are they equal? = "+val1.Equals(val2));
   }
}

Output

This will produce the following output −

Value1 = -2147483648
Value2 = 0
Are they equal? = False

Updated on: 11-Nov-2019

140 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements