npub1u6…v0yg2 on Nostr: Learn: IDisposable and using Some objects hold things the garbage collector doesn't ...
Learn: IDisposable and using
Some objects hold things the garbage collector doesn't manage well: files, network connections, database connections. They implement IDisposable, and you must call Dispose when done.
using does it for you, even if an exception is thrown:
```csharp
using (var r = new Resource()) Console.WriteLine("working");
Console.WriteLine("after");
class Resource : IDisposable { public void Dispose() => Console.WriteLine("cleaned up"); }
```
It prints working, cleaned up, after.
Shorter form: using var file = File.OpenRead(path); disposes at the end of the method or block.
Read more:
https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/statements/using#tip · TIP-101
Published at
2026-10-27 06:07:57 UTCEvent JSON
{
"id": "05e0424d6bb78ff480edc5474a78698e5d77af206a0fef520a4770e1907e9bab",
"pubkey": "e68a0856e90d9997b1f721e905249896412dd570399a987b2e8fd5e7fcfe2279",
"created_at": 1793081277,
"kind": 1,
"tags": [
[
"t",
"tip"
]
],
"content": "Learn: IDisposable and using\n\nSome objects hold things the garbage collector doesn't manage well: files, network connections, database connections. They implement IDisposable, and you must call Dispose when done.\nusing does it for you, even if an exception is thrown:\n\n```csharp\nusing (var r = new Resource()) Console.WriteLine(\"working\");\nConsole.WriteLine(\"after\");\n\nclass Resource : IDisposable { public void Dispose() =\u003e Console.WriteLine(\"cleaned up\"); }\n```\n\nIt prints working, cleaned up, after.\nShorter form: using var file = File.OpenRead(path); disposes at the end of the method or block.\n\nRead more: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/statements/using\n\n#tip · TIP-101",
"sig": "28f0b2d556e85dde3adcb8aaacfee1e5d9e1aad02549f7bdebcdb37fc9293d26aca058f229b0953eeb2835bc6241c642239d94c8234fd51e6238c0ac1cf8e818"
}