Skip to main content
The byte keyword in C# represents an 8-bit unsigned integer. It is an alias for the .NET System.Byte structure and is used to store positive numeric values ranging from 0 to 255. As a value type, it is allocated directly on the stack (or inline within an object) and has a default value of 0.

Technical Specifications

  • .NET Type: System.Byte
  • Memory Size: 8 bits (1 byte)
  • Minimum Value: 0 (byte.MinValue)
  • Maximum Value: 255 (byte.MaxValue)
  • Sign: Unsigned (cannot represent negative numbers)

Declaration and Initialization

You can initialize a byte using standard decimal literals, hexadecimal literals, or binary literals.

Type Conversions

Because byte is the smallest unsigned integer type in C#, its conversion rules are strictly defined to prevent unintended data loss. Implicit Conversions A byte can be implicitly converted to any larger numeric type or any floating-point type: short, ushort, int, uint, long, ulong, float, double, or decimal.
Explicit Conversions Converting from a larger integer type, a floating-point type, or a signed type (like sbyte) to a byte requires an explicit cast.

Overflow Behavior

When a mathematical operation causes a byte to exceed its maximum value (255) or drop below its minimum value (0), the behavior depends on the compiler’s overflow checking context.

Bitwise Operations and Numeric Promotion

When performing arithmetic or bitwise operations (~, &, |, ^, <<, >>) on byte operands, C# implicitly promotes the operands to int (32-bit) before evaluating the expression. The result of the operation is an int, which must be explicitly cast back to a byte if you intend to store it in a byte variable.
Tired of Poor C# Skills? Fix That With Deep Grasping!Learn More