Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Int32. Equals Method in C# with Examples
The Int32.Equals() method in C# is used to compare the current integer instance with another value to determine if they are equal. It returns a boolean value indicating whether the comparison values are equal or not.
Syntax
The Int32.Equals() method has two overloaded forms −
public bool Equals(int other); public override bool Equals(object obj);
Parameters
other − An Int32 value to compare with the current instance.
obj − An object to compare with the current instance.
Return Value
Both overloads return a bool value −
trueif the values are equalfalseif the values are not equal
Using Equals() with Int32 Values
Example
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));
int val3 = 299;
Console.WriteLine("Value3 = " + val3);
Console.WriteLine("val1.Equals(val3)? = " + val1.Equals(val3));
}
}
The output of the above code is −
Value1 = 299 Value2 = 450 Are they equal? = False Value3 = 299 val1.Equals(val3)? = True
Using Equals() with Object Parameter
Example
using System;
public class Demo {
public static void Main(){
int val1 = 100;
object obj1 = 100;
object obj2 = "100";
object obj3 = null;
Console.WriteLine("val1 = " + val1);
Console.WriteLine("obj1 = " + obj1);
Console.WriteLine("obj2 = " + obj2);
Console.WriteLine("obj3 = " + obj3);
Console.WriteLine("val1.Equals(obj1): " + val1.Equals(obj1));
Console.WriteLine("val1.Equals(obj2): " + val1.Equals(obj2));
Console.WriteLine("val1.Equals(obj3): " + val1.Equals(obj3));
}
}
The output of the above code is −
val1 = 100 obj1 = 100 obj2 = 100 obj3 = val1.Equals(obj1): True val1.Equals(obj2): False val1.Equals(obj3): False
Using Equals() with Special Values
Example
using System;
public class Demo {
public static void Main(){
int val1 = Int32.MinValue;
int val2 = Int32.MaxValue;
int val3 = 0;
int val4 = -2147483648;
Console.WriteLine("MinValue = " + val1);
Console.WriteLine("MaxValue = " + val2);
Console.WriteLine("Zero = " + val3);
Console.WriteLine("Manual MinValue = " + val4);
Console.WriteLine("MinValue.Equals(Zero): " + val1.Equals(val3));
Console.WriteLine("MinValue.Equals(Manual): " + val1.Equals(val4));
Console.WriteLine("MaxValue.Equals(Zero): " + val2.Equals(val3));
}
}
The output of the above code is −
MinValue = -2147483648 MaxValue = 2147483647 Zero = 0 Manual MinValue = -2147483648 MinValue.Equals(Zero): False MinValue.Equals(Manual): True MaxValue.Equals(Zero): False
Key Points
The method performs value comparison, not reference comparison.
When comparing with an object, the method returns
falseif the object is not anInt32type.Comparing with
nullalways returnsfalse.The method is more efficient than using the
==operator when working with boxed integers.
Conclusion
The Int32.Equals() method provides a reliable way to compare integer values for equality. It handles both direct integer comparisons and object comparisons, making it useful in scenarios where you need to verify if two integer values are exactly the same.
