HOWTO · Csharp
How to Initialize a Byte Array in C#
Learn how to create a byte array of a specific length in C#, set its values, and copy raw bytes.
On this page
A C# byte[] stores byte values from 0 through 255. Use new byte[length] to create an array whose elements initially contain zero, or use an array initializer when the values are already known. A loop, Array.Fill, and LINQ can assign repeated or calculated values; Buffer.BlockCopy is for copying bytes already stored in arrays.
Create a Byte Array with a Fixed Length or Explicit Values
To create a fixed-size byte[], pass its length to new byte[length]. The runtime initializes each element to zero. When you know the values in advance, write them in an initializer instead. A loop is useful when each value depends on its index or another calculation.
The declarations below show two common forms side by side. The first reserves four positions, and the second lists three byte values directly. The loop then visits each position in order and stores a value based on its index. Length supplies the number of positions, so the same loop shape adapts when you change the requested size. The final lines print both arrays so you can compare the values produced by each form.
using System;
class Program
{
static void Main()
{
byte[] values = new byte[4];
byte[] explicitValues = { 0x10, 0x20, 0x30 };
for (int i = 0; i < values.Length; i++)
{
values[i] = (byte)(i + 1);
}
Console.WriteLine(string.Join(", ", values));
Console.WriteLine(string.Join(", ", explicitValues));
}
}
1, 2, 3, 4
16, 32, 48
For the first array, the loop stores 1, 2, 3, and 4. The explicit initializer creates a separate array containing the listed hexadecimal byte values.
Set Every Element to the Same Value
Use Array.Fill when the project’s target framework provides it. It assigns one byte value to every element in the specified array. If that API is unavailable for the project, a for loop over the array’s Length provides the same assignment.
using System;
class Program
{
static void Main()
{
byte[] buffer = new byte[4];
Array.Fill(buffer, (byte)255);
Console.WriteLine(string.Join(", ", buffer));
}
}
255, 255, 255, 255
The cast makes the value’s type explicit. A byte can hold values from 0 through 255.
Create an Array from a Repeated Value with LINQ
Enumerable.Repeat produces a sequence containing a value the requested number of times. Call ToArray to materialize that sequence as a byte[]. The example imports System.Linq, which supplies both methods.
using System;
using System.Linq;
class Program
{
static void Main()
{
byte value = 42;
int length = 5;
byte[] values = Enumerable.Repeat(value, length).ToArray();
Console.WriteLine(string.Join(", ", values));
}
}
42, 42, 42, 42, 42
This approach is readable when a new array should contain the same value throughout. Use Array.Fill when you already have an array to update.
Copy Raw Bytes with Buffer.BlockCopy
Buffer.BlockCopy copies raw bytes between arrays whose element types are primitive types, such as byte[] or int[]. Its source and destination offsets and its count are measured in bytes, not array elements. Ensure the source and destination ranges have enough space for the requested copy. It does not generate or fill a general value; for that task, use a loop or Array.Fill.
This example copies four bytes from a source byte array into a destination array starting at byte offset one.
using System;
class Program
{
static void Main()
{
byte[] source = { 10, 20, 30, 40 };
byte[] destination = new byte[6];
Buffer.BlockCopy(source, 0, destination, 1, source.Length);
Console.WriteLine(string.Join(", ", destination));
}
}
0, 10, 20, 30, 40, 0
When copying bytes from a larger numeric array, the copy count and offsets still use bytes. For example, this copies the first four bytes from two int values into a four-byte destination:
using System;
class Program
{
static void Main()
{
int[] numbers = { 0x01020304, 0x05060708 };
byte[] bytes = new byte[4];
Buffer.BlockCopy(numbers, 0, bytes, 0, bytes.Length);
Console.WriteLine(string.Join(", ", bytes));
}
}
The numeric-array example is platform-dependent: the order of bytes in each int depends on the platform’s representation. In the supplied environment, this example produced 4, 3, 2, 1; that observed order is specific to the platform’s integer byte representation and is not universal. Copying from a byte[] avoids that interpretation because its elements are already bytes.
Choose an Initialization Method
Use new byte[length] when you need a fixed-size array initialized with zeros, and an initializer for a short list of known values. Use a loop for values calculated by position, Array.Fill for assigning one value to an existing array, or Enumerable.Repeat(...).ToArray() to construct a repeated-value array with LINQ. Choose Buffer.BlockCopy when the data already exists as raw bytes in arrays and needs to be copied.