ReadDouble Method

BinaryReader.ReadDouble Method ()


Reads an 8-byte floating point value from the current stream and advances the current position of the stream by eight bytes.

Namespace:   System.IO
Assembly:  mscorlib (in mscorlib.dll)

public virtual double ReadDouble()

Return Value

Type: System.Double

An 8-byte floating point value read from the current stream.

Exception Condition

The end of the stream is reached.


The stream is closed.


An I/O error occurs.

BinaryReader does not restore the file position after an unsuccessful read.

BinaryReader reads this data type in little-endian format.

For a list of common I/O tasks, see Common I/O Tasks.

The following code example shows how to read and write Double data to memory by using the BinaryReader and BinaryWriter classes on top of the MemoryStream class. MemoryStream only reads and writes Byte data.

using System;
using System.IO;

class BinaryRW
    static void Main()
        int i;
        const int arrayLength = 1000;

        // Create random data to write to the stream.
        Random randomGenerator = new Random();
        double[] dataArray = new double[arrayLength];
        for(i = 0; i < arrayLength; i++)
            dataArray[i] = 100.1 * randomGenerator.NextDouble();

        using(BinaryWriter binWriter = 
            new BinaryWriter(new MemoryStream()))
            // Write the data to the stream.
            Console.WriteLine("Writing data to the stream.");
            for(i = 0; i < arrayLength; i++)

            // Create a reader using the stream from the writer.
            using(BinaryReader binReader = 
                new BinaryReader(binWriter.BaseStream))
                    // Return to the beginning of the stream.
                    binReader.BaseStream.Position = 0;

                    // Read and verify the data.
                    Console.WriteLine("Verifying the written data.");
                    for(i = 0; i < arrayLength; i++)
                        if(binReader.ReadDouble() != dataArray[i])
                            Console.WriteLine("Error writing data.");
                    Console.WriteLine("The data was written " +
                        "and verified.");
                catch(EndOfStreamException e)
                    Console.WriteLine("Error writing data: {0}.",

Universal Windows Platform
Available since 4.5
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
Return to top
© 2015 Microsoft