DEV Community

Cover image for C# Sugar & Improvements [6.0 to 10.0]
Patricio Filice for Cloud(x);

Posted on • Updated on

C# Sugar & Improvements [6.0 to 10.0]

Hi everyone, just like me you came across to different projects with different versions of C# so is a common issue to forget the new features, the amenities of the version we are using.
So to ammend that I made something more interactive than an image or a post, a project with examples, were you can run and see step by step each one of the features incorporated in each version, most of them are related with sugar sintax the others are new tools to simplify our work.
As an extra there is a url for each addon and C# version description.

You can click here to access the repo with the project

Features

For a more detailed explanation of what's new on each version:

C# 10.0

  • Assignment and declaration: There was a restriction where you couldn't mix the declaration and assignment of variables on the same line, that restriction was lifted.

  • File scoped namespace: Is a reduced way to declare a namespace saving space horizontally and vertically.

  • (PREVIEW) Generic attributes: As the name implies now is possible to create attributes based on a generic declaration, there are some limitations, and it can change on the final release but is a good tool to check on.

C# 9.0

  • Record: A new positional data type intended to be non mutable with its own traits closing a little more the gap with functional programming.

  • Targeted type: Reduces the redundant declarations when a type is implicit.

C# 8.0

  • Indexes and ranges for arrays: Its quicker and simpler to retrieve a specific element or a segment from an array.

  • Null coalescing: Reduces the checking and assignment when null is involved and wants to be avoided.

  • Pattern matching: Allows more advanced conditional statements, involving a syntax similar to functional pattern matching.

  • Using disposal: Eliminates the boilerplate declaration for disposables, for a block or a full method declaration.

C# 7.0

  • Bodied expression in a class: It's possible to use bodied expressions when declaring a constructor o destructor for a class.

  • Is operator for casting: The is operator was extended to allow outputting the casting result wich allows to reduce the needed code lines to check and use a casting.

  • Local function: We can declare our own methods inside the declaration of another method.

  • Out parameter with inline declaration: There is no further need to declare a variable before storing the value, we can declare and output to it on the same line.

C# 6.0

  • Name of: We can bind compile checking of a code definition (class, properties, variables, etc.) avoiding maintainability problems when plain text is related to an existing definition.

  • Null spreading: A way to avoid boilerplate null checking.

  • String interpolation: A less laborious way to dynamically format a string.

Top comments (0)