Get the TypeCode for value type Int16 in C#

The GetTypeCode() method in C# returns a TypeCode enumeration that identifies the type of a value. For Int16 (short) values, this method consistently returns TypeCode.Int16 regardless of the actual numeric value stored.

Syntax

Following is the syntax for getting the TypeCode of an Int16 value −

TypeCode typeCode = int16Variable.GetTypeCode();

Return Value

The GetTypeCode() method returns TypeCode.Int16 for all short variables, which represents the 16-bit signed integer type.

Using GetTypeCode() with Int16 Values

Example

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

Example with Custom Values

using System;
public class Demo {
   public static void Main() {
      short val1 = 23;
      short val2 = 0;
      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 = 23
Value2 = 0
HashCode for value1 = 1507351
HashCode for value2 = 0
Are they equal? = False
TypeCode for val1 = Int16
TypeCode for val2 = Int16

TypeCode for Different Numeric Types

The following example demonstrates how different numeric types return different TypeCode values −

using System;
public class Demo {
   public static void Main() {
      short shortVal = 100;
      int intVal = 100;
      long longVal = 100L;
      
      Console.WriteLine("Short value: " + shortVal + " -> TypeCode: " + shortVal.GetTypeCode());
      Console.WriteLine("Int value: " + intVal + " -> TypeCode: " + intVal.GetTypeCode());
      Console.WriteLine("Long value: " + longVal + " -> TypeCode: " + longVal.GetTypeCode());
   }
}

The output of the above code is −

Short value: 100 -> TypeCode: Int16
Int value: 100 -> TypeCode: Int32
Long value: 100 -> TypeCode: Int64

Conclusion

The GetTypeCode() method for Int16 values always returns TypeCode.Int16, providing a way to identify the data type at runtime. This is useful for type checking and polymorphic operations where you need to determine the specific numeric type being used.

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

175 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements