Int16.GetTypeCode Method in C# with Examples

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

The TypeCode enumeration provides a way to categorize the data types supported by the Common Language Runtime (CLR). For Int16 values, this method always returns TypeCode.Int16.

Syntax

Following is the syntax −

public TypeCode GetTypeCode();

Return Value

This method returns TypeCode.Int16, which is the enumerated constant representing the Int16 type.

Using GetTypeCode() with Int16 Values

Example

Let us see an example to implement the Int16.GetTypeCode() method −

using System;

public class Demo {
   public static void Main(){
      short val1 = 0;
      short val2 = Int16.MaxValue;
      Console.WriteLine("Value1 = " + val1);
      Console.WriteLine("Value2 = " + val2);
      Console.WriteLine("HashCode for value1 = " + val1.GetHashCode());
      Console.WriteLine("HashCode for value2 = " + val2.GetHashCode());
      Console.WriteLine("Are they equal? = " + (val1.Equals(val2)));
      TypeCode type1 = val1.GetTypeCode();
      TypeCode type2 = val2.GetTypeCode();
      Console.WriteLine("TypeCode for val1 = " + type1);
      Console.WriteLine("TypeCode for val2 = " + type2);
   }
}

The output of the above code is −

Value1 = 0
Value2 = 32767
HashCode for value1 = 0
HashCode for value2 = 2147450879
Are they equal? = False
TypeCode for val1 = Int16
TypeCode for val2 = Int16

Comparing TypeCodes of Different Data Types

Example

Let us see another example that compares TypeCode values across different numeric types −

using System;

public class Demo {
   public static void Main(){
      short val1 = 23;
      int val2 = 23;
      long val3 = 23;
      
      Console.WriteLine("Int16 value = " + val1);
      Console.WriteLine("Int32 value = " + val2);
      Console.WriteLine("Int64 value = " + val3);
      
      TypeCode type1 = val1.GetTypeCode();
      TypeCode type2 = val2.GetTypeCode();
      TypeCode type3 = val3.GetTypeCode();
      
      Console.WriteLine("TypeCode for Int16 = " + type1);
      Console.WriteLine("TypeCode for Int32 = " + type2);
      Console.WriteLine("TypeCode for Int64 = " + type3);
      
      Console.WriteLine("Are TypeCodes equal (Int16 vs Int32)? = " + (type1 == type2));
   }
}

The output of the above code is −

Int16 value = 23
Int32 value = 23
Int64 value = 23
TypeCode for Int16 = Int16
TypeCode for Int32 = Int32
TypeCode for Int64 = Int64
Are TypeCodes equal (Int16 vs Int32)? = False

Common Use Cases

The GetTypeCode() method is commonly used in scenarios like:

  • Type checking at runtime without using reflection

  • Serialization processes where type information needs to be preserved

  • Generic programming where behavior depends on the underlying data type

Conclusion

The Int16.GetTypeCode() method provides a simple way to identify Int16 data types at runtime by returning TypeCode.Int16. This method is useful for type checking, serialization, and generic programming scenarios where you need to determine the exact numeric type being used.

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

166 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements