UInt16.GetTypeCode() Method in C# with Examples

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

The TypeCode enumeration represents different data types in .NET, and for all UInt16 values, this method always returns TypeCode.UInt16.

Syntax

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

public TypeCode GetTypeCode();

Return Value

This method returns TypeCode.UInt16, which is the enumerated constant for the UInt16 data type.

Using GetTypeCode() with Different UInt16 Values

Example

using System;

public class Demo {
   public static void Main() {
      ushort val1 = 55;
      ushort val2 = 100;
      TypeCode type1 = val1.GetTypeCode();
      TypeCode type2 = val2.GetTypeCode();
      Console.WriteLine("TypeCode for val1 = " + type1);
      Console.WriteLine("TypeCode for val2 = " + type2);
      Console.WriteLine("Both values have the same TypeCode: " + (type1 == type2));
   }
}

The output of the above code is −

TypeCode for val1 = UInt16
TypeCode for val2 = UInt16
Both values have the same TypeCode: True

Using GetTypeCode() with UInt16 Boundary Values

Example

using System;

public class Demo {
   public static void Main() {
      ushort val1 = UInt16.MinValue;
      ushort val2 = UInt16.MaxValue;
      ushort val3 = 0;
      
      TypeCode type1 = val1.GetTypeCode();
      TypeCode type2 = val2.GetTypeCode();
      TypeCode type3 = val3.GetTypeCode();
      
      Console.WriteLine("Value: " + val1 + ", TypeCode: " + type1);
      Console.WriteLine("Value: " + val2 + ", TypeCode: " + type2);
      Console.WriteLine("Value: " + val3 + ", TypeCode: " + type3);
      
      Console.WriteLine("UInt16.MinValue = " + UInt16.MinValue);
      Console.WriteLine("UInt16.MaxValue = " + UInt16.MaxValue);
   }
}

The output of the above code is −

Value: 0, TypeCode: UInt16
Value: 65535, TypeCode: UInt16
Value: 0, TypeCode: UInt16
UInt16.MinValue = 0
UInt16.MaxValue = 65535

Comparing TypeCodes of Different Data Types

Example

using System;

public class Demo {
   public static void Main() {
      ushort uint16Val = 100;
      short int16Val = 100;
      int int32Val = 100;
      
      Console.WriteLine("UInt16 value: " + uint16Val + ", TypeCode: " + uint16Val.GetTypeCode());
      Console.WriteLine("Int16 value: " + int16Val + ", TypeCode: " + int16Val.GetTypeCode());
      Console.WriteLine("Int32 value: " + int32Val + ", TypeCode: " + int32Val.GetTypeCode());
      
      Console.WriteLine("Are UInt16 and Int16 TypeCodes equal? " + 
                       (uint16Val.GetTypeCode() == int16Val.GetTypeCode()));
   }
}

The output of the above code is −

UInt16 value: 100, TypeCode: UInt16
Int16 value: 100, TypeCode: Int16
Int32 value: 100, TypeCode: Int32
Are UInt16 and Int16 TypeCodes equal? False

Conclusion

The UInt16.GetTypeCode() method consistently returns TypeCode.UInt16 for all UInt16 values, regardless of their actual numeric value. This method is useful for runtime type identification and type conversion operations in generic programming scenarios.

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

151 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements