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#.
- 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.
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
- //Before 8.0
- using(var fileStream = new FileStream("abcd.txt",FileMode.Open))
- {
- //do somthing here
- }// fileStream dispose here
- //With 8.0
- var fileName = "abcd.txt";
- if(fileName != null)
- {
- using var fileStream = new FileStream(fileName, FileMode.Open);
- //do somthing here
- }// fileStream dispose here

RyderPosted Feb 7, 2020, 5:48 AM
Nice one...
Chittaranjan SwainPosted Dec 23, 2019, 10:48 PM
Nice one......
Mageshwaran RPosted Nov 16, 2019, 4:02 AM
Super Article..
Sourav Kumar DasPosted Oct 17, 2019, 3:49 AM
Nice Article.