Skip to content

Repository files navigation

Deferly

Build Status

A simple C# library providing a DeferContext for registering deferred actions that execute in Last-In-First-Out (LIFO) order when the context is disposed. Inspired by Go's defer, this library helps ensure cleanup code runs predictably at the end of a scope.


Table of Contents

  1. Getting Started
  2. Usage
  3. Running Tests
  4. Contributing
  5. License

Getting Started

This library targets .NET Standard and can be used in any .NET Core or .NET Framework project. It provides two main types:

  • DeferContext: A disposable context for registering actions that run when disposed.
  • Defer: A static helper with shortcuts to create contexts and register actions.

Use DeferContext in a using block (or a using declaration) to ensure all registered callbacks run at the end of the scope.


Usage

DeferContext

using Defer;

public void MyMethod()
{
    using (var context = new DeferContext())
    {
        // Register cleanup actions
        context.Defer(() => Console.WriteLine("Cleanup A"));
        context.Defer(() => Console.WriteLine("Cleanup B"));

        // Main logic here
    }
    // When the using block ends, deferred actions run in LIFO: B, then A.
}

Defer Helper

Alternatively, use the static Defer helper to create contexts and register actions:

using Defer;

public void AnotherMethod()
{
    // Create a new DeferContext
    var context = Defer.Create();
    try
    {
        Defer.OnExit(context, () => Console.WriteLine("Cleanup via helper"));
        // ... work ...
    }
    finally
    {
        // Ensure all deferred actions run
        context.Dispose();
    }
}

Running Tests

This project includes xUnit tests covering core behaviors of DeferContext and the Defer helper.

  1. Navigate to the tests/Deferly.Tests folder.
  2. Run dotnet test:
    cd tests/Deferly.Tests
    dotnet test

Contributing

Contributions are welcome! Please follow these guidelines:

  1. Fork the repository and create a new branch for your feature or fix.
  2. Ensure all existing tests pass, and add new tests for any new behavior.
  3. Adhere to existing code style conventions (use var where appropriate, PascalCase for public members, etc.).
  4. Open a pull request with a description of changes.

License

This project is licensed under the MIT License.

About

Deferly brings Go-style defer semantics to C#, letting you register cleanup or teardown actions that automatically run at the end of a method or scope. With a lightweight, fluent API and zero dependencies, it simplifies resource management and guarantees exception-safe cleanup.

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages