Working with new using declaration and read-only struct members in C# 8.0

Hello friends,
C# 8 officially got released on 23rd Sep 2019 along with .NET Core 3.0 and Visual Studio 2019 v16.3. It has several new features and enhancements to give more power to C# developers and make their day to day coding even easier.
I have started writing series of articles to go through new C# 8 features one by one to cover all the new features and enhancements. In case you have not gone through my previous articles on C# 8, you may go though as follows.
In this article, we will go through the new using declaration and read-only struct members.

New using declaration

Using keyword plays a big role in primary cleaning-up resources without explicit invocation. With C#, it has become even simpler to use and does the same job as earlier.
Let's have a look at the code below to understand the change. In the example below, both old and new using styles have been added. 
  1. public static void ExecuteUsingFunction()  
  2. {  
  3. List<string> messages = new List<string>(); messages.Add("Hello readers of Learning C# 8!!"); messages.Add("How are you!!"); ExecuteOldUsingFunction(messages); ExecuteNewUsingFunction(messages);  
  4. }  
  5. static void ExecuteOldUsingFunction(IEnumerable<string> lines)  
  6. {  
  7. using (var file = new StreamWriter("file.txt"))  
  8. {  
  9. foreach (string line in lines)  
  10. {  
  11. if (!line.Contains("C# 8"))  
  12. {  
  13. file.WriteLine(line);  
  14. }  
  15. }  
  16. // file is disposed here automatically  
  17. }  
  19. static void ExecuteNewUsingFunction(IEnumerable<string> lines)  
  20. {  
  21. using var file = new StreamWriter("file.txt"); foreach (string line in lines)  
  22. {  
  23. if (!line.Contains("C# 8"))  
  24. {  
  25. file.WriteLine(line);  
  26. }  
  27. }  
  28. // file is disposed here automatically  
  29. }  
As you can see in the method ExecuteNewUsingFunction that the new using is intuitive to work with.
In an old using statement, the file is disposed of when the closing brace associated with the using is reached however in a new pattern, the file is disposed of when the closing brace for the method is reached as that's the end of the scope for file declaration. In both types of using statement, the compiler generates the call to Dispose() to clean up resources hence they are considered equivalent.

Read-only struct members

You can now apply readonly to any struct members. This is an extension of adding to readonly as a struct level. It indicates that the member does not alter the data or modify the state. This feature helps the compiler in not creating a defensive copy of a variable. 
Let's take an example below to see it in action. 
  1. public struct Distance  
  2. {  
  3. public double X { getset; } public double Y { getset; }  
  4. public double PointDistance => Math.Sqrt(X * X + Y * Y);  
  5. public readonly override string ToString() =>  
  6. $"({X}, {Y}) is {PointDistance} from the origin point";  
  7. }  
The above code results in a warning message as following.
csharp read only struct member 
Compiler generates a warning because of ToString property as it accesses the property PointDistance is not created as readonly.
To fix the warning, just put the readonly on PointDistance property as follows. 
  1. public readonly double PointDistance => Math.Sqrt(X * X + Y * Y);  
By adding readonly to PointDistance property you may have realized that the readonly modifier is necessary even on get properties as the compiler doesn't assume get accessors do not modify state. This feature lets you specify your design intent clearly so that compiler can make necessary optimizations.


In this article, we have learned about new using declaration and read-only struct members and why are they required. We have gone through to understand that how new using declaration simplifies the syntax. read-only struct members is also a new addition in struct and helps designing intent more clearly. Clearly these new features are the great value additions for developers.

Similar Articles