Export (0) Print
Expand All

Creating a Writer

The following code example creates a writer, which is a class that can take data of some type and convert it to a byte array that can be passed to a stream.


using System;
using System.IO;

public class MyWriter
{
    private Stream s;

    public MyWriter(Stream stream)
    {
        s = stream;
    }

    public void WriteDouble(double myData)
    {
        byte[] b = BitConverter.GetBytes(myData);
        // GetBytes is a binary representation of a double data type.
        s.Write(b, 0, b.Length);
    }

    public void Close()
    {
        s.Close();
    }
}


In this example, you create a class that has a constructor with a stream argument. From here, you can expose whatever Write methods are necessary. You must convert whatever you are writing to a byte[]. After you obtain the byte[],the Write method writes it to the stream s.

Community Additions

ADD
Show:
© 2014 Microsoft