C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Here: 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.
Increment: You can add values to a short local variable. This computation is expressed in the same way as adding numbers to integers.
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.
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
System.Int16 information:
short.MinValue = -32768
short.MaxValue = 32767
System.UInt16 information:
ushort.MinValue = 0
ushort.MaxValue = 65535
Info: The call to short.Parse succeeds without causing an error because 100 is a valid short.
So: With the 2 calls to short.TryParse, the first call succeeds and you can use the result, but the second call returns false.
True, FalseC# 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
Locals: Local ushort variables are stored on the method stack. The ushort type occupies 16 bits—it is half the size of an int value.
Typeof: The typeof operator uses an optimized reflection method to get the type pointer for an object type.
Typeof, nameofAnd: The type of a ushort variable is actually a System.UInt16 type, because ushort is aliased to that type in the System namespace.
Array: We use an array of unsigned shorts. The elements in the array are always initialized to have all their bits set to zero.
Int ArrayC# 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
Version 1: The method loops over a ushort and increments one. The loop branches on a ushort (in a loop boundary test).
Version 2: This code uses int instead of ushort. It performs the same actions as the ushort method but with int.
Result: It is faster to use an int in loops and increment statements. Ushort does not help runtime speed in this test.
C# program that tests ushort performance
using System;
using System.Diagnostics;
using System.Runtime.CompilerServices;
class Program
{
const int _max = 1000000;
static void Main()
{
// Version 1: use ushort method.
var s1 = Stopwatch.StartNew();
for (int i = 0; i < _max; i++)
{
Test1();
}
s1.Stop();
// Version 2: use int method.
var s2 = Stopwatch.StartNew();
for (int i = 0; i < _max; i++)
{
Test2();
}
s2.Stop();
Console.WriteLine(((double)(s1.Elapsed.TotalMilliseconds * 1000000) /
_max).ToString("0.00 ns"));
Console.WriteLine(((double)(s2.Elapsed.TotalMilliseconds * 1000000) /
_max).ToString("0.00 ns"));
}
static void Test1()
{
ushort result = 0;
for (ushort i = 0; i < 100; i++)
{
result++;
}
}
static void Test2()
{
int result = 0;
for (int i = 0; i < 100; i++)
{
result++;
}
}
}
Output
62.54 ns ushort method
35.68 ns int method
And: It detects the assignment of a negative constant to the ushort variable, and gives you a helpful error.
Note: This will help you avoid hard-to-find bugs later in the program's development.
C# program that causes ushort error
class Program
{
static void Main()
{
ushort test = -1;
}
}
Output
Error CS0031
Constant value '-1' cannot be converted to a 'ushort'
Note: Generally programming languages use the "int" type to indicate the integer type that is fastest for the computer to use.
Also: Using short local variables will not reduce memory usage. It may degrade performance.
Locals: Short locals are stored in the evaluation stack as native integers. Expert .NET IL Assembler, by Serge Lidin, has many details.