VB.NET IntegerExamine the Integer type, which is 4 bytes, along with Int32, MinValue and MaxValue.
dot net perls
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.
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.
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).
VB.NET program that tests Integer type
' Part 1: specify an Integer Dim.
Dim number As Integer
number = -1
' Part 2: use expressions.
Console.WriteLine(number = -1)
Console.WriteLine(number + 100)
' Part 3: get type and maximum and minimum values.
' 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)
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
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).
VB.NET program that uses nullable Integer
' A nullable Integer can be a numeric value, or Nothing.
Dim test As Integer?
test = Nothing
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.
For Each, For
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.
© 2007-2021 sam allen. send bug reports to firstname.lastname@example.org.