{"id":20848357,"url":"https://github.com/emako/defer","last_synced_at":"2026-02-12T18:32:51.120Z","repository":{"id":263165540,"uuid":"888956312","full_name":"emako/Defer","owner":"emako","description":"Provides IDeferable to emulate Golang's defer keyword in C#.","archived":false,"fork":false,"pushed_at":"2024-11-18T07:18:01.000Z","size":31,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-10-29T03:49:04.894Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/emako.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2024-11-15T10:27:28.000Z","updated_at":"2024-11-18T07:15:41.000Z","dependencies_parsed_at":"2024-11-16T17:26:53.831Z","dependency_job_id":"13680aa2-c783-4637-ab5b-04c8ba94b18b","html_url":"https://github.com/emako/Defer","commit_stats":null,"previous_names":["emako/defer"],"tags_count":3,"template":false,"template_full_name":null,"purl":"pkg:github/emako/Defer","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/emako%2FDefer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/emako%2FDefer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/emako%2FDefer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/emako%2FDefer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/emako","download_url":"https://codeload.github.com/emako/Defer/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/emako%2FDefer/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29376901,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-12T18:17:34.915Z","status":"ssl_error","status_checked_at":"2026-02-12T18:17:34.495Z","response_time":55,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-18T02:25:41.193Z","updated_at":"2026-02-12T18:32:51.116Z","avatar_url":"https://github.com/emako.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Defer\n\nThis repository provides `IDeferable` to emulate Golang's `defer` keyword in C#.\n\n## Installing\n\n[![NuGet](https://img.shields.io/nuget/v/Defer.svg)](https://nuget.org/packages/Defer)\n\nThe package is available [on NuGet](https://www.nuget.org/packages/Defer). To install, run:\n\n```bash\ndotnet add package Defer\n```\n\n## Build Status\n\n[![Actions](https://github.com/emako/Defer/actions/workflows/library.nuget.yml/badge.svg)](https://github.com/emako/Defer/actions/workflows/library.nuget.yml)\n\n## Usage\n\n1. Usage of `Deferable`\n\n```c#\n// Deferable.Defer\nConsole.WriteLine(\"Hello, World!\");\nusing (Deferable.Defer(() =\u003e Console.WriteLine(\"Goodbye, World!\")))\n{\n    Console.WriteLine(\"Do something\");\n}\n\n// output:\n// Hello, World!\n// Do something\n// Goodbye, World!\n```\n\n2. Usage of `Deferable\u003cT\u003e`\n\n```c#\n// Deferable\u003cT\u003e\nint status = -1;\n\nConsole.WriteLine(\"init status: \" + status);\nusing (Deferable\u003cint\u003e.Defer(value =\u003e status = value, initValue: 1, deferValue: 0))\n{\n    Console.WriteLine(\"doing something status: \" + status);\n}\nConsole.WriteLine(\"after defer status: \" + status);\n\n// output:\n// init status: -1\n// doing something status: 1\n// after defer status: 0\n```\n\n3. Usage of `BooleanDeferable`\n\n```c#\n// BooleanDeferable\nbool flag = default;\n\nConsole.WriteLine(\"init flag: \" + flag);\nusing (BooleanDeferable.Defer(value =\u003e flag = value))\n{\n    Console.WriteLine(\"doing something flag: \" + flag);\n}\nConsole.WriteLine(\"after defer flag: \" + flag);\n\n// output:\n// init flag: False\n// doing something flag: True\n// after defer flag: False\n```\n\n4. Usage of `RefDeferable\u003cT\u003e`\n\n```c#\n// RefDeferable\u003cT\u003e\n// Only applicable for .NET Standard 2.1, .NET Core 3.0, or later versions\ndouble value = -1d;\n\nConsole.WriteLine(\"init value: \" + value);\nusing (RefDeferable\u003cdouble\u003e.Defer(ref value, 0d, 1d))\n{\n    Console.WriteLine(\"doing something value: \" + value);\n}\nConsole.WriteLine(\"after defer value: \" + value);\n\n// output:\n// init value: -1\n// doing something value: 0\n// after defer value: 1\n```\n\n## Comparison\n\n\u003e Comparison with Go's `defer`\n\nIn Go, `defer` is a keyword used to ensure that a function call is performed later in a program's execution, usually for purposes of cleanup.\n\n```go\nfmt.Println(\"Hello, World!\")\ndefer fmt.Println(\"Goodbye, World!\")\nfmt.Println(\"Do something\")\n```\n\nIn C#, using this library, you can achieve similar behavior with `Deferable.Defer` and the `using` statement.\n\n```csharp\nConsole.WriteLine(\"Hello, World!\");\nusing IDeferable defer = Deferable.Defer(() =\u003e Console.WriteLine(\"Goodbye, World!\"));\nConsole.WriteLine(\"Do something\");\n```\n\nBoth snippets output:\n\n```text\nHello, World!\nDo something\nGoodbye, World!\n```\n\n## Before / After\n\nPreviously you might show and hide a loading indicator with a try/finally block:\n\n```csharp\n// Before: try/finally\nbool _isLoading = false;\ntry\n{\n    _isLoading = true;\n    // Do work that shows loading indicator\n}\nfinally\n{\n    _isLoading = false;\n}\n```\n\nWith this library you can achieve the same with two lines using `Deferable`: the show and hide logic stay next to each other, the code is simpler and more readable.\n\n```csharp\n// After: Deferable\nbool _isLoading = false;\n_isLoading = true;\nusing IDeferable _ = Deferable.Defer(() =\u003e _isLoading = false);\n// Do work while loading; when the scope exits, loading is hidden automatically\n```\n\nThis keeps the show/hide logic colocated, reduces boilerplate, and improves readability.\n\n## References\n\nhttps://blog.coldwind.top/posts/mimic-go-defer-in-csharp/\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Femako%2Fdefer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Femako%2Fdefer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Femako%2Fdefer/lists"}