Giter Club home page Giter Club logo

what-is-new-csharp-7.2-8's Introduction

What is new/interesting in C# 7.2 and 8.0

Simple and small exploration of new features available in the latests C# releases.

Enabling C# Latest versions

C# 8 can be used with the .NET Framework and other targets older than .NET Core 3.0/.NET Standard 2.1 in Visual Studio 2019 (or older versions of Visual Studio if you install a Nuget package.

The language version must be set to 8.0 in the csproj file.

Nullable reference types

C# 8.0 introduces nullable reference types and non-nullable reference types that enable you to make important statements about the properties for reference type variables.

Samples in NullableReferenceType.cs
more

Where it is useful

The ability to declare more properties about reference types brings more typesafety and type soundness to your code. I consider this feature of high valuable to express input and output of functions, providing a more accurate information to API client.

Using declaration

A using declaration is a variable declaration preceded by the using keyword. It tells the compiler that the variable being declared should be disposed at the end of the enclosing scope. Using declaration is a declarative way to handle disposable objects instead of the classic imperative of the using statement.

Samples in UsingDeclaration.cs
more

Switch expressions

Often, a switch statement produces a value in each of its case blocks. Switch expressions enable you to use more concise expression syntax.

Samples in SwitchExpressions.cs
more

Indices and Ranges

Ranges and indices provide a succinct syntax for accessing single elements or ranges in a sequence. This feature should be a good replacement if you are already working with indices, therefore handling Out of range exceptions. In the examples there are scenarios like paging which can be solved by using indices and ranges but I would continue with the combination of skip + take in order to avoid the Out of range validations.

Samples in IndicesAndRanges.cs
more

Null-Coalescing assignement

C# 8.0 introduces the null-coalescing assignment operator ??=. You can use the ??= operator to assign the value of its right-hand operand to its left-hand operand only if the left-hand operand evaluates to null.

Samples in NullCoalescingOperator.cs
more

Default implementation in interfaces

You can now add members to interfaces and provide an implementation for those members. This language feature enables API authors to add methods to an interface in later versions without breaking source or binary compatibility with existing implementations of that interface. It is not that bad as the interface method can only be seen when using the interface type, therefore transparent to the type which implements the interface itself. The ability which strikes me to the most is the definition of static methods in interfaces.

Samples in DefaultInterfaceMethods.cs
more

what-is-new-csharp-7.2-8's People

Contributors

higorcesar avatar

Watchers

 avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.