csharp / beginner
Snippet
Safe Resource Disposal
The 'using' statement ensures that objects implementing IDisposable (like file streams) are properly closed and cleared from memory as soon as the block is finished.
snippet.cs
csharp
1
2
3
4
using (var writer = new System.IO.StreamWriter("log.txt")){writer.WriteLine("Process started.");}
Breakdown
1
using (var writer = new System.IO.StreamWriter("log.txt"))
Initializes a disposable resource within a scope that automatically calls Dispose().
2
writer.WriteLine("Process started.");
Writes text to the file using the stream writer.