CHAPTER 1
C# is continuing to evolve and improve with each major release of the language. As the C# team is innovating and adding features to C#, its members share their thought process around the design of C# with the community.
If you head over to the dotnet/Roslyn repository on GitHub, you will see detailed language feature statuses as well as features that the C# team is considering for upcoming releases. You can also view a history C# on the Microsoft Docs. As of this writing, of particular interest are the planned features surrounding C# 9. Later in this book, we will have a look at what is planned for C# 9.
The C# build tools will default the language version to the latest major release. As of C# 7.0, however, developers started seeing more point releases in the form of versions C# 7.1, C# 7.2, and C# 7.3.
When creating a new project in Visual Studio, the C# compiler figures out which version of C# to use based on the target framework of your project. This ensures you do not use a language version that requires types or runtime behavior not available in the target framework of the project.
It is worth mentioning that C# 8.0 and higher will only be supported on .NET Core 3.x and later. Table 1 outlines the C# language defaults based on the target framework.
Table 1: Language version default mapping
Target framework | Version | C# language default |
|---|---|---|
.NET Core | 3.x | C# 8.0 |
.NET Core | 2.x | C# 7.3 |
.NET Standard | 2.1 | C# 8.0 |
.NET Standard | 2.0 | C# 7.3 |
.NET Standard | 1.x | C# 7.3 |
.NET Framework | All | C# 7.3 |
As you will see later in this book, you can override the default language version. You can do this by:
For more on the -langversion compiler option, refer to this .NET documentation.
The release of Visual Studio .NET 2002 brought with it C# 1.0, a Java-looking, general-purpose, object-oriented language. It didn’t contain LINQ or generics, but what it did offer developers was a viable alternative to Java, on a Windows platform.
C# 1.0 included the following:
Having a look at these features today, one would be excused for feeling a little spoiled with the features available in C# 8.0.
Visual Studio .NET 2003 shipped with C# 1.2 and only contained a few small feature enhancements. It is here that code generated in foreach loops would call Dispose on an IEnumerator if it implemented IDisposable.
When Visual Studio 2005 was released in 2005, we saw some nice features, such as:
Existing features were also improved, such as:
In many respects, C# 2.0 was a turning point in the language, especially with the introduction of generics and iterators.
In the latter part of 2007, Visual Studio 2008 was released. The language features it provided came with the release of the .NET Framework 3.5. These were as follows:
LINQ was one of the killer features of C#. This allowed you to write SQL-style, declarative queries on collections, for example. Initially, I struggled with LINQ (and I’m not the only one). These days, it is just quicker and more concise to write. C# 3.0 was truly a groundbreaking release.
Visual Studio 2010 brought C# 4.0. The features added in version 4.0 were:
For me, the introduction of optional parameters was a breath of fresh air, but it was the addition of the dynamic keyword in version 4.0 that stole the show.
C# version 5.0 was released with Visual Studio 2012 and made another groundbreaking addition to the C# language—the introduction of async and await for asynchronous programming. C# 5.0 introduced:
Asynchrony was now baked into C# as a first-class citizen.
Visual Studio 2015 brought C# version 6.0, and it is here that we started seeing smaller features that made developers more productive. These included:
I remember Mads Torgersen saying that C# 6.0 added a lot of syntactic sugar to the language. I agree with him 100 percent.
Visual Studio 2017 saw C# version 7.0 released to the developer community. It introduced the following language features:
Developers could now write cleaner code and be more productive.
With C# 7.0, we started seeing point releases on C#, starting with version 7.1. This marked an increased release cadence for C#. New language features for this release were:
C# 7.1 also added the language version selection configuration element, as well as new compiler behavior.
C# 7.2 added a few more smaller language features to C#. These were:
You can read more on writing safe and efficient code in C# here.
The point releases of C# 7 allowed developers to get their hands onto new language features sooner rather than later. It was the release of C# 7.3 that had two main themes. One theme allowed safe code to be as performant as unsafe code, and the other provided additional improvements to existing features.
From a better performant safe code perspective, we saw:
From an enhancement perspective, we saw:
We also saw new compiler options: -publicsign to enable open-source software signing of assemblies, and -pathmap to provide a mapping for source directories.
C# 8.0 specifically targets .NET Core. This is what this book is all about, and why I would imagine you are reading it.
The following features and enhancements were added to C# 8.0:
There is a lot to consume here. If you have little experience with C#, the best place to start is C# 7. This book will take a look at C# 7 before delving into the new features of C# 8.0.
If you are already comfortable with C# 7, then dive straight into the chapter on C# 8.0. Are you ready? Let’s go.