C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Note: If you try to assign a negative value to Byte, a compile-time error may occur.
And: If the compiler cannot detect the error on its own, runtime behavior may be unexpected.
Alias: This type is aliased to the System.Byte type. System.Byte is a composite name—it is easier to just specify Byte in your VB.NET program.
VB.NET program that uses Byte type
Module Module1
Sub Main()
' Byte variable.
Dim value As Byte = 5
Console.WriteLine(value)
' Min and Max.
Console.WriteLine(Byte.MinValue)
Console.WriteLine(Byte.MaxValue)
' Types.
Console.WriteLine(value.GetType())
Console.WriteLine(value.GetTypeCode())
' Memory usage (allocate 1 million and 1 elements).
Dim a As Long = GC.GetTotalMemory(False)
Dim arr(1000 * 1000) As Byte
arr(0) = 1
Dim b As Long = GC.GetTotalMemory(False)
Console.WriteLine((b - a) / (1000 * 1000))
End Sub
End Module
Output
5
0
255
System.Byte
6
1.000032
Tip: The program allocates 1 million and 1 elements in the array—in VB.NET, we specify the last index, so we have an extra element.
Tip 2: When an array is allocated in a VB.NET program, it requires both memory for the array reference, and the array object data.
Also: Byte can be beneficial for when file formats need to be read or parsed at the level of the binary. The BinaryReader type is helpful.
BinaryReaderVB.NET program that uses SByte
Module Module1
Sub Main()
' Display minimum and maximum values for SByte.
Dim value As SByte = SByte.MinValue
Console.WriteLine(value)
value = SByte.MaxValue
Console.WriteLine(value)
End Sub
End Module
Output
-128
127