csharp / beginner
Snippet
Resource Management Scope
The 'using' statement ensures that objects implementing IDisposable are cleaned up from memory immediately after use.
snippet.cs
csharp
1
2
3
4
5
6
7
8
9
10
using System.IO;public class Program {public static void Main() {using (StringReader reader = new StringReader("Hello World")) {string content = reader.ReadToEnd();System.Console.WriteLine(content);}}}
Breakdown
1
using (StringReader reader = ...)
Defines a scope where the reader object will be automatically disposed at the end.
2
reader.ReadToEnd()
Reads all characters from the current position to the end of the string.