SByte.GetTypeCode Method in C# with Examples

The SByte.GetTypeCode() method in C# is used to return the TypeCode enumeration value for the sbyte data type. This method is part of the IConvertible interface and helps identify the specific type of a value at runtime.

Syntax

Following is the syntax for the SByte.GetTypeCode() method −

public TypeCode GetTypeCode();

Return Value

The method returns TypeCode.SByte, which is the enumeration constant representing the sbyte type.

Using GetTypeCode() with SByte Values

Example

using System;

public class Demo {
   public static void Main() {
      sbyte s1 = 55;
      object s2 = (sbyte)55;
      
      Console.WriteLine("Value of S1 = " + s1);
      Console.WriteLine("Value of S2 = " + s2);
      
      int res = s1.CompareTo(s2);
      if (res > 0)
         Console.WriteLine("s1 > s2");
      else if (res < 0)
         Console.WriteLine("s1 < s2");
      else
         Console.WriteLine("s1 = s2");
         
      Console.WriteLine("HashCode for s1 = " + s1.GetHashCode());
      Console.WriteLine("GetTypeCode for s1 = " + s1.GetTypeCode());
      Console.WriteLine("HashCode for s2 = " + s2.GetHashCode());
   }
}

The output of the above code is −

Value of S1 = 55
Value of S2 = 55
s1 = s2
HashCode for s1 = 14135
GetTypeCode for s1 = SByte
HashCode for s2 = 14135

Comparing Different SByte Values

Example

using System;

public class Demo {
   public static void Main() {
      sbyte s1 = 10;
      sbyte s2 = 100;
      
      Console.WriteLine("Value of S1 = " + s1);
      Console.WriteLine("Value of S2 = " + s2);
      
      int res = s1.CompareTo(s2);
      if (res > 0)
         Console.WriteLine("s1 > s2");
      else if (res < 0)
         Console.WriteLine("s1 < s2");
      else
         Console.WriteLine("s1 = s2");
         
      Console.WriteLine("HashCode for s1 = " + s1.GetHashCode());
      Console.WriteLine("GetTypeCode for s1 = " + s1.GetTypeCode());
      Console.WriteLine("HashCode for s2 = " + s2.GetHashCode());
      Console.WriteLine("GetTypeCode for s2 = " + s2.GetTypeCode());
   }
}

The output of the above code is −

Value of S1 = 10
Value of S2 = 100
s1 < s2
HashCode for s1 = 2570
GetTypeCode for s1 = SByte
HashCode for s2 = 25700
GetTypeCode for s2 = SByte

Using GetTypeCode() for Type Identification

Example

using System;

public class Demo {
   public static void Main() {
      sbyte sbyteValue = -50;
      byte byteValue = 200;
      int intValue = 1000;
      
      Console.WriteLine("SByte TypeCode: " + sbyteValue.GetTypeCode());
      Console.WriteLine("Byte TypeCode: " + byteValue.GetTypeCode());
      Console.WriteLine("Int32 TypeCode: " + intValue.GetTypeCode());
      
      // Check if the value is specifically an SByte
      if (sbyteValue.GetTypeCode() == TypeCode.SByte) {
         Console.WriteLine("The value is an SByte type");
      }
   }
}

The output of the above code is −

SByte TypeCode: SByte
Byte TypeCode: Byte
Int32 TypeCode: Int32
The value is an SByte type

Conclusion

The SByte.GetTypeCode() method returns TypeCode.SByte to identify the type at runtime. This method is useful for type checking and conversion operations, particularly when working with generic code or the IConvertible interface.

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

138 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements