This is a core type in VB.NET programs. It is the default numeric type. Usually loops use the Integer type for their indexes.
Because it is everywhere, it is worthwhile to explore this .NET type in more depth. This is a 4-byte numeric type. We can measure its memory usage in an array.
We test some important parts of the Integer type. The Integer is a Structure
, but we think of it as a low-level native type in VB.NET.
Dim
statement. It can store positive values, such as 1, and negative values, such as -1.System.Int32
type. It has a range of over 4 billion values.Module Module1 Sub Main() ' Part 1: specify an Integer Dim. Dim number As Integer = 1 Console.WriteLine(number) number = -1 Console.WriteLine(number) ' Part 2: use expressions. Console.WriteLine(number = -1) Console.WriteLine(number + 100) ' Part 3: get type and maximum and minimum values. Console.WriteLine(number.GetType()) Console.WriteLine(Integer.MaxValue) Console.WriteLine(Integer.MinValue) ' Part 4: compute memory per Integer. Dim bytes1 = GC.GetTotalMemory(False) Dim array(100000) As Integer array(0) = 1 Dim bytes2 = GC.GetTotalMemory(False) Console.WriteLine((bytes2 - bytes1) / 100000) End Sub End Module1 -1 True 99 System.Int32 2147483647 -2147483648 4.00032
It is worthwhile to emphasize the minimum and maximum values for an integer. Often our programs will access Integer.MinValue
and MaxValue
for bounds.
Mapped to: System.Int32 MinValue: -2147483648 MaxValue: 2147483647
An Integer is a value type, so it cannot be set to Nothing. But we can wrap an Integer in a Nullable Structure
, with special syntax in VB.NET.
GetValueOrDefault
to safely get the underlying numeric value (if any).Module Module1 Sub Main() ' A nullable Integer can be a numeric value, or Nothing. Dim test As Integer? = 100 Console.WriteLine("GETVALUEORDEFAULT: {0}", test.GetValueOrDefault()) test = Nothing Console.WriteLine("GETVALUEORDEFAULT: {0}", test.GetValueOrDefault()) End Sub End ModuleGETVALUEORDEFAULT: 100 GETVALUEORDEFAULT: 0
As a fundamental type, the Integer is used in For loops as an index. In languages, the integer type (int
) is usually optimized for performance.
int
as a local variable is the fastest. It is worth keeping int
for loop indexes.Integer is a fundamental type in VB.NET programs. It is mapped to System.Int32
, which makes it precisely equivalent, in the C# language, to int
. It requires four bytes of memory.
Integer has well-known lower and upper values. It has low-level optimizations for performance. This core type is worth researching. It is used throughout software projects.