Value Byte

A byte is 8 bits. The byte type, in the .NET Framework and C# language, is an 8-bit value type that is compact and efficient. Byte arrays are useful more often than single bytes: they can store file data. The primary limitation of the byte type is that it can only represent the numbers between 0 and 255.
Sbyte Type KeywordsSystem.Byte information byte.MinValue = 0 byte.MaxValue = 255
Overview: This C# example shows the byte number type. Byte requires 8 bits. It represents 0 to 255.
First here we look at an example program that demonstrates the usage of the byte type in the C# language. The byte type is a value type, which means its storage location is directly contained in the evaluation stack when used as a local variable. You can assign the byte variable any value between 0 and 255 without an error. The program proves this by demonstrating the result of several expressions related to the byte type.
Program that uses byte type [C#]
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
0Declaring and assigning bytes. 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. The program also shows the minimum value, maximum value and type of the byte struct.
Minimum and maximum values. The program shows that the minimum value as returned by byte.MinValue is equal to zero and the maximum value as returned by byte.MaxValue is equal to 255. Therefore, you cannot have a byte variable have a value greater than 255 or an overflow error will occur. The behavior during an overflow error is determined by the overflow context, such as a checked or unchecked context.
Checked Context Unchecked Context OverflowException
Typeof/default. The typeof operator returns a managed pointer that describes the type of the argument. The typeof(byte), which can be uppercase or lowercase, is equal to System.Byte. You can actually substitute System.Byte wherever you use byte. The default value of any value type such as byte is equal to zero.
Typeof Operator Default
Here we mention some types in the C# programming language 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. In the .NET Framework, the System.Char type is two bytes and requires twice as much storage. The ushort type is another small integer type you can use.
Char Type Ushort Example
Byte arrays. One of the most important uses 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 Array File.ReadAllBytes Tips
We looked at some important parts of the byte type. 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. Finally we noted related types in the language and how they compare to the byte type shown here.
Number Examples