IntegerExamine the Integer type, which is 4 bytes, along with Int32, MinValue and MaxValue.
This page was last reviewed on Apr 14, 2023.
Integer. This is a core type in VB.NET programs. It is the default numeric type. Usually loops use the Integer type for their indexes.
Integer details. 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.
Byte, Sbyte
An example. 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.
Part 1 An Integer is declared with a Dim statement. It can store positive values, such as 1, and negative values, such as -1.
Part 2 Expressions in VB.NET do not change the value of variables—unless the variables are assigned to the expression result.
Part 3 In this language, an Integer is mapped to the System.Int32 type. It has a range of over 4 billion values.
Part 4 We allocate an Integer array. We determine that each Integer occupies 4 bytes (all data is stored within the array).
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 Module
1 -1 True 99 System.Int32 2147483647 -2147483648 4.00032
Some important values. 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
Nullable Integer. 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.
And The nullable structure can be set to Nothing. We can use 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 Module
Loops. 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.
Thus In most programs, the int as a local variable is the fastest. It is worth keeping int for loop indexes.
A summary. 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.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Apr 14, 2023 (edit).
© 2007-2024 Sam Allen.