# MemoryBinaryWriter

> Provides a writer to write primitive types to a binary buffer in memory.

## Definition

* **Type:** Class
* **Namespace:** [Unity.Entities.Serialization](/engine/7000.0/script-reference/unity/entities/serialization.md)
* **Assembly:** UnityEngine.EntitiesModule
* **Implements:** [BinaryWriter](/engine/7000.0/script-reference/unity/entities/serialization/binarywriter.md), [IDisposable](https://learn.microsoft.com/dotnet/api/system.idisposable)

```csharp
public class MemoryBinaryWriter : BinaryWriter, IDisposable
```

## Remarks

This class can be used to serialize, for example, blob assets. The resulting binary buffer is stored in memory.

## Examples

```csharp
struct MyData
{
    public float embeddedFloat;
    public BlobString str;
}
public void WriteBlobAsset()
{
    unsafe
    {
        var writer = new MemoryBinaryWriter();
        var blobBuilder = new BlobBuilder(Allocator.Temp);
        ref var root = ref blobBuilder.ConstructRoot<MyData>();
        builder.AllocateString(ref root.str, "Hello World!");
        root.embeddedFloat = 4;
        BlobAssetReference<MyData>.Write(writer, blobBuilder, kVersion);
    }
}
```

## Constructors

| Constructor                                                                                                                                               | Description              |
| --------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------ |
| [MemoryBinaryWriter(Unity.Entities.EntityManager,System.String)](/engine/7000.0/script-reference/unity/entities/serialization/memorybinarywriter/ctor.md) | Creates an empty writer. |

## Properties

| Property                                                                                                | Description                                                        |
| ------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------ |
| [Data](/engine/7000.0/script-reference/unity/entities/serialization/memorybinarywriter/data.md)         | A pointer to the data that has been written to memory.             |
| [Length](/engine/7000.0/script-reference/unity/entities/serialization/memorybinarywriter/length.md)     | The total length of the all written data.                          |
| [Position](/engine/7000.0/script-reference/unity/entities/serialization/memorybinarywriter/position.md) | Gets or sets the current write position of the MemoryBinaryWriter. |

## Methods

| Method                                                                                                      | Description                                                                                           |
| ----------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------- |
| [Dispose](/engine/7000.0/script-reference/unity/entities/serialization/memorybinarywriter/dispose.md)       | Disposes the MemoryBinaryWriter.                                                                      |
| [WriteBytes](/engine/7000.0/script-reference/unity/entities/serialization/memorybinarywriter/writebytes.md) | Writes the specified number of bytes and advances the current write position by that number of bytes. |
