C# - Top 10 New Features (Part 3 of 4)

Article Overview

  • Background
  • Pre-requisites
  • Using Declarations
    • Points
    • Example
  • Nullable Reference Types
    • Points
    • Example
  • Readonly Struct Members
    • Points
    • Example
  • Summary

Background

 
I have divided this whole article into 4 parts to make it easy to understand. This article mainly focuses on three key features and enhancements to C# 8.0,
  • Using Declarations
  • Nullable Reference Types
  • Readonly Struct Members
Here, I have kept all the implementation details with a complete example.
 
Prerequisites
  • You should have a basic knowledge of C#.
  • InPart 1 of 4; I had explained “How to compile/test with C# 8.0 in MSVisual Studio OR online”, “Key features and enhancements to the C# 8.0”,and the “Switch Expression” feature.
  • In Part 2 of 4; I had explained key three "Indices and Ranges", "Default Interface Members", and "Static Local Functions" feature.
  • So, it will be good if you read the first part, i.e., “C# - Top 10 New Features (Part 1 Of 4)” of this article.
  • So, it will be good if you read the second part, i.e. “C# - Top 10 New Features (Part 2 Of4)” of this article.
For your reference, I have kept all the examples in a single .cs file and uploaded with this article for your ease of use.
 

Using Declarations

 
Below are the key syntax improvements:
  • Using declaration is a variable declaration preceded by using keyword.
  • It tells the compiler that a variable being declared should be disposed at end of enclosing scope.
  • Compiler will generate the call to Dispose() when a variable falls out of scope.
  • This will be especially useful when multiple instances of types implementing the IDisposable interface are used in same code block.
  • It enhances the ‘using’ operator to use with patterns and it also makes it more natural.
Now, let us understand it by example.
 
Example
  1. //Before 8.0  
  2. using(var fileStream = new FileStream("abcd.txt",FileMode.Open))    
  3. {    
  4.     //do somthing here    
  5. }// fileStream dispose here    
  1. //With 8.0  
  2. var fileName = "abcd.txt";    
  3. if(fileName != null)    
  4. {    
  5.     using var fileStream = new FileStream(fileName, FileMode.Open);    
  6.    //do somthing here    
  7. }// fileStream dispose here  
This is very usful when you have multiple using requirements.
 

Nullable Reference Types

 
Below are the key syntax improvements,
  • Nullable reference types will not be checked to ensure that they are not assigned or initialized to null.
  • Compiler uses the flow analysis to ensure that any nullable reference type variable is checked against null before it is accessed/assigned to a non-nullable reference type.
  • Key purpose is to allow variable type definitions to specify whether they will have null value assigned to them or not.
  • It will emit the compiler warning/error if the variable which must not be null is assigned to null.
Now, let us understand by example.

Example
  1. string? nullableString = null;  
  2.  
  3. Console.WriteLine(nullableString.Length);  

  4. // WARNING: may be null! So, take care.  

Readonly Struct Members

 
Below are the key syntax improvements,
  • Readonly modifier can be applied to any member of a struct.
  • It indicates that a member does not modify the state.
  • Itwill prevent using a variable from containing method in a localfunction and at the same time avoid performance costs related to makingthem available.
Now, let us understand by example.
 
Example
  1. Point obj = new Point();
  2.  
  3. obj.X = 2;  
  4. obj.Y = 3; 

  5. Console.WriteLine(obj.Distance); 

  6. public struct Point  
  7. {  
  8.     public double X { getset; }  
  9.     public double Y { getset; }  

  10.     public readonly double Distance => Math.Sqrt(X * X + Y * Y);  
  11.  

Summary

 
Now, I believe you will be able to implement "Using Declarations", "Nullable Reference Types", and "Readonly Struct Members" in C# 8.0.
 
In my next article, I will cover next three features such as "Null-Coalescing Assignment", "Async Streams", and "Property, Tuple, and Positional Patterns".
 
Previous parts of this series,