Book Image

C# 12 and .NET 8 – Modern Cross-Platform Development Fundamentals - Eighth Edition

By : Mark J. Price
4.6 (14)
Book Image

C# 12 and .NET 8 – Modern Cross-Platform Development Fundamentals - Eighth Edition

4.6 (14)
By: Mark J. Price

Overview of this book

This latest edition of the bestselling Packt series will give you a solid foundation to start building projects using modern C# and .NET with confidence. You'll learn about object-oriented programming; writing, testing, and debugging functions; and implementing interfaces. You'll take on .NET APIs for managing and querying data, working with the fi lesystem, and serialization. As you progress, you'll explore examples of cross-platform projects you can build and deploy, such as websites and services using ASP.NET Core. This latest edition integrates .NET 8 enhancements into its examples: type aliasing and primary constructors for concise and expressive code. You'll handle errors robustly through the new built-in guard clauses and explore a simplified implementation of caching in ASP.NET Core 8. If that's not enough, you'll also see how native ahead-of-time (AOT) compiler publish lets web services reduce memory use and run faster. You'll work with the seamless new HTTP editor in Visual Studio 2022 to enhance the testing and debugging process. You'll even get introduced to Blazor Full Stack with its new unified hosting model for unparalleled web development flexibility.
Table of Contents (18 chapters)
17
Index

Discovering your C# compiler version

The .NET language compiler for C# and Visual Basic, also known as Roslyn, along with a separate compiler for F#, is distributed as part of the .NET SDK. To use a specific version of C#, you must have at least that version of the .NET SDK installed, as shown in Table 2.3:

.NET SDK

Roslyn compiler

Default C# language

1.0.4

2.0-2.2

7.0

1.1.4

2.3-2.4

7.1

2.1.2

2.6-2.7

7.2

2.1.200

2.8-2.10

7.3

3.0

3.0-3.4

8.0

5.0

3.8

9.0

6.0

4.0

10.0

7.0

4.4

11.0

8.0

4.8

12.0

Table 2.3: .NET SDK versions and their C# compiler versions

When you create class libraries, you can choose to target .NET Standard as well as versions of modern .NET. They have default C# language versions, as shown in Table 2.4:

.NET Standard

C#

2.0

7.3

2.1

8.0

Table 2.4: .NET Standard versions and their default C# compiler versions

Although you must have a minimum version of the .NET SDK installed to have access to a specific compiler version, the projects that you create can target older versions of .NET and still use a modern compiler version. For example, if you have the .NET 7 SDK or later installed, then you can use C# 11 language features in a console app that targets .NET Core 3.0.

How to output the SDK version

Let’s see what .NET SDK and C# language compiler versions you have available:

  1. On Windows, start Windows Terminal or Command Prompt. On macOS, start Terminal.
  2. To determine which version of the .NET SDK you have available, enter the following command:
    dotnet --version
    
  3. Note that the version at the time of publishing is 8.0.100, indicating that it is the initial version of the SDK without any bug fixes or new features yet, as shown in the following output:
    8.0.100
    

Enabling a specific language version compiler

Developer tools like Visual Studio and the dotnet command-line interface assume that you want to use the latest major version of a C# language compiler by default. Before C# 8 was released, C# 7 was the latest major version and was used by default.

To use the improvements in a C# point release like 7.1, 7.2, or 7.3, you had to add a <LangVersion> configuration element to the project file, as shown in the following markup:

<LangVersion>7.3</LangVersion>

After the release of C# 12 with .NET 8, if Microsoft releases a C# 12.1 compiler and you want to use its new language features, then you will have to add a configuration element to your project file, as shown in the following markup:

<LangVersion>12.1</LangVersion>

Potential values for the <LangVersion> are shown in Table 2.5:

<LangVersion>

Description

7, 7.1, 7.2, 7.3, 8, 9, 10, 11, 12

Entering a specific version number will use that compiler if it has been installed.

latestmajor

Uses the highest major number, for example, 7.0 in August 2019, 8 in October 2019, 9 in November 2020, 10 in November 2021, 11 in November 2022, and 12 in November 2023.

latest

Uses the highest major and highest minor number, for example, 7.2 in 2017, 7.3 in 2018, 8 in 2019, and perhaps 12.1 in H1 2024.

preview

Uses the highest available preview version, for example, 12.0 in July 2023 with .NET 8 Preview 6 installed.

Table 2.5: LangVersion settings for a project file

Using future C# compiler versions

In February 2024, Microsoft is likely to release the first preview of .NET 9 with a C# 13 compiler. You will be able to install its SDK from the following link:

https://dotnet.microsoft.com/en-us/download/dotnet/9.0

The link will give a 404 Missing resource error until February 2024, so do not bother using it until then!

After you’ve installed a .NET 9 SDK preview, you will be able to use it to create new projects and explore the new language features in C# 13. After creating a new project, you can edit the .csproj file and add the <LangVersion> element set to preview to use the preview C# 13 compiler, as shown highlighted in the following markup:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net9.0</TargetFramework>
    <LangVersion>preview</LangVersion>
  </PropertyGroup>
</Project>

Switching the C# compiler for .NET 8 to a future version

.NET 8 is an LTS release, so Microsoft must support developers who continue to use .NET 8 for three years. But that does not mean that you are stuck with the C# 12 compiler for three years!

In November 2024, Microsoft is likely to release .NET 9, including a C# 13 compiler with new features. Although future versions of .NET 8 are likely to include preview versions of the C# 13 compiler, to be properly supported by Microsoft, you should only set <LangVersion> to preview for exploration, not production projects, because it is not supported by Microsoft, and it is more likely to have bugs. Microsoft makes previews available because they want to hear feedback. You can be a part of C#’s development and improvement.

Once the .NET 9 SDK is made generally available in November 2024, you will be able to get the best of both worlds. You can use the .NET 9 SDK and its C# 13 compiler while your projects continue to target .NET 8. To do so, set the target framework to net8.0 and add a <LangVersion> element set to 13, as shown highlighted in the following markup:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net8.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
    <LangVersion>13</LangVersion>
  </PropertyGroup>
</Project>

The preceding project targets net8.0, so it is supported until November 2026 when run on a monthly patched version of the .NET 8 runtime. If the preceding project is built using .NET 9 SDK, then it can have the <LangVersion> set to 13, meaning C# 13.

If you target net9.0, which new projects will by default if you have installed the .NET 9 SDK, then the default language will be C# 13 so it would not need to be explicitly set.

In February 2025, Microsoft is likely to release the first preview of .NET 10, and, in November 2025, it will likely release .NET 10 for general availability in production. You will be able to install its SDK from the following link and explore C# 14 in the same way as described above for C# 13 with .NET 9:

https://dotnet.microsoft.com/en-us/download/dotnet/10.0

Again, the preceding link is for future use! It will give a 404 Missing resource error until February 2025, so do not bother using it until then.

Warning! Some C# language features depend on changes in the underlying .NET libraries. Even if you use the latest SDK with the latest compiler, you might not be able to use all the new language features while targeting an older version of .NET. For example, C# 11 introduced the required keyword, but it cannot be used in a project that targets .NET 6 because that language feature requires new attributes that are only available in .NET 7. Luckily, the compiler will warn you if you try to use a C# feature that is not supported. Just be prepared for that eventuality.

Showing the compiler version

We will start by writing code that shows the compiler version:

  1. If you’ve completed Chapter 1, Hello, C#! Welcome, .NET!, then you will already have a cs12dotnet8 folder. If not, then you’ll need to create it.
  2. Use your preferred code editor to create a new project, as defined in the following list:
    • Project template: Console App [C#] / console
    • Project file and folder: Vocabulary
    • Solution file and folder: Chapter02
    • Do not use top-level statements: Cleared
    • Enable native AOT publish: Cleared

    Good Practice: If you have forgotten how, or did not complete the previous chapter, then step-by-step instructions for creating a solution with multiple projects are given in Chapter 1, Hello, C#! Welcome, .NET!.

  1. In the Vocabulary project, in Program.cs, after the comment, add a statement to show the C# version as an error, as shown in the following code:
    #error version
    
  2. Run the console app:
    • If you are using Visual Studio 2022, then navigate to Debug | Start Without Debugging. When prompted to continue and run the last successful build, click No.
    • If you are using Visual Studio Code, then in a terminal for the Vocabulary folder, enter the dotnet run command. Note that we are expecting a compiler error, so do not panic when you see it!
  3. Note that the compiler version and the language version appear as compiler error message number CS8304, as shown in Figure 2.1:

    Figure 2.1: A compiler error that shows the C# language version

    The error message in the Visual Studio Code PROBLEMS window or Visual Studio Error List window says Compiler version: '4.8.0...' with language version default (12.0).

  1. Comment out the statement that causes the error, as shown in the following code:
    // #error version
    
  2. Note that the compiler error messages disappear.