# short (C# Reference)

short denotes an integral data type that stores values according to the size and range shown in the following table.

Type Range Size .NET type
short -32,768 to 32,767 Signed 16-bit integer System.Int16

## Literals

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

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

short shortValue1 = 1034;
Console.WriteLine(shortValue1);

short shortValue2 = 0x040A;
Console.WriteLine(shortValue2);

short shortValue3 = 0b0100_00001010;
Console.WriteLine(shortValue3);
// The example displays the following output:
//          1034
//          1034
//          1034


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.

short shortValue1 = 1_034;
Console.WriteLine(shortValue1);

short shortValue2 = 0b00000100_00001010;
Console.WriteLine(shortValue2);

short shortValue3 = 0b_00000100_00001010;       // C# 7.2 onwards
Console.WriteLine(shortValue3);
// The example displays the following output:
//          1034
//          1034
//          1034


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

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


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

SampleMethod(5);         // Calling the method with the int parameter
SampleMethod((short)5);  // Calling the method with the short parameter


## Conversions

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

You cannot implicitly convert nonliteral numeric types of larger storage size to short (see Integral Types Table for the storage sizes of integral types). Consider, for example, the following two short variables x and y:

short x = 5, y = 12;


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

short z  = x + y;        // Compiler error CS0266: no conversion from int to short


To fix this problem, use a cast:

short z  = (short)(x + y);   // Explicit conversion


It is also possible 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;


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

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


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.