With the C# 8.0 update, Microsoft has introduced the using declaration feature. This feature allows us to declare a disposable object with the keyword using so the object will be disposed of when it goes out of the scope.
In the classic approach, prior to the C# 8.0 update, we used a using statement to define the scope of the managed type objects that access unmanaged resources and the unmanaged resources that implement the IDisposable interface.
The following code example uses the classic approach in the creation of a text file.
static void CreateTextFileClassic() { using (var file = new System.IO.StreamWriter("Sample.txt")) { file.WriteLine("Hello world"); }// file object is disposed of here }
Let’s see the same example but with the using declaration.
static void CreateTextFileModern() { using var file = new System.IO.StreamWriter("Sample.txt"); file.WriteLine("Hello world"); // file is disposed of here }
At first glance, all of us would notice the missing curly braces. Before we jump to conclusions, let’s compile these code examples and refactor the resultant assembly. To our surprise, both sets of code resulted in the same piece of code.
private static void CreateTextFileClassic() { using (StreamWriter file = new StreamWriter("Sample.txt")) { file.WriteLine("Hello world"); } } private static void CreateTextFileModern() { using (StreamWriter file = new StreamWriter("Sample.txt")) { file.WriteLine("Hello world"); } }
Based on our understanding so far, the using declaration does not need us to define the scope of the object; it is instead handled by the compiler itself.
The scope of the object declared in the using statement is the scope in which it is declared.
Let’s consider the following scenario in which I am going to add a few more lines of code to see how the using declaration and using statements behave.
The following code is implemented with the using statement:
static int CreateTextFileClassic(List lines) { int addedLines = 0; using (var file = new System.IO.StreamWriter("Sample.txt")) { foreach (string line in lines) if (line.Contains("account number")) { file.WriteLine(line); addedLines++; } } return addedLines; }
The following code is implemented with the using declaration:
static int CreateTextFileModern(List lines) { int addedLines = 0; using var file = new System.IO.StreamWriter("Sample.txt"); foreach (string line in lines) if (line.Contains("account number")) { file.WriteLine(line); addedLines++; } return addedLines; }
Now let’s compile these code samples and refactor the resultant assembly to take a deeper look.
private static int CreateTextFileClassic(List lines) { int addedLines = 0; using (StreamWriter file = new StreamWriter("Sample.txt")) { foreach (string line in lines) { if (line.Contains("account number")) { file.WriteLine(line); addedLines++; } } } return addedLines; } private static int CreateTextFileModern(List lines) { int addedLines = 0; using (StreamWriter file = new StreamWriter("Sample.txt")) { foreach (string line in lines) { if (line.Contains("account number")) { file.WriteLine(line); addedLines++; } } return addedLines; } }
Though these method looks similar, they are not. The scope of the using block in CreateTextFileModern is a bit larger than in CreateTextFileClassic.
In the case of a using statement, we have control over defining the scope of the object. In the case of a using declaration, its scope is automatically defined from the object’s declaration statement to the end of the current code block.
If the methods are going to be small and you are using the using declaration in the final part of the method, the new approach is well and good.
If you’re interested in other new features in C# 8.0, we have already published a blog on the nullable reference type feature. Refer to the blog What’s New with C# 8.0 – Nullable Reference Type for more details.
Syncfusion provides more than 1,000 custom controls to ease the work of developers on various platforms. Please have a look and use them in your application development:
If you have any questions or require clarifications about our components, please let us know in the comments below. You can also contact us through our support forum, Direct-Trac, or feedback portal. We are happy to assist you!
Happy coding!