C# Ushort Example

Ushort type

Unsigned numeric types 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. Aliased to the System.UInt16 type in the .NET Framework, ushort is available in the C# language.

Short Type Keywords
System.UInt16 information

ushort.MinValue = 0
ushort.MaxValue = 65535

Overview: This C# article explores the ushort integral type. It shows how you can find its typeof value and create an array of instances.

Example

The ushort type is a value type: the actual value of the variable is stored in the storage location memory. Local variables of type ushort will be stored on the method stack, not in the managed heap. The ushort type occupies 16 bits, making it half the size of an int value.

Program that uses ushort [C#]

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
Main method

Ushort declaration. The program text above 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.

Typeof operator

Typeof ushort. The typeof operator in the C# programming language uses an optimized reflection method to get the type pointer for an object type. The type of a ushort variable is actually a System.UInt16 type, because ushort is aliased to that type in the System namespace.

Typeof Operator

Ushort array. You can declare and 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 array type uses less memory than int arrays and improves memory usage on very large arrays.

Ushort minimum value and maximum value. The final part of the program text above prints the MinValue and MaxValue constant fields on the ushort type to the screen. The minimum value of the ushort type of 0, while the maximum value is 65535. If you have an array of ushort length, its last offset value will be 65534.

Compile-time errors

This is a warning

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

Compile-Time Error

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

Advantages

Garbage collection visualization

There are some advantages of using the ushort type in the C# language. Because the ushort type 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, but in small arrays it may decrease performance. Generally programming languages use the "int" type to indicate the integer type that is fastest for the computer to use.

Compact arrays. 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. The BitArray will use a single bit for each index, while a bool array will use 8 bits for each value.

Bool Array Memory

Summary

The C# programming language

We looked at the ushort numeric type in the C# programming language. This type is excellent for using in large arrays to save memory usage over other types, but will not offer consistent performance advantages in other cases. We saw an example of the System.UInt16 type, the MaxValue property and the MinValue property on the ushort type, and an error causes by ushort usage.

Number Examples
Dot Net Perls