17th Mar 2022

Features of C# 10

.NET

Written By, Nancy Bhimani

Overview

Hello Techs,

Hope you are aware of  C#10 with lots of new features. With every new feature there is a simplicity of code, faster and more expressive which are a part of .NET6 and Visual Studio 2022. Here are some of the features that help to get key concepts and also improve workflow.

Let’s get some deep ideas about some features with their examples.

Introduction

Every new release brings a plenty of features included in versions of C#. Every new feature provides a purity of code for advancing and simplicity. Thereby C#10 support in .NET 6  as part of it.

Here are the add on in C#10 features which are provided by Microsoft.

  • Record Structs
  • Improvement of Structure Types
  • Interpolated string handlers
  • Global using Directives
  • File-Scoped namespace declaration
  • Extended property patterns
  • Lambda expression improvements
  • Const Interpolated Strings
  • Record Types can seal To String()
  • Assignment and Declaration in same Deconstruction
  • Improved Definite Assignment
  • Allow Async Method Builder attribute on methods
  • CallerArgumentExpression Attribute
  • Enhanced #line pragma

Record Structs

  • Record keyword is used for defining reference type as well as also allows defining record struct type as a value type with higher improvement.
  • Record Struct overrides  == and != operator which inherits System.ValueType and has  IEquatable<T> interface.
  • Major difference in C#10 is it pass regular record is reference from its functions and hereby record struct copy its values.
  • Record Struct can be declared as immutable and also uses positional parameters along with it.

Syntax:

public  record struct  Employee(string Name, int Id);    // positional record syntax

  • Can also use readonly modifier for making record struct immutable.

public  readonly record struct Employee(string Name, int Id);

  • Record data models are designed to work with immutable they are used for changing variables or objects of records but they are not assigned to immutable. Once the record is created that cannot be changed. Can use a constructor for initializing record types.

Example:

File-Scoped Namespaces

  • File-Scoped Namespace is a statement that applies automatically to the file. While adding a block to the namespace file it will create a nested namespace. This will help for simplifying the code expressions and complexity of nesting namespace.
  • New Syntax for file-scoped namespaces used without {} braces and includes simply “;” a semicolon.

Example: 

Global Using Directives

  • For not writing the namespace over and again here in C#10  allows importing  the namespaces in an entire project globally using global keyword.
  • While declaring namespace as globally means it can be accessed in all files of the project.

Example:

Null Parameters Checking

  • CallerArgumentExpression is a method that allows passing an expression for checking and validating properties at debugging the code.
  • While the condition or a command fails at a time it creates a custom method and adds  CallerArgument Expression to the method.
  • CallerArgumentExpression is used for avoid the error “object reference not set to an instance of an object
  • ThrowIfNull” extension and “!!” exclamation mark  that is used in ArgumentNullException for checking if null.

Example : (ThrowIfNull)

Example : (!!)

  • This will automatically check if it is null and will throw ArgumentNullException.

Conclusion

Hope this blog helps you for understanding and getting clear idea  about the Features of C#10 with examples and also helps with coding expressions.

Written By,

.NET Developer at Yudiz Solutions Pvt. Ltd