C# Short Type

Value Short

Short type

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 Keywords
System.Int16 information

short.MinValue = -32768
short.MaxValue =  32767

This C# example shows the short number type. This type occupies 16 bits.

Example

1

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
.NET Framework information

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.

The C# programming language

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.

Memory

Expert .NET 2.0 IL Assembler

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 Review

Parse and TryParse

Note

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. With the two calls to short.TryParse, the first call succeeds and you can use the result, but the second call fails: short.TryParse there returns false.

Program that uses short.Parse and short.TryParse [C#]

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

Why TryParse fails. In this example, the second short.TryParse call returns false because 100000 cannot be stored in a short value; there are simply not enough bits. If you were to call short.Parse("100000"), you would have to deal with an exception, which would probably complicate your code.

int.Parse int.TryParse TryParse

Summary

We 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
.NET