C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Resources are scarce. In some computer programs, memory use is excessive. The short type reduces the memory usage of integers. It represents a number in 2 bytes—16 bits—half the size of an int.
System.Int16 information short.MinValue = -32768 short.MaxValue = 32767
Example. First, this example shows how to 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.
Next: The program shows the memory usage of the short type on the managed heap, and its usage on the evaluation stack as a local variable.
C# program that uses short numbers 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
The program shows how to 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.
Next: The program shows that you can assign shorts to a positive or negative number.
Incrementing and comparing. 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 between ints and shorts.
Note: The evaluation stack in the execution engine stores short types in the same memory size and slots as integers.
Int16 alias. The short keyword is aliased to the "System.Int16" type. This mapping is referenced in the C# specification itself. It is invariant. You can use the Int16 type to explicitly indicate the size of a variable.
Tip: It is best to conform to project guidelines. If no guidelines exist, "short" is more common than "System.Int16" and thus preferable.
Minimum and maximum. The program next shows the values returned by short.MinValue and short.MaxValue. The smallest decimal number a short can hold is -32768. And the largest is 32767.
Warning: Be careful not to use a negative maximum value as the minimum value, as this is not correct.
Memory. A large array of shorts is half the physical size of a large array of ints. This results 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. It may degrade performance—these locals are stored in the evaluation stack as native integers. The book Expert .NET IL Assembler by Serge Lidin has many details.
Parse, TryParse. This program uses two string literals as the input data: "100" and "100000". The value 100 can be stored in a short. The value 100000 cannot be. The call to short.Parse succeeds without causing an error because 100 is a valid short.
So: With the two calls to short.TryParse, the first call succeeds and you can use the result, but the second call returns false.
C# program that uses short.Parse and short.TryParse using System; class Program { static void Main() { const string value1 = "100"; const string value2 = "100000"; // Can use short.Parse. short sh1 = short.Parse(value1); Console.WriteLine("sh1 = {0}", sh1); // Can use short.TryParse. short sh2; if (short.TryParse(value1, out sh2)) { Console.WriteLine("sh2 = {0}", sh2); } // Returns false: short.TryParse on number too large. short sh3; if (short.TryParse(value2, out sh3)) { Console.WriteLine("sh3 = {0}", sh3); // Never reached. } } } Output sh1 = 100 sh2 = 100
In this example, the second short.TryParse call returns false. 100000 cannot be stored in a short—there are not enough bits. If you were to call short.Parse("100000"), you encounter an exception. This would complicate your code.
Summary. We explored the short data type. We used it as a local variable. And we allocated it in an array on the managed heap. We saw that short types can be used like native integers as local variables.