TheDeveloperBlog.com


C# Ushort Example

Ushort is aliased to System.UInt16. Unsigned numbers cannot be negative. They represent a greater range of positive numbers than do signed. The ushort type—unsigned short—uses its 16 bits to represent the numbers between 0 and 65535.

Short
System.UInt16 information

ushort.MinValue = 0
ushort.MaxValue = 65535


Example. The ushort type is a value type. The actual value of the variable is stored in the storage location memory. Local ushort variables are stored on the method stack. The ushort type occupies 16 bits—it is half the size of an int value.

C# program that uses ushort

using System;

class Program
{
    static void Main()
    {
	// Declare and initialize ushort value.
	ushort value1 = 1000;

	// This won't compile:
	// value1 = -1;

	// Write value.
	Console.WriteLine(value1);

	// Write type of ushort.
	Console.WriteLine(typeof(ushort));

	// Alternative syntax for declaring a ushort value.
	UInt16 value2 = 1000;

	// Write value.
	Console.WriteLine(value2);

	// Declare a ushort array.
	ushort[] array1 = new ushort[2];
	array1[0] = 5;

	// Write array elements.
	Console.WriteLine(array1[0]);
	Console.WriteLine(array1[1]); // <-- Default is zero.

	// Write minimum and maximum values.
	Console.WriteLine(ushort.MinValue);
	Console.WriteLine(ushort.MaxValue);
    }
}

Output

1000
System.UInt16
1000
5
0
0             <- Min
65535         <- Max

The program defines the Main entry point and in the Main method it declares a ushort variable called "value1". The variable is assigned to the value 100. This is an appropriate value for a ushort variable.

Tip: The typeof operator uses an optimized reflection method to get the type pointer for an object type.

Typeof

And: The type of a ushort variable is actually a System.UInt16 type, because ushort is aliased to that type in the System namespace.

We use an array of unsigned shorts in the same way as any other value type array. The elements in the array are always initialized to have all their bits set to zero. This uses less memory than int arrays and improves memory usage.

Int Array

Minimum and maximum value. The final part of the program prints the MinValue and MaxValue constants. The minimum value is 0, while the maximum value is 65535. In an array of ushort length, the last offset value will be 65534.

Console.WriteLine

Compile-time errors. The program also shows a common compile-time error you will encounter in programs that use ushort variables. At compile-time, the C# compiler uses a checked context to check for overflow of numeric variables.

Compile-Time Error

It detects the assignment of a negative constant to the ushort variable, and gives you a helpful compile-time error. This will help you avoid hard-to-find bugs later in the program's development.

Ushort error:

Constant value '-1' cannot be converted to a 'ushort' ...


Advantages. There are advantages to ushort. Because it is only 16 bits, it will use about one-half the memory in an array as the int or uint type. In very large arrays, this can improve performance. In small arrays it may decrease performance.

Note: Generally programming languages use the "int" type to indicate the integer type that is fastest for the computer to use.

If you have to store many small values, using a BitArray collection or an array of bool values is more compact than an array of ushort values. A BitArray uses a single bit for each index. A bool array uses 8 bits for each value.

BitArrayBool Array

Summary. We looked at the ushort numeric type in the C# language. This type is excellent for using in large arrays to save memory usage over other types. But it does not offer consistent performance advantages in other cases.