
Byte arrays store binary data. This data may be part of a data file, image file, compressed file, downloaded server response, or many other files. With byte arrays, we have an ideal representation of this data—it has an accurate representation in memory.
Tip: The byte array type allows you to store low-level representations. It is useful when optimizing certain methods.

This tiny program shows how you can allocate a byte array on the managed heap in the C# language. Additionally, the program measures the memory usage of the managed heap before and after this allocation occurs. The program demonstrates that a byte array in the C# language is a precise representation of bytes in computer science, and not a higher-level construct.
Program that allocates byte array [C#]
using System;
class Program
{
static void Main()
{
byte[] array1 = null;
//
// Allocate three million bytes and measure memory usage.
//
long bytes1 = GC.GetTotalMemory(false);
array1 = new byte[1000 * 1000 * 3];
array1[0] = 0;
long bytes2 = GC.GetTotalMemory(false);
Console.WriteLine(bytes2 - bytes1);
}
}
Output
3000032
Memory usage of byte array. The program begins its execution at the Main entry point. The GC.GetTotalMemory method first gathers the memory usage figures from the system before and after the allocation of the byte array. The byte array variable is assigned a reference to three million bytes in an array allocated on the managed heap.
The program proves that allocating a three million element array of bytes on the managed heap causes almost exactly three million bytes to be added to the memory usage of the program. This proves that the byte type in the C# language is a low-level type that has almost no overhead when part of an array. It is safe to use where memory usage and performance are critical.
You can read in a byte array from a file using the C# programming language and the System.IO namespace. The easiest way to read a file into a binary array is the File.ReadAllBytes static public method in the File class. You can find a detailed tutorial on the ReadAllBytes method here.
File HandlingYou can use the Seek method on some Stream classes to read a part of a file into a byte array in the C# programming language. The article linked here demonstrates that it is faster to read in an entire byte array from a file at once rather than one byte at a time, which will utilize the file system's buffering more effectively. You will find that byte[] arrays are used in many file input/output programs.
Seek File Examples
The ASP.NET Framework commonly used with the C# language makes extensive use of byte arrays in its internal code and also in some public interfaces. You can use the BinaryWrite method and provide a byte[] array parameter to achieve a very fast write to the HTTP response. Some websites use this approach to optimize server-side performance extensively. At the source level of the C# code, byte arrays are used frequently in this approach.
Response.BinaryWriteIt is also possible to modify the contents and length of a byte[] array using the C# programming language. The link demonstrates how you can rewrite a binary file encoded in GZIP to remove parts of the binary format, resulting in superior data compression. You can rewrite some byte arrays that contain images or other binary formats to develop a program that can manipulate these kinds of files.
GZIP Header Flag Byte
We can test the individual array elements in an allocated byte array using the C# programming language. If you have a GZIP file in an array, you can test the first two elements and the length to determine that the file contains the correct magic header bytes defined in the file format specification. This process is also called "data sniffing" and is used constantly on the Internet and in web browsers.
GZIP File TestYou can download data over the Internet or other network with a network connection using the WebClient class in the C# programming language. The WebClient provides a way to directly access the bytes of any server's response, and the data is returns is stored in a byte[] array. This is useful for scraping web sites or automating tedious Internet tasks for your users.
WebClient Tutorial
The GZipStream class in the .NET Framework and C# language allows you to use a byte[] array buffer for expanding or compressing GZIP-encoded data. The GZipStream class is the easiest way to implement compression or decompression for web applications directly inside the C# code in your project. Byte arrays are also used for FileStream instances, and more information is available in the GZipStream article.
GZipStream ExampleWhen you are using byte[] arrays, you are usually just storing the exact data from the file system in the memory of your program. In some cases, this actually duplicates the Windows file system cache mechanism that is optimized for this exact purpose. The article linked here demonstrates how byte[] array caches on the file system can improve or degrade performance in various situations.
File Read BenchmarksThe BinaryReader class allows you to imperatively read in a binary file with more helper methods than testing the bytes yourself. The BinaryReader class allows you to transform individual bytes into integers or strings very easily and is highly recommended for some kinds of binary files.
BinaryReader Tutorial
We looked at the byte array type in the C# language, seeing its memory allocation statistics. Next we provided pointers to many other articles that also focus on the byte array type in specific usage scenarios encountered in practical programming tasks. Byte arrays provide an accurate machine representation of binary data that is addressable by byte positions.
Array Types