New using Declarations

What is Using declaration? 

 
In C# 8.0, there are two new capabilities added around the 'Using' statement in order to make resource management simpler -
  • 'Using' should recognize a disposable pattern in addition to 'IDisposable' 
  • Add a 'using' declaration to the language.
This new feature allows the developers to use 'using' in the local variable declaration. It has the same effect as that of the previous 'using' statements, like in the following example.
 
Old 'using' statement
  1. using(var fileStream = new FileStream("abcd.txt",FileMode.Open))  
  2. {  
  3.     //do somthing here  
  4. }// fileStream dispose here  

New 'using' statement

  1. var fileName = "abcd.txt";  
  2. if(fileName != null)  
  3. {  
  4.     using var fileStream = new FileStream(fileName, FileMode.Open);  
  5.   
  6.     //do somthing here  
  7. }// fileStream dispose here  
The lifetime of the 'using' local variable is the end of the scope where it is declared in the above example. Its scope is limited to the only if block. After the if block, it disposes of using the local variable. It disposes the 'using' local variables in reverse order of its declaration. Let us see the below example.
  1. var fileName = "abcd.txt";  
  2. if(fileName != null)  
  3. {  
  4.     using var fileStream1 = new FileStream(fileName, FileMode.Open);  
  5.     using var fileStream2 = new FileStream(fileName, FileMode.Open);  
  6.     using var fileStream3 = new FileStream(fileName, FileMode.Open);  
  7.   
  8.     //do somthing here  
  9.   
  10.     //dispose fileStream3  
  11.     //dispose fileStream2  
  12.     //dispose fileStream1  
  13. }// fileStream dispose here  

What is Static local function?

 
In this new feature, you can now add the 'static' modifier to the local function. It can not access any variable from the enclosing scope. In C# 7.0, we can access the variable from enclosing scope as follows.
  1. static void Main(string[] args)  
  2. {  
  3.     var firstName = "Jeetendra";  
  4.     var lastName = "Gund";  
  5.   
  6.     Console.WriteLine("My name is " + GetFullName());  //output My name is Jeetendra Gund
  7.   
  8.     string GetFullName() => firstName + ' ' + lastName;  
  9. }  

Now, in C# 8.0, we can add 'static' modifier to local function then the above example firstName and lastName won't be accessible there in GetFullName local function as follows:

  1. static void Main(string[] args)  
  2. {  
  3.     var firstName = "Jeetendra";  
  4.     var lastName = "Gund";  
  5.   
  6.     Console.WriteLine("My name is " + GetFullName());  
  7.   
  8.     static string GetFullName() => firstName + ' ' + lastName;  
  9. }  

After compiling this we will get the below errors,

C# 8.0 New Feature - Using Declarations And Static Local Functions
 
Let's resolve the above errors as follows,
  1. static void Main(string[] args)  
  2. {  
  3.     var firstName = "Jeetendra";  
  4.     var lastName = "Gund";  
  5.   
  6.     Console.WriteLine("My name is " + GetFullName(firstName, lastName));  //output My name is Jeetendra Gund
  7.   
  8.     static string GetFullName(string firstName, string lastName) => firstName + ' ' + lastName;  
  9. }  

You can also download all these examples from here.

Summary

 
In this article, we discussed what Using declarations and Static local functions in C# 8.0 are and how to use this with examples. If you have any suggestions or queries regarding this article, please contact me.

Stay tuned for other concepts of C# 8.0.

“Learn It, Share it.”

Author
Jeetendra Gund
42 32.2k 2.9m
Next » Null-Coalescing Assignment Operator