byte

Type Range Default Value
byte 0 to 255 0

The byte type represents a 8-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 byte type, and integer literals that fit within the range of the byte type can be written without a suffix.

Auto-boxing

All primitive types have a corresponding wrapper class. The corresponding wrapper class for the byte type is System.UInteger8.

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 byte and can be assigned to a variable of type System.UInteger8 via auto-boxing:

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

Without auto-boxing, the equivalent code would be:

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

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

1
byte value = new System.UInteger8(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 byte type, and, at runtime, the variable's value exceeds the byte maximum (255) by one, it will wrap around to the byte minimum (0); if the value exceeds the byte maximum by two, it will wrap around to the byte minimum plus one (1); and so on.

Conversions for Internal Types

For conversions to and from the byte 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