Int16.Equals Method in C# with Examples
Last Updated :
04 Apr, 2019
Int16.Equals() Method is used to get a value which indicates whether the current instance is equal to a specified object or Int16. There are 2 methods in the overload list of this method which are as follows:
- Equals(Int16) Method
- Equals(Object) Method
Int16.Equals(Int16)
This method is used to return a value indicating whether the current instance is equal to a specified Int16 value or not.
Syntax: public bool Equals (short obj);
Here, it takes a Int16 value to compare to this instance.
Return Value: This method returns true if obj has the same value as this instance otherwise, false.
Below programs illustrate the use of Int16.Equals(Int16) Method:
Example 1:
using System;
class GFG {
public static void Main()
{
short value1 = 15;
short value2 = 17;
bool status = value1.Equals(value2);
if (status)
Console.WriteLine( "{0} is equal to {1}" ,
value1, value2);
else
Console.WriteLine( "{0} is not equal to {1}" ,
value1, value2);
}
}
|
Output:
15 is not equal to 17
Example 2:
using System;
class GFG {
public static void Main()
{
get (5, 5);
get (5, 4);
get (10, 20);
get (7, 7);
}
public static void get ( short value1,
short value2)
{
bool status = value1.Equals(value2);
if (status)
Console.WriteLine( "{0} is equal to {1}" ,
value1, value2);
else
Console.WriteLine( "{0} is not equal to {1}" ,
value1, value2);
}
}
|
Output:
5 is equal to 5
5 is not equal to 4
10 is not equal to 20
7 is equal to 7
Int16.Equals(Object) Method
This method is used to returns a value indicating whether the current instance is equal to a specified object or not.
Syntax: public override bool Equals (object obj);
Here, it takes an object to compare with this instance.
Return Value: This method returns true if obj is an instance of Int16 and equals the value of this instance otherwise, false.
Below programs illustrate the use of the above-discussed method:
Example 1:
using System;
class GFG {
public static void Main()
{
short value1 = 10;
object value2 = 37;
bool status = value1.Equals(value2);
if (status)
Console.WriteLine( "{0} is equal to {1}" ,
value1, value2);
else
Console.WriteLine( "{0} is not equal to {1}" ,
value1, value2);
}
}
|
Output:
10 is not equal to 37
Example 2:
using System;
class GFG {
public static void Main()
{
get (5, 5);
get (5, 4);
get (10, 20);
get (7, 7);
}
public static void get ( short value1,
object value2)
{
bool status = value1.Equals(value2);
if (status)
Console.WriteLine( "{0} is equal to {1}" ,
value1, value2);
else
Console.WriteLine( "{0} is not equal to {1}" ,
value1, value2);
}
}
|
Output:
5 is not equal to 5
5 is not equal to 4
10 is not equal to 20
7 is not equal to 7
Reference:
Please Login to comment...