# ushort (C# Reference)

The ushort keyword indicates an integral data type that stores values according to the size and range shown in the following table.

Type Range Size .NET type
ushort 0 to 65,535 Unsigned 16-bit integer System.UInt16

## Literals

You can declare and initialize a ushort 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 ushort (that is, if it is less than UInt16.MinValue or greater than UInt16.MaxValue), a compilation error occurs.

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

ushort ushortValue1 = 65034;
Console.WriteLine(ushortValue1);

ushort ushortValue2 = 0xFE0A;
Console.WriteLine(ushortValue2);

ushort ushortValue3 = 0b1111_1110_0000_1010;
Console.WriteLine(ushortValue3);
// The example displays the following output:
//          65034
//          65034
//          65034


Note

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.

ushort ushortValue1 = 65_034;
Console.WriteLine(ushortValue1);

ushort ushortValue2 = 0b11111110_00001010;
Console.WriteLine(ushortValue2);

ushort ushortValue3 = 0b_11111110_00001010;     // C# 7.2 onwards
Console.WriteLine(ushortValue3);
// The example displays the following output:
//          65034
//          65034
//          65034


A cast must be used when you call overloaded methods. Consider, for example, the following overloaded methods that use ushort and int parameters:

public static void SampleMethod(int i) {}
public static void SampleMethod(ushort s) {}


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

// Calls the method with the int parameter:
SampleMethod(5);
// Calls the method with the ushort parameter:
SampleMethod((ushort)5);


## Conversions

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

There is a predefined implicit conversion from byte or char to ushort. Otherwise a cast must be used to perform an explicit conversion. Consider, for example, the following two ushort variables x and y:

ushort x = 5, y = 12;


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

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


To fix this problem, use a cast:

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


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

int m = x + y;
long n = x + y;


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

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


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