byte (C# Reference)

byte denotes an integral type that stores values as indicated in the following table.

Type Range Size .NET type
byte 0 to 255 Unsigned 8-bit integer System.Byte


You can declare and initialize a byte variable by assigning a decimal literal, a hexadecimal literal, or (starting with C# 7.0) a binary literal to it. If the integer literal is outside the range of byte (that is, if it is less than Byte.MinValue or greater than Byte.MaxValue), a compilation error occurs.

In the following example, integers equal to 201 that are represented as decimal, hexadecimal, and binary literals are implicitly converted from int to byte values.

byte byteValue1 = 201;

byte byteValue2 = 0x00C9;

byte byteValue3 = 0b1100_1001;
// The example displays the following output:
//          201
//          201
//          201


You use the prefix 0x or 0X to denote a hexadecimal literal and the prefix 0b or 0B to denote a binary literal. Decimal literals have no prefix.

Starting with C# 7.0, a couple of features have been added to enhance readability.

  • C# 7.0 allows the usage of the underscore character, _, as a digit separator.
  • C# 7.2 allows _ to be used as a digit separator for a binary or hexadecimal literal, after the prefix. A decimal literal isn't permitted to have a leading underscore.

Some examples are shown below.

byte byteValue4 = 0b1100_1001;

byte byteValue5 = 0b_1100_1001;
Console.WriteLine(byteValue5);       // C# 7.2 onwards
// The example displays the following output:
//          201
//          201


There is a predefined implicit conversion from byte to short, ushort, int, uint, long, ulong, float, double, or decimal.

You cannot implicitly convert non-literal numeric types of larger storage size to byte. For more information on the storage sizes of integral types, see Integral Types Table. Consider, for example, the following two byte variables x and y:

byte x = 10, y = 20;

The following assignment statement will produce a compilation error, because the arithmetic expression on the right-hand side of the assignment operator evaluates to int by default.

// Error: conversion from int to byte:
byte z = x + y;

To fix this problem, use a cast:

// OK: explicit conversion:
byte z = (byte)(x + y);

It is possible though, to use the following statements where the destination variable has the same storage size or a larger storage size:

int x = 10, y = 20;
int m = x + y;
long n = x + y;

Also, there is no implicit conversion from floating-point types to byte. For example, the following statement generates a compiler error unless an explicit cast is used:

// Error: no implicit conversion from double:
byte x = 3.0;
// OK: explicit conversion:
byte y = (byte)3.0;

When calling overloaded methods, a cast must be used. Consider, for example, the following overloaded methods that use byte and int parameters:

public static void SampleMethod(int i) {}
public static void SampleMethod(byte b) {}

Using the byte cast guarantees that the correct type is called, for example:

// Calling the method with the int parameter:
// Calling the method with the byte parameter:

For information on arithmetic expressions with mixed floating-point types and integral types, see float and double.

For more information on implicit numeric conversion rules, see the Implicit Numeric Conversions Table.

C# Language Specification

For more information, see Integral types in the C# Language Specification. The language specification is the definitive source for C# syntax and usage.

See also