unsigned short

Type Range Default Value
unsigned short 0 to 65,535 0

The unsigned short type represents a 16-bit unsigned two's-complement integer.

Literals

Integrals without a suffix are interpreted in this order: int, unsigned int, long, unsigned long, byte, signed byte, short, unsigned short, and char. If the integral value does not "fit" within any specified types, such as possible function overloads or a specified variable type, an error will be raised at compile time.

There is no suffix for the unsigned short type, and integer literals that fit within the range of the unsigned short type can be written without a suffix.

Auto-boxing

All primitive types have a corresponding wrapper class. The corresponding wrapper class for the unsigned short type is System.UInteger16.

Auto-boxing occurs when a value of a primitive type is assigned or passed as an argument to a variable or function that expects a value of its corresponding wrapper class. For example, the integer literal 100 would fit within the range of a unsigned short and can be assigned to a variable of type System.UInteger16 via auto-boxing:

1
System.UInteger16 value = 100; // Auto-boxing

Without auto-boxing, the equivalent code would be:

1
System.UInteger16 value = new System.UInteger16(100);

The reverse also occurs. Unboxing is when the wrapper class is automatically converted to its corresponding primitive type. Thus, an instance of System.UInteger16 can be unboxed to the unsigned short primitive type.

1
unsigned short value = new System.UInteger16(100); // Unboxing

Overflow and Wrapping

When values for integer types go out of range at runtime, they will "wrap around". Thus, if a variable is declared as having the unsigned short type, and, at runtime, the variable's value exceeds the unsigned short maximum (65,535) by one, it will wrap around to the unsigned short minimum (0); if the value exceeds the unsigned short maximum by two, it will wrap around to the short minimum plus one (1); and so on.

Conversions for Internal Types

For conversions to and from the unsigned short type, consult the Conversions Table.

Conversions for external Type

When converting from external:

  • If the external value is a numeric value, it will be automatically converted to the equivalent integer value.

  • If the external value is a numeric value that is out of range, wrapping will occur.

  • If the external value is a numeric string value, it will be automatically converted to the equivalent integer value.

  • If the external value is a numeric string value that is out of range, wrapping will occur.

  • If the external value is a non-numeric string value, it will be automatically converted to zero (0).

  • If the external value is the Boolean true value, it will be automatically converted to one (1).

  • If the external value is the Boolean false value, it will be automatically converted to zero (0).

  • If the external value is a non-numeric value and non-Boolean value, it will be automatically converted to zero (0).

When converting to external, the value will be converted to a JavaScript number value.

See Also

Share

HTML | BBCode | Direct Link