Introducing the C# language
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.
This book covers features of the C# language from version 1.0 up to the latest version 10.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.
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 |
6 |
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 |
11 |
All topics in Chapter 11, 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 |
Storing dynamic types |
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 |
12 |
Understanding async and await |
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 |
|
2 |
Simplifying the usage of the console |
Interpolated strings |
2 |
Displaying output to the user |
Expression bodied members |
5 |
Defining read-only properties |
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 |
|
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 |
|
12 |
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 |
|
5 |
Understanding access modifiers |
You can test |
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.
C# 8
C# 8 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 |
6 |
Making a reference type nullable |
Switch expressions |
3 |
Simplifying |
Default interface methods |
6 |
Understanding default interface methods |
C# 9
C# 9 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 |
Target-typed new |
2 |
Using target-typed new to instantiate objects |
Enhanced pattern matching |
5 |
Pattern matching with objects |
Records |
5 |
Working with records |
C# 10
C# 10 was released in November 2021 and focused on features that minimize the amount of code needed in common scenarios, including the topics listed in the following table:
Feature |
Chapter |
Topic |
Global namespace imports |
2 |
Importing namespaces |
Constant string literals |
2 |
Formatting using interpolated strings |
File-scoped namespaces |
5 |
Simplifying namespace declarations |
Required properties |
5 |
Requiring properties to be set during instantiation |
Record structs |
6 |
Working with record struct types |
Null parameter checks |
6 |
Checking for null in method parameters |
Understanding C# standards
Over the years, Microsoft has submitted a few versions of C# to standards bodies, as shown in the following table:
C# version |
ECMA standard |
ISO/IEC standard |
1.0 |
ECMA-334:2003 |
ISO/IEC 23270:2003 |
2.0 |
ECMA-334:2006 |
ISO/IEC 23270:2006 |
5.0 |
ECMA-334:2017 |
ISO/IEC 23270:2018 |
The standard for C# 6 is still a draft and work on adding C# 7 features is progressing. Microsoft made C# open source in 2014.
There are currently three public GitHub repositories for making the work on C# and related technologies as open as possible, as shown in the following table:
Description |
Link |
C# language design |
|
Compiler implementation |
|
Standard to describe the language |
Discovering your C# compiler versions
.NET language compilers for C# and Visual Basic, also known as Roslyn, along with a separate compiler for F#, are 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 the following table:
.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 |
3.9 - 3.10 |
10.0 |
When you create class libraries then you can choose to target .NET Standard as well as versions of modern .NET. They have default C# language versions, as shown in the following table:
.NET Standard |
C# |
2.0 |
7.3 |
2.1 |
8.0 |
How to output the SDK version
Let's see what .NET SDK and C# language compiler versions you have available:
- On macOS, start Terminal. On Windows, start Command Prompt.
- To determine which version of the .NET SDK you have available, enter the following command:
dotnet --version
- Note the version at the time of writing is 6.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:
6.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.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 <LangVersion>
configuration element to the project file, as shown in the following markup:
<LangVersion>7.3</LangVersion>
After the release of C# 10.0 with .NET 6.0, if Microsoft releases a C# 10.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>10.1</LangVersion>
Potential values for the <LangVersion>
are shown in the following table:
LangVersion |
Description |
7, 7.1, 7.2, 7.3 8, 9, 10 |
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, 10.0 in November 2021. |
|
Uses the highest major and highest minor number, for example, 7.2 in 2017, 7.3 in 2018, 8 in 2019, perhaps 10.1 in early 2022. |
|
Uses the highest available preview version, for example, 10.0 in July 2021 with .NET 6.0 Preview 6 installed. |
After creating a new project, 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>net6.0</TargetFramework>
<LangVersion>preview</LangVersion>
</PropertyGroup>
</Project>
Your projects must target net6.0
to use the full features of C# 10.
Good Practice: If you are using Visual Studio Code and you have not done so already, install the Visual Studio Code extension named MSBuild project tools. This will give you IntelliSense while editing .csproj
files, including making it easy to add the <LangVersion>
element with appropriate values.