Sunday, June 14, 2026

C# Byte Array Explained

Array

In C#, an array is strongly typed, meaning it can store only values of its declared element type.

For example:

int[] numbers = { 10, 20, 30 };     // Only int values
string[] names = { "A", "B" };      // Only string values
byte[] bytes = { 1, 2, 255 };       // Only byte values

The following would cause an error:

byte[] data = { 10, "Hello" }; // Error

Byte Array

In C#, an array stores multiple values of the same data type. Therefore, a Byte Array (byte[]) can store only byte values (e.g. 0, 1, 2, ..., 255). The size of the array is defined when it is created. Each element in a byte array occupies 1 byte (8 bits) of memory and can store values from 0 to 255 because byte in C# is an unsigned 8-bit integer.

Byte Array Example:

byte[] arr = new byte[4]; arr[0] = 57;
arr[1] = 48;
arr[2] = 0;
arr[3] = 254;

Memory Representation (1B stands for 1 byte):

Index:   0    1    2    3

Value:  57   48    0    0

Size:   1B   1B   1B   1B

No comments:

Post a Comment

Hot Topics