Int16.Equals Method in C# with Examples

The Int16.Equals() method in C# is used to return a value indicating whether this instance is equal to a specified object or Int16. This method provides two overloads − one for comparing with another Int16 value and another for comparing with any object.

Syntax

Following is the syntax for both overloads −

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

Parameters

For the first overload −

  • ob − An Int16 value to compare to this instance.

For the second overload −

  • ob − The object to compare to this instance.

Return Value

Both overloads return true if ob has the same value as this instance; otherwise, false.

Using Int16.Equals with Different Values

Let us see an example comparing two different Int16 values −

using System;

public class Demo {
    public static void Main(){
        short val1 = 15;
        short val2 = 17;
        Console.WriteLine("Value1 = " + val1);
        Console.WriteLine("Value2 = " + val2);
        Console.WriteLine("Are they equal? = " + (val1.Equals(val2)));
    }
}

The output of the above code is −

Value1 = 15
Value2 = 17
Are they equal? = False

Using Int16.Equals with Same Values

Let us now see an example comparing two identical Int16 values −

using System;

public class Demo {
    public static void Main(){
        short val1 = 25;
        short val2 = 25;
        Console.WriteLine("Value1 = " + val1);
        Console.WriteLine("Value2 = " + val2);
        Console.WriteLine("Are they equal? = " + (val1.Equals(val2)));
    }
}

The output of the above code is −

Value1 = 25
Value2 = 25
Are they equal? = True

Using Int16.Equals with Object Parameter

This example demonstrates the object overload of the Equals() method −

using System;

public class Demo {
    public static void Main(){
        short val1 = 42;
        object obj1 = (short)42;
        object obj2 = 42;  // int, not short
        object obj3 = "42"; // string
        
        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.Equals(obj1): True
val1.Equals(obj2): False
val1.Equals(obj3): False

Conclusion

The Int16.Equals() method provides type-safe equality comparison for short values. When using the object overload, the method returns true only if the object is exactly an Int16 with the same value, ensuring strict type and value equality.

Updated on: 2026-03-17T07:04:35+05:30

204 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements