Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
C# 9 and .NET 5 – Modern Cross-Platform Development

You're reading from   C# 9 and .NET 5 – Modern Cross-Platform Development Build intelligent apps, websites, and services with Blazor, ASP.NET Core, and Entity Framework Core using Visual Studio Code

Arrow left icon
Product type Paperback
Published in Nov 2020
Publisher Packt
ISBN-13 9781800568105
Length 822 pages
Edition 5th Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Mark J. Price Mark J. Price
Author Profile Icon Mark J. Price
Mark J. Price
Arrow right icon
View More author details
Toc

Table of Contents (23) Chapters Close

Preface 1. Hello, C#! Welcome, .NET! 2. Speaking C# FREE CHAPTER 3. Controlling Flow and Converting Types 4. Writing, Debugging, and Testing Functions 5. Building Your Own Types with Object-Oriented Programming 6. Implementing Interfaces and Inheriting Classes 7. Understanding and Packaging .NET Types 8. Working with Common .NET Types 9. Working with Files, Streams, and Serialization 10. Protecting Your Data and Applications 11. Working with Databases Using Entity Framework Core 12. Querying and Manipulating Data Using LINQ 13. Improving Performance and Scalability Using Multitasking 14. Introducing Practical Applications of C# and .NET 15. Building Websites Using ASP.NET Core Razor Pages 16. Building Websites Using the Model-View-Controller Pattern 17. Building Websites Using a Content Management System 18. Building and Consuming Web Services 19. Building Intelligent Apps Using Machine Learning 20. Building Web User Interfaces Using Blazor 21. Building Cross-Platform Mobile Apps 22. Index

Introducing C#

This part of the book is about the C# language—the grammar and vocabulary that you will use every day to write the source code for your applications.

Programming languages have many similarities to human languages, except that in programming languages, you can make up your own words, just like Dr. Seuss!

In a book written by Dr. Seuss in 1950, If I Ran the Zoo, he states this:

"And then, just to show them, I'll sail to Ka-Troo And Bring Back an It-Kutch, a Preep, and a Proo, A Nerkle, a Nerd, and a Seersucker, too!"

Understanding language versions and features

This part of the book covers the C# programming language and is written primarily for beginners, so it covers the fundamental topics that all developers need to know, from declaring variables to storing data to how to define your own custom data types.

Advanced and obscure topics like ref local variable reassignment and reference semantics with value types are not covered.

This book covers features of the C# language from version 1.0 up to the latest version, 9.0. If you already have some familiarity with older versions of C# and are excited to find out about the new features in the most recent versions of C#, I have made it easier for you to jump around by listing language versions and their important new features below, along with the chapter number and topic title where you can learn about them.

More Information: You can learn more about the current status of the C# language at this link: https://github.com/dotnet/roslyn/blob/master/docs/Language%20Feature%20Status.md

C# 1.0

C# 1.0 was released in 2002 and included all the important features of a statically typed object-oriented modern language, as you will see throughout chapters 2 to 6.

C# 2.0

C# 2.0 was released in 2005 and focused on enabling strong data typing using generics, to improve code performance and reduce type errors, including the topics listed in the following table:

Feature

Chapter

Topic

Nullable value types

2

Making a value type nullable

Generics

6

Making types more reusable with generics

C# 3.0

C# 3.0 was released in 2007 and focused on enabling declarative coding with Language INtegrated Queries (LINQ) and related features like anonymous types and lambda expressions, including the topics listed in the following table:

Feature

Chapter

Topic

Implicitly typed local variables

2

Inferring the type of a local variable

LINQ

12

All topics in Chapter 12, Querying and Manipulating Data Using LINQ

C# 4.0

C# 4.0 was released in 2010 and focused on improving interoperability with dynamic languages like F# and Python, including the topics listed in the following table:

Feature

Chapter

Topic

Dynamic types

2

The dynamic type

Named/optional arguments

5

Optional parameters and named arguments

C# 5.0

C# 5.0 was released in 2012 and focused on simplifying asynchronous operation support by automatically implementing complex state machines while writing what looks like synchronous statements, including the topics listed in the following table:

Feature

Chapter

Topic

Simplified asynchronous tasks

13

Understanding async and await

More Information: You can download the C# Language Specification 5.0 from the following link: https://www.microsoft.com/en-us/download/details.aspx?id=7029

C# 6.0

C# 6.0 was released in 2015 and focused on minor refinements to the language, including the topics listed in the following table:

Feature

Chapter

Topic

Static imports

2

Simplifying the usage of the console

Interpolated strings

2

Displaying output to the user

Expression bodied members

5

Defining read-only properties

More Information: You can read draft proposals for C# Language Specifications for 6.0 and later at the following link: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/

C# 7.0

C# 7.0 was released in March 2017 and focused on adding functional language features like tuples and pattern matching, as well as minor refinements to the language, including the topics listed in the following table:

Feature

Chapter

Topic

Binary literals and digit separators

2

Storing whole numbers

Pattern matching

3

Pattern matching with the if statement

out variables

5

Controlling how parameters are passed

Tuples

5

Combining multiple values with tuples

Local functions

6

Defining local functions

C# 7.1

C# 7.1 was released in August 2017 and focused on minor refinements to the language, including the topics listed in the following table:

Feature

Chapter

Topic

Default literal expressions

5

Setting fields with default literal

Inferred tuple element names

5

Inferring tuple names

async Main

13

Improving responsiveness for console apps

C# 7.2

C# 7.2 was released in November 2017 and focused on minor refinements to the language, including the topics listed in the following table:

Feature

Chapter

Topic

Leading underscores in numeric literals

2

Storing whole numbers

Non-trailing named arguments

5

Optional parameters and named arguments

private protected access modifier

5

Understanding access modifiers

You can test == and != with tuple types

5

Comparing tuples

C# 7.3

C# 7.3 was released in May 2018 and focused on performance-oriented safe code that improves ref variables, pointers, and stackalloc. These are advanced and rarely needed for most developers, so they are not covered in this book.

More Information: If you're interested, you can read the details at the following link: https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-7-3

C# 8.0

C# 8.0 was released in September 2019 and focused on a major change to the language related to null handling, including the topics listed in the following table:

Feature

Chapter

Topic

Nullable reference types

2

Making a reference type nullable

Switch expressions

3

Simplifying switch statements with switch expressions

Default interface methods

6

Understanding default interface methods

C# 9.0

C# 9.0 was released in November 2020 and focused on record types, refinements to pattern matching, and minimal-code console apps, including the topics listed in the following table:

Feature

Chapter

Topic

Minimal-code console apps

1

Top-level programs

Enhanced pattern matching

5

Pattern matching with objects

Records

5

Working with records

More Information: You can read more about new C# 9.0 features at the following link: https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-9

Discovering your C# compiler versions

With the C# 7.x generation, Microsoft decided to increase the cadence of language releases, releasing minor version numbers, also known as point releases, for the first time since C# 1.1.

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

.NET SDK

Roslyn

C#

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

5.0

9.0

More Information: You can see a list of versions at the following link: https://github.com/dotnet/roslyn/blob/master/docs/wiki/NuGet-packages.md

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

  1. Start Visual Studio Code.
  2. Navigate to View | Terminal.
  3. To determine which version of the .NET SDK you have available, enter the following command:
    dotnet --version
    
  4. Note the version at the time of writing is 5.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:
    5.0.100
    
  5. To determine which versions of the C# compiler you have available, enter the following command:
    csc -langversion:?
    
  6. Note all the versions available at the time of writing, as shown in the following output:
    Supported language versions:
    default
    1
    2
    3
    4
    5
    6
    7.0
    7.1
    7.2
    7.3
    8.0
    9.0 (default)
    latestmajor
    preview
    latest
    

More Information: On Windows, the preceding command returns the error, The name "csc" is not recognized as the name of a command, function, script file, or executable program. Check the spelling of the name, as well as the presence and correctness of the path. To fix this issue, follow the instructions at the following link: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/compiler-options/command-line-building-with-csc-exe

Enabling a specific language version compiler

Developer tools like Visual Studio Code 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.0 was released, C# 7.0 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 configuration element to the project file, as shown in the following markup:

<LangVersion>7.3</LangVersion>

After the release of C# 9.0 with .NET 5.0, if Microsoft releases a C# 9.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>9.1</LangVersion>

Potential values for the <LangVersion> are shown in the following table:

LangVersion

Description

7, 7.1, 7.2, 7.3, 8, 9

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.0 in October 2019, 9.0 in November 2020.

latest

Uses the highest major and highest minor number, for example, 7.2 in 2017, 7.3 in 2018, 8 in 2019, 9 in 2020, perhaps 9.1 in early 2021.

preview

Uses the highest available preview version, for example, 9.0 in May 2020 with .NET 5.0 Preview 4 installed.

After creating a new project with the dotnet command-line tool, you can edit the csproj file and add the <LangVersion> element, as shown highlighted in the following markup:

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

Your projects must target net5.0 to use the full features of C# 9.

If you have not done so already, install the extension MSBuild project tools. This will give you IntelliSense while editing .csproj files, including making it easy to add the <LangVersion> element with appropriate values.

More Information: You can read about C# language versioning at the following link: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/configure-language-version

You have been reading a chapter from
C# 9 and .NET 5 – Modern Cross-Platform Development - Fifth Edition
Published in: Nov 2020
Publisher: Packt
ISBN-13: 9781800568105
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime