C# BinaryWriter Write(UInt16)

Description

BinaryWriter Write(UInt16) Writes a two-byte unsigned integer to the current stream and advances the stream position by two bytes.

Syntax

BinaryWriter.Write(UInt16) has the following syntax.


[CLSCompliantAttribute(false)]
public virtual void Write(
  ushort value
)

Parameters

BinaryWriter.Write(UInt16) has the following parameters.

  • value - The two-byte unsigned integer to write.

Returns

BinaryWriter.Write(UInt16) method returns

Example


using System;//from   w ww . j av  a2 s.  c om
using System.IO;

class ConsoleApplication
{
    const string fileName = "data.dat";

    static void Main()
    {
        using (BinaryWriter writer = new BinaryWriter(File.Open(fileName, FileMode.Create)))
        {
            writer.Write((UInt16)123);
        }
        using (BinaryReader reader = new BinaryReader(File.Open(fileName, FileMode.Open)))
        {
            Console.WriteLine(reader.ReadUInt16());
        }
    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System.IO »




BinaryReader
BinaryWriter
Directory
DirectoryInfo
DriveInfo
File
FileInfo
FileStream
MemoryStream
Path
StreamReader
StreamWriter
StringReader
StringWriter