Article Overview
- Background
- Pre-requisites
- Using Declarations
- Nullable Reference Types
- Readonly Struct Members
- 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#.
- In
Part 1 of 4; I had explained “How to compile/test with C# 8.0 in MS
Visual 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 Of
4)” 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
-
- using(var fileStream = new FileStream("abcd.txt",FileMode.Open))
- {
-
- }
-
- var fileName = "abcd.txt";
- if(fileName != null)
- {
- using var fileStream = new FileStream(fileName, FileMode.Open);
-
- }
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
- string? nullableString = null;
-
- Console.WriteLine(nullableString.Length);
-
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.
- It
will prevent using a variable from containing method in a local
function and at the same time avoid performance costs related to making
them available.
Now, let us understand by example.
Example
- Point obj = new Point();
-
- obj.X = 2;
- obj.Y = 3;
- Console.WriteLine(obj.Distance);
- public struct Point
- {
- public double X { get; set; }
- public double Y { get; set; }
- public readonly double Distance => Math.Sqrt(X * X + Y * Y);
- }
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,