C# Sugar & Improvements [6.0 to 10.0] 
Tags: csharp, cheatsheet, programming, sugarsyntax
Published: 2022-01-25 18:58:03

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](https://github.com/Patricio-Filice-CloudX/CSharp-Sugar-And-Improvements) to access the repo with the project**


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

C# 10.0

- <u>[Assignment and declaration:](https://github.com/Patricio-Filice-CloudX/CSharp-Sugar-And-Improvements/blob/master/CSharp10/AssignmentAndDeclarationCSharp10.cs)</u> There was a restriction where you couldn't mix the declaration and assignment of variables on the same line, that restriction was lifted.

- <u>[File scoped namespace:](https://github.com/Patricio-Filice-CloudX/CSharp-Sugar-And-Improvements/blob/master/CSharp10/FileScopedNamespaceCSharp10.cs)</u> Is a reduced way to declare a namespace saving space horizontally and vertically.

- **(PREVIEW)** <u>[Generic attributes:](https://github.com/Patricio-Filice-CloudX/CSharp-Sugar-And-Improvements/blob/master/CSharp10/Preview/GenericAttributesCSharp10.cs)</u> 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
- <u>[Record:](https://github.com/Patricio-Filice-CloudX/CSharp-Sugar-And-Improvements/blob/master/CSharp9/RecordCSharp9.cs)</u> A new positional data type intended to be non mutable with its own traits closing a little more the gap with functional programming.

- <u>[Targeted type:](https://github.com/Patricio-Filice-CloudX/CSharp-Sugar-And-Improvements/blob/master/CSharp9/TargedTypedCSharp9.cs)</u> Reduces the redundant declarations when a type is implicit.

C# 8.0
- <u>[Indexes and ranges for arrays:](https://github.com/Patricio-Filice-CloudX/CSharp-Sugar-And-Improvements/blob/master/CSharp8/ArrayCSharp8.cs)</u> Its quicker and simpler to retrieve a specific element or a segment from an array.

- <u>[Null coalescing:](https://github.com/Patricio-Filice-CloudX/CSharp-Sugar-And-Improvements/blob/master/CSharp8/NullCoalescingCSharp8.cs)</u> Reduces the checking and assignment when null is involved and wants to be avoided.

- <u>[Pattern matching:](https://github.com/Patricio-Filice-CloudX/CSharp-Sugar-And-Improvements/blob/master/CSharp8/PatternMatchingCSharp8.cs)</u> Allows more advanced conditional statements, involving a syntax similar to functional pattern matching.

- <u>[Using disposal:](https://github.com/Patricio-Filice-CloudX/CSharp-Sugar-And-Improvements/blob/master/CSharp8/UsingCSharp8.cs)</u> Eliminates the boilerplate declaration for disposables, for a block or a full method declaration.

C# 7.0
- <u>[Bodied expression in a class:](https://github.com/Patricio-Filice-CloudX/CSharp-Sugar-And-Improvements/blob/master/CSharp7/BodiedExpressionCSharp7.cs)</u> It's possible to use bodied expressions when declaring a constructor o destructor for a class.

- <u>[Is operator for casting:](https://github.com/Patricio-Filice-CloudX/CSharp-Sugar-And-Improvements/blob/master/CSharp7/IsOperatorCSharp7.cs)</u> 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.

- <u>[Local function:](https://github.com/Patricio-Filice-CloudX/CSharp-Sugar-And-Improvements/blob/master/CSharp7/LocalFunctionCSharp7.cs)</u> We can declare our own methods inside the declaration of another method.

- <u>[Out parameter with inline declaration:](https://github.com/Patricio-Filice-CloudX/CSharp-Sugar-And-Improvements/blob/master/CSharp7/OutVariableCSharp7.cs)</u> 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
- <u>[Name of:](https://github.com/Patricio-Filice-CloudX/CSharp-Sugar-And-Improvements/blob/master/CSharp6/NameOfCSharp6.cs)</u> 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.

- <u>[Null spreading:](https://github.com/Patricio-Filice-CloudX/CSharp-Sugar-And-Improvements/blob/master/CSharp6/NullSpredCSharp6.cs)</u> A way to avoid boilerplate null checking.

- <u>[String interpolation:](https://github.com/Patricio-Filice-CloudX/CSharp-Sugar-And-Improvements/blob/master/CSharp6/StringInterpolationCSharp6.cs)</u> A less laborious way to dynamically format a string.