C# Binarywriter - C# Tutorial
C# Course / File I/O / C# Binarywriter

C# Binarywriter

BLUF: Mastering C# Binarywriter is essential for building robust applications with the .NET ecosystem. This tutorial provides clear explanations and practical examples to help you understand and apply this C# concept.
Enterprise Development Tip: C# Binarywriter

C# is a powerful, modern language for enterprise solutions. Discover how C# Binarywriter enhances your development workflow in the guide below.

The BinaryWriter class in C# is employed for composing binary data into a stream. This class is located within the System.IO namespace and provides functionality for encoding and writing strings into the stream with a specified encoding format.

C# BinaryWriter Example

Let's explore a basic illustration of the BinaryWriter class that is responsible for storing data in a .dat file.

Example

using System;
using System.IO;
namespace BinaryWriterExample
{
    class Program
    {
        static void Main(string[] args)
        {
            string fileName = "e:\\binaryfile.dat";
            using (BinaryWriter writer = new BinaryWriter(File.Open(fileName, FileMode.Create)))
            {
                writer.Write(2.5);
                writer.Write("this is string data");
                writer.Write(true);
            }
            Console.WriteLine("Data written successfully...");  
        }
    }
}

Output:

Output

Data written successfully...

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience