Skip to content

Latest commit

 

History

History
30 lines (24 loc) · 835 Bytes

Defer.md

File metadata and controls

30 lines (24 loc) · 835 Bytes
Error in user YAML: Alias parsing is not enabled.
---
uid: Recore.Defer
example:
- *content
---

@Recore.Defer is for ad-hoc RAII.

Say you want to perform some action before you exit a method, regardless of whether you return normally or throw an exception. This is usually something like releasing a resource that was acquired in the method.

The classic way to do this in C# is with try-finally:

try
{
    Console.WriteLine("Doing stuff");
}
finally
{
    Console.WriteLine("Running cleanup");
}

This isn't bad, but it adds an extra level of indentation and 6 extra lines for the try-finally. With @Recore.Defer and C# 8's new using declarations, we can do it more simply:

using var cleanup = new Defer(() => Console.WriteLine("Running cleanup"));
Console.WriteLine("Doing stuff");