csharp / beginner
Snippet
Resource Disposal Patterns
The 'using' statement ensures that important resources, like file handles, are closed and cleaned up immediately after use.
snippet.cs
csharp
1
2
3
4
5
6
7
8
9
10
using System;using System.IO;public class ResourceDemo {public void WriteToFile() {using (StreamWriter writer = new StreamWriter("test.txt")) {writer.WriteLine("Hello Memory Management!");}}}
Breakdown
1
using (StreamWriter writer = ...)
Defines a scope where the writer object will be automatically disposed at the end.