This type is a fundamental unit of memory. It contains eight bits. Other VB.NET types such as Integer are measured in terms of bytes.
A byte
is the smallest addressable unit. To change bits, bitwise operators must be used. Often in VB.NET programs we employ Byte arrays.
Just like an Integer, the Byte can be declared and initialized in a Dim
statement. Next we see that the Byte's minimum value is zero. The maximum value is 255.
System.Byte
type. System.Byte
is a composite name—it is easier to just specify Byte in a program.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 Module5 0 255 System.Byte 6 1.000032
SByte
A Byte is unsigned—it has no sign bit. But SByte
is a signed Byte—it has the same representation size but includes a sign bit.
SByte
type can store the values -128 (its MinValue
) through 127 (its MaxValue
).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-128 127
The reference is often stored on the evaluation stack in the virtual
execution environment. But the array object data is always stored in a separate place—the managed heap.
The Byte type is probably most useful as part of an array type. An array of bytes can store any complete file in memory. This can be useful for optimizations such as caches.
BinaryReader
type is helpful.A Byte can store values between, and including, zero and 255. It has no sign bit so cannot store a negative value. Byte arrays represent binary data files in memory.