IDisposable and Using in C#

From the C# Programming Language cheat sheet · Memory Management · verified Jul 2026

IDisposable and Using

Resource management and deterministic cleanup

csharp
// Using statement
using var file = File.OpenRead("data.txt");
// Automatically disposed at end of scope

// IDisposable pattern
public class Resource : IDisposable
{
    public void Dispose()
    {
        // Cleanup
        GC.SuppressFinalize(this);
    }
}

// Span<T> for efficient memory
Span<int> numbers = stackalloc int[100];

// Memory<T> for async scenarios
Memory<byte> buffer = new byte[1024];
💡 IDisposable pattern ensures deterministic resource cleanup
⚡ Span<T> enables zero-allocation array operations
📌 ArrayPool reduces garbage collection pressure
🟢 Using declarations simplify resource management
Back to the full C# Programming Language cheat sheet