---
title: "What are the differences between FileStream.Flush(), FlushAsync(), and Dispose()?"  
description: "What are the differences between FileStream.Flush(), FlushAsync(), and Dispose()?"  
author: "ICSM Computer"  
published: 2025-05-13  
updated: 2025-05-13  
canonical: https://www.mindstick.com/interview/34113/what-are-the-differences-between-filestream-flush-flushasync-and-dispose  
category: "c#"  
tags: ["c#", "file handling"]  
reading_time: 3 minutes  

---

# What are the differences between FileStream.Flush(), FlushAsync(), and Dispose()?

Here’s a clear breakdown of the differences between `FileStream.Flush()`, `FlushAsync()`, and `Dispose()` in C#:

### `Flush()`

1. **Purpose:** Forces all buffered data to be written from memory to disk.
2. **Synchronous**
3. **Does not close** the stream.

```cs
fileStream.Flush();
```

**Use when** you want to make sure data is written immediately but **continue using** the stream.

### `FlushAsync()`

1. **Purpose:** Same as `Flush()`, but performs the operation **asynchronously**.
2. Returns a `Task`, so it can be `await`ed.
3. Useful for **non-blocking I/O** in responsive or high-performance applications.

```cs
await fileStream.FlushAsync();
```

**Use when** you're working in an `async` method and want to **avoid blocking**.

### `Dispose()` (or `Close()`)

1. **Purpose:** Flushes buffers **and releases all resources**, including the underlying file handle.
2. Automatically **calls** `Flush()`, then disposes.
3. Once disposed, the stream **cannot be reused**.

```cs
fileStream.Dispose();
// OR with using:
using (var fs = new FileStream(...))
{
    // ... use fs
} // Dispose() is called automatically here
```

**Use when** you're done with the stream.

### Summary Table:

| Method | Flushes Data | Asynchronous | Closes Stream |
| --- | --- | --- | --- |
| `Flush()` | Yes | No | No |
| `FlushAsync()` | Yes | Yes | No |
| `Dispose()` | Yes | No | Yes |

### Best Practices:

1. Use `using` or `await using` blocks to ensure `Dispose()` is always called, even on exceptions.
2. Call `Flush()` or `FlushAsync()` if you want to write changes *without closing* the file.

## Answers

### Answer by ICSM Computer

Here’s a clear breakdown of the differences between `FileStream.Flush()`, `FlushAsync()`, and `Dispose()` in C#:

### `Flush()`

1. **Purpose:** Forces all buffered data to be written from memory to disk.
2. **Synchronous**
3. **Does not close** the stream.

```cs
fileStream.Flush();
```

**Use when** you want to make sure data is written immediately but **continue using** the stream.

### `FlushAsync()`

1. **Purpose:** Same as `Flush()`, but performs the operation **asynchronously**.
2. Returns a `Task`, so it can be `await`ed.
3. Useful for **non-blocking I/O** in responsive or high-performance applications.

```cs
await fileStream.FlushAsync();
```

**Use when** you're working in an `async` method and want to **avoid blocking**.

### `Dispose()` (or `Close()`)

1. **Purpose:** Flushes buffers **and releases all resources**, including the underlying file handle.
2. Automatically **calls** `Flush()`, then disposes.
3. Once disposed, the stream **cannot be reused**.

```cs
fileStream.Dispose();
// OR with using:
using (var fs = new FileStream(...))
{
    // ... use fs
} // Dispose() is called automatically here
```

**Use when** you're done with the stream.

### Summary Table:

| Method | Flushes Data | Asynchronous | Closes Stream |
| --- | --- | --- | --- |
| `Flush()` | Yes | No | No |
| `FlushAsync()` | Yes | Yes | No |
| `Dispose()` | Yes | No | Yes |

### Best Practices:

1. Use `using` or `await using` blocks to ensure `Dispose()` is always called, even on exceptions.
2. Call `Flush()` or `FlushAsync()` if you want to write changes *without closing* the file.


---

Original Source: https://www.mindstick.com/interview/34113/what-are-the-differences-between-filestream-flush-flushasync-and-dispose

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
