The USING Declarations In C# 8.0

Introduction

 
In today’s article, we will look at a new feature introduced with C# 8.0. This is not entirely a new feature, but an enhancement to an existing feature. This is the improvement in the use of the using declaration. We use the using declaration to automatically dispose of an object once it is out of the scope of the using statement. We will see the enhancement of this feature introduced with C# 8.0 in this article. C# 8.0 is supported on .NET Core 3.x and .NET Standard 2.1.

Creating a Simple Console Application

 
We start by creating a simple console application using Visual Studio 2019 community edition. The framework used is .NET Core 3.1.
 
In this application, we read a list of string elements and then write them into a text file on the local drive. However, we want to skip any text which contains the words “SkipMe”.
 
We would normally write this application as shown below:
  1. using System;  
  2. using System.Collections.Generic;  
  3. namespace CSharp8Features {  
  4.     classProgram {  
  5.         staticvoid Main(string[] args) {  
  6.             var texts = new List < string > {  
  7.                 "Line1",  
  8.                 "SkipMe",  
  9.                 "Line2",  
  10.                 "Line3",  
  11.                 "SkipMe"  
  12.             };  
  13.             Console.WriteLine($ "Total lines missed in Old Format are {WriteToFileOld(texts)}");  
  14.             Console.ReadKey();  
  15.         }  
  16.         //Old method of using the USING declartion  
  17.         staticint WriteToFileOld(List < string > texts) {  
  18.             //We declare this outside the using block  
  19.             var missedLines = 0;  
  20.             using(var file = new System.IO.StreamWriter(@ "C:\Temp\OldFormatFile.txt")) {  
  21.                 foreach(string text in texts) {  
  22.                     if (!text.Contains("SkipMe")) file.WriteLine(text);  
  23.                     else missedLines++;  
  24.                 }  
  25.             }  
  26.             return missedLines;  
  27.         }  
  28.     }  
  29. }  
Here, you can see that we are writing by using the Stream Writer which is disposed of when the using statement goes out of scope. However, any variable we need to use outside this block must also be declared outside the block as we see in the case of the “missedLines” variable.
 
This has changed and now in C# 8.0 as can simply define the using statement without an enclosing block and the Stream Writer will be disposed when the enclosing block of the file variable (defined with using), which in the below case is the entire function goes out of scope. See the two functions below:
  1. using System;  
  2. using System.Collections.Generic;  
  3. namespace CSharp8Features {  
  4.     classProgram {  
  5.         staticvoid Main(string[] args) {  
  6.             var texts = new List < string > {  
  7.                 "Line1",  
  8.                 "SkipMe",  
  9.                 "Line2",  
  10.                 "Line3",  
  11.                 "SkipMe"  
  12.             };  
  13.             Console.WriteLine($ "Total lines missed in Old Format are {WriteToFileOld(texts)}");  
  14.             Console.WriteLine($ "Total lines missed in New Format are {WriteToFileNew(texts)}");  
  15.             Console.ReadKey();  
  16.         }  
  17.         //Old method of using the USING declartion  
  18.         staticint WriteToFileOld(List < string > texts) {  
  19.             //We declare this outside the using block  
  20.             var missedLines = 0;  
  21.             using(var file = new System.IO.StreamWriter(@ "C:\Temp\OldFormatFile.txt")) {  
  22.                 foreach(string text in texts) {  
  23.                     if (!text.Contains("SkipMe")) file.WriteLine(text);  
  24.                     else missedLines++;  
  25.                 }  
  26.             }  
  27.             return missedLines;  
  28.         }  
  29.         //New method of using the USING declartion  
  30.         staticint WriteToFileNew(List < string > texts) {  
  31.             usingvar file = new System.IO.StreamWriter(@ "C:\Temp\NewFormatFile.txt");  
  32.             //We can declare this after the using statement  
  33.             var missedLines = 0;  
  34.             foreach(string text in texts) {  
  35.                 if (!text.Contains("SkipMe")) file.WriteLine(text);  
  36.                 else missedLines++;  
  37.             }  
  38.             return missedLines;  
  39.         }  
  40.     }  
  41. }  
When run, both produce the same results, as shown below:
 
The USING declarations in C# 8.0
 
However, please remember that the compiler will generate an error if the expression in the using statement is not disposable in both cases.
 

Summary

 
In this article, we looked at how to use the improved version of the using declaration in C#. This is another one of the new features that has been introduced in C# 8.0. I will be covering more of these features in future articles. Happy Coding!


Similar Articles