Value Short

Resources are scarce. In some computer programs, memory use is excessive. The short type in the .NET Framework and C# language reduces the memory usage of integers: it represents a number in 2 bytes—16 bits—half the size of an int. Short is aliased to the System.Int16 type in the .NET Framework.
Ushort Example KeywordsSystem.Int16 information short.MinValue = -32768 short.MaxValue = 32767
Overview: This C# example shows the short number type. This type occupies 16 bits.
First, this example shows how you can use the short type as a storage location for positive or negative integers that are not huge. The short type is always aliased to the System.Int16 struct in the base class library in the .NET Framework. The program demonstrates the memory usage of the short type on the managed heap, and also its usage on the evaluation stack as a local variable.
Program that uses short numbers [C#]
using System;
class Program
{
static void Main()
{
// Demonstrates a short value type used as local variable.
short number = 11;
Console.WriteLine(11);
number = -1; // Can be negative
Console.WriteLine(number);
Console.WriteLine(number == -1); // Can use == operator
Console.WriteLine(number + 100); // Can use + operator
Console.WriteLine(number.GetType());
Console.WriteLine(typeof(short));
Console.WriteLine(short.MinValue);
Console.WriteLine(short.MaxValue);
// Find the memory usage for a short value in a large array.
long bytes1 = GC.GetTotalMemory(false);
short[] array = new short[1000 * 1000];
array[0] = 1;
long bytes2 = GC.GetTotalMemory(false);
// Console.WriteLine(bytes1);
// Console.WriteLine(bytes2);
Console.WriteLine(((bytes2 - bytes1) / (1000 * 1000)).ToString() +
" bytes per short");
}
}
Output
11
-1
True
99
System.Int16
System.Int16
-32768
32767
2 bytes per short
Overview. The program shows how you can use the short type in a local variable declaration. The .NET Framework treats local variables separately from fields in classes, but you can also use the short type on classes, either instance or static. The program shows that you can assign shorts to a positive or negative number.
Incrementing and comparing short types. The program also shows how you can add values to a short local variable. This computation is expressed in the same way as adding numbers to integers. Often there is no difference in the results of these manipulations between ints and shorts. The evaluation stack in the execution engine stores short types in the same memory size and slots as integers.

System.Int16 alias. The short keyword in the C# programming language is always aliased to the "System.Int16" type in the .NET Framework's libraries. This mapping is referenced in the C# specification itself so it is invariant. You can use the Int16 type to explicitly indicate the size of the variable in your program if you want to.
Note: It is usually best to conform to any project guidelines first. If no guidelines exist, "short" is more common in C# programs than "System.Int16" and thus preferable.
Minimum and maximum value for short. The program next shows the values returned by evaluation of the short.MinValue and short.MaxValue fields. The very smallest decimal number a short can hold is -32768. The very largest decimal number a short can hold is 32767. Be careful not to use a negative maximum value as the minimum value, as this is not correct.
Memory usage of short. The program also demonstrates the memory size usage of the short type when it is stored in an array. The array is allocated on the managed heap. The GC.GetTotalMemory method shows that the short elements use 2 bytes of memory each. Note that not all shorts are allocated on the managed heap in this way; shorts that are local variables are stored in the evaluation stack or local variable slots.

As the program shows, a large array of shorts is half the physical size of a large array of ints. For numbers that are never extremely large, this can result in a significant memory size reduction. When a short element is read from an array, the runtime expands it to fit on the evaluation stack.
Also, using short local variables will not reduce memory usage and may degrade performance because these locals are stored in the evaluation stack as native integers. The book Expert .NET IL Assembler by Serge Lidin provides many details here.
Expert .NET 2.0 IL Assembler ReviewWe explored the short data type in the C# language when used as a local variable and also when allocated in an array on the managed heap. We saw that short types can be used like native integers as local variables, and then looked at the MinValue and MaxValue of the short type. We also saw that the short type is stored in 2 bytes of memory when in a large array. Short local variables are not more efficient than native ints in this context.
Number Examples