SByte.Equals() Method in C# with Examples

The SByte.Equals() method in C# is used to return a value indicating whether this instance is equal to a specified object or SByte. The sbyte data type represents an 8-bit signed integer with values ranging from -128 to 127.

Syntax

The SByte.Equals() method has two overloaded forms −

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

Parameters

  • ob (sbyte) − An SByte value to compare to this instance.

  • ob (object) − An object to compare with this instance.

Return Value

Returns true if the specified value or object is equal to this instance; otherwise, false.

Using Equals() with SByte Values

Example

using System;
public class Demo {
    public static void Main() {
        sbyte s1 = 10;
        sbyte s2 = 100;
        sbyte s3 = 10;
        Console.WriteLine("Value of S1 = " + s1);
        Console.WriteLine("Value of S2 = " + s2);
        Console.WriteLine("Value of S3 = " + s3);
        Console.WriteLine("Is s1 and s2 equal? = " + s1.Equals(s2));
        Console.WriteLine("Is s1 and s3 equal? = " + s1.Equals(s3));
    }
}

The output of the above code is −

Value of S1 = 10
Value of S2 = 100
Value of S3 = 10
Is s1 and s2 equal? = False
Is s1 and s3 equal? = True

Using Equals() with Object Parameter

Example

using System;
public class Demo {
    public static void Main() {
        sbyte s1 = 10;
        object s2 = (sbyte)10;  // Boxing sbyte to object
        object s3 = 10;        // Integer boxed as object
        Console.WriteLine("Value of S1 = " + s1);
        Console.WriteLine("Value of S2 = " + s2);
        Console.WriteLine("Value of S3 = " + s3);
        Console.WriteLine("Is s1 and s2 equal? = " + s1.Equals(s2));
        Console.WriteLine("Is s1 and s3 equal? = " + s1.Equals(s3));
        Console.WriteLine("s2 type: " + s2.GetType());
        Console.WriteLine("s3 type: " + s3.GetType());
    }
}

The output of the above code is −

Value of S1 = 10
Value of S2 = 10
Value of S3 = 10
Is s1 and s2 equal? = True
Is s1 and s3 equal? = False
s2 type: System.SByte
s3 type: System.Int32

How It Works

The Equals() method performs both value and type checking. When comparing with an object, it returns true only if the object is also an sbyte with the same value. Even if an int object has the same numeric value, it will return false because the types don't match.

Comparison with Different Approaches

Method Type Check Usage
Equals(sbyte) Not needed Direct comparison with another sbyte
Equals(object) Required Comparison with boxed values or objects
== operator Compile-time Simple equality comparison

Conclusion

The SByte.Equals() method provides reliable equality comparison for sbyte values. When using the object overload, it performs strict type checking, ensuring that only objects of the same type with equal values return true.

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

127 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements