TheDeveloperBlog.com


C# Byte Type

Byte. A byte is 8 bits. The byte type, in the .NET Framework, is a compact and efficient type. Byte arrays are useful more often than single bytes. They can store file data. Sometimes bits are even more efficient.

Sbyte
System.Byte information

byte.MinValue = 0
byte.MaxValue = 255


Example. Let us begin. This program uses the byte type. This type is a value type, which means its storage location is directly contained in the evaluation stack when used as a local variable.

Tip: You can assign the byte variable any value between 0 and 255 without an error.

Info: The program proves this by demonstrating the result of several expressions related to the byte type.

Based on:

.NET 4.5

C# program that uses byte type

using System;

class Program
{
    static void Main()
    {
	// You can assign an integer to a byte variable.
	byte value = 5;
	Console.WriteLine(value);

	// The byte type includes a minimum value and maximum value.
	Console.WriteLine(byte.MinValue);
	Console.WriteLine(byte.MaxValue);

	// The byte type has a Type pointer and default value.
	Console.WriteLine(typeof(byte));
	Console.WriteLine(typeof(Byte)); // Uppercase Byte
	Console.WriteLine(default(byte));
    }
}

Output

5
0
255
System.Byte
System.Byte
0

The program performs a series of accesses to a byte variable on the evaluation stack. The variable is of byte type and is assigned the integer value 5, which fits nicely into the storage location.

Also: The program shows the minimum value, maximum value and type of the byte struct.

Next, we see that the minimum value (byte.MinValue) is equal to zero. And the maximum value (byte.MaxValue) is equal to 255. A value greater than 255 will cause an overflow error.

OverflowException

Checked: The behavior during an overflow error is determined by the overflow context, such as a checked or unchecked context.

CheckedUnchecked

Typeof. The typeof operator returns a managed pointer. This pointer describes the type of the argument. The expression typeof(byte), which can be uppercase or lowercase, is equal to System.Byte.

Typeof

And: You can substitute System.Byte wherever you use byte. The default value of any value type such as byte is equal to zero.

Default

Example 2. Consider this example. The C# compiler will automatically treat some numbers as bytes (or other types) in a program. So it passes the value 10 as a byte value. But it cannot treat 1000 as a byte, so it causes an error.

Tip: The compiler uses a special type of error, a compile-time error, to prevent incorrect programs from ever running.

Compile-Time Error
C# program that uses byte arguments

using System;

class Program
{
    static void Main()
    {
	// The 10 is automatically treated as a byte.
	Test(10);
	// This does not compile:
	// Test(1000);
    }

    static void Test(byte value)
    {
	Console.WriteLine(value);
    }
}

Output

10


Char. There are some types that are related to, or similar to, the byte type. In programming languages such as C or C++, the char type is only one byte, which makes it equivalent to the byte type in the C# language.

However: In the .NET Framework, the System.Char type is two bytes and requires twice as much storage.

Char

Tip: The ushort type is another small integer type. It is best to use the smallest type that fits your range of data.

Ushort

Arrays. An important use of bytes in C# programs is through byte arrays. These arrays can store binary data very efficiently. Several file handling methods, such as File.ReadAllBytes, can also be used with byte arrays.

Byte ArrayFile.ReadAllBytes

BitArray. Bytes have eight bits. But if we only need a true or false value, we can use a single bit. If many thousands of values are needed, a BitArray can greatly reduce memory usage over a byte array.

BitArray

Summary. Byte variables can contain the values between 0 and 255. The type is a value type that is stored directly in the evaluation stack in the execution engine when used as a local variable.