Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The ushort keyword in C# is an alias for the .NET System.UInt16 structure. It represents a 16-bit (2-byte) unsigned integral value type used to store positive whole numbers and zero. Because it is unsigned, it cannot represent negative values, allocating all 16 bits strictly to the magnitude of the number.

Technical Specifications

  • Underlying Type: System.UInt16
  • Memory Size: 16 bits (2 bytes)
  • Value Range: 0 to 65,535 (21612^{16} - 1)
  • Default Value: 0
  • CLS Compliance: ushort is not Common Language Specification (CLS) compliant.

Syntax and Initialization

C# does not have a dedicated literal suffix for the ushort type. Integer literals are treated as int by default, but the compiler will implicitly convert an int literal to a ushort during assignment if the value falls within the valid 16-bit unsigned range.
// Standard initialization
ushort minValue = 0;
ushort maxValue = 65535;

// Using the underlying .NET struct constants
ushort min = System.UInt16.MinValue;
ushort max = System.UInt16.MaxValue;

// Hexadecimal and binary literals
ushort hexValue = 0xFFFF;      // 65535
ushort binaryValue = 0b_1010;  // 10

Type Conversions

Implicit Conversions A ushort can be implicitly converted to any numeric type that has a larger memory footprint or can accommodate its maximum value without data loss. Valid implicit conversion targets are: int, uint, long, ulong, float, double, and decimal.
ushort baseValue = 42000;
int intValue = baseValue;       // Implicit conversion to 32-bit signed integer
float floatValue = baseValue;   // Implicit conversion to single-precision floating-point
Explicit Conversions (Casting) Converting to a ushort from a larger integral type (e.g., int), a floating-point type, or a signed type (e.g., short) requires an explicit cast. This is enforced by the compiler because the source value might exceed the ushort upper bound of 65,535 or be less than 0. Depending on the execution context (checked vs. unchecked), invalid conversions will either throw an OverflowException or result in silent bitwise truncation.
int largeValue = 70000;
// ushort invalid = largeValue; // Compiler error: Cannot implicitly convert type 'int' to 'ushort'

// Explicit cast required. In an unchecked context, this results in data loss (truncation).
ushort truncatedValue = (ushort)largeValue; 

short signedValue = -5;
// Explicit cast required due to sign difference. Results in underflow if unchecked.
ushort castedFromSigned = (ushort)signedValue; 
Master C# with Deep Grasping Methodology!Learn More