Programmingcsharp10 Preview
Programmingcsharp10 Preview
An attribution usually
includes the title, author, publisher, and ISBN. For example: <Programming C# 10
by Ian Griffiths (O’Reilly). Copyright 2022 by Ian Griffiths, 978-1-098-11781-8.=
If you feel your use of code examples falls outside fair use or the permission given
above, feel free to contact us at [email protected].
Our unique network of experts and innovators share their knowledge and expertise
through books, articles, conferences, and our online learning platform. O’Reilly’s
online learning platform gives you on-demand access to live training courses, in-
depth learning paths, interactive coding environments, and a vast collection of text
and video from O’Reilly and 200+ other publishers. For more information, please
visit http://oreilly.com.
How to Contact Us
Please address comments and questions concerning this book to the publisher:
We have a web page for this book, where we list errata, examples, and any additional
information. You can access this page at https://oreil.ly/prgrmg-c-10.
Email us with comments or technical questions at [email protected].
For news and information about our books and courses, visit https://oreilly.com.
Find us on LinkedIn: https://linkedin.com/company/oreilly-media.
Follow us on Twitter: https://twitter.com/oreillymedia.
Watch us on YouTube: https://youtube.com/oreillymedia.
Preface | xix
Acknowledgments
Many thanks to the book’s official technical reviewers: Stephen Toub, Howard van
Rooijen, and Glyn Griffiths. I’d also like to give a big thank-you to those who
reviewed individual chapters or otherwise offered help or information that improved
this book: Brian Rasmussen, Eric Lippert, Andrew Kennedy, Daniel Sinclair, Brian
Randell, Mike Woodring, Mike Taulty, Bart De Smet, Matthew Adams, Jess Panni,
Jonathan George, Mike Larah, Carmel Eve, Ed Freeman, Elisenda Gascon, Jessica
Hill, Liam Mooney, Nehemiah Campbell, and Shahryar Saljoughi. Thanks in particu‐
lar to endjin, both for allowing me to take time out from work to write this book and
for creating such a great place to work.
Thank you to everyone at O’Reilly whose work brought this book into existence. In
particular, thanks to Corbin Collins for his support in making this book happen,
and to Amanda Quinn for her support in getting this project started. Thanks also to
Elizabeth Faerm, Cassandra Furtado, Ron Bilodeau, Nick Adams, Kate Dullea, Karen
Montgomery, and Kristen Brown, for their help in bringing the work to completion.
Further thanks to Sue Klefstad and WordCo Indexing Services, Inc. for their work
on the index. Thanks also to Kim Cofer for her thorough and thoughtful copy
editing and to Kim Sandoval for her diligent proofreading. Finally, thank you to John
Osborn, for taking me on as an O’Reilly author back when I wrote my first book.
xx | Preface
CHAPTER 1
Introducing C#
The C# programming language (pronounced <see sharp=) is used for many kinds of
applications, including websites, cloud-based systems, IoT devices, machine learning,
desktop applications, embedded controllers, mobile apps, games, and command-line
utilities. C#, along with the supporting runtime, libraries, and tools known collec‐
tively as .NET, has been center stage for Windows developers for over 20 years.
Today, .NET is cross-platform and open source, enabling applications and services
written in C# to run on operating systems including Android, iOS, macOS, and
Linux, as well as on Windows.
The release of C# 10.0 and its corresponding runtime, .NET 6.0, marks an important
milestone: C#’s journey to becoming a fully cross-platform, open source language
is now complete. Although open source implementations have existed for most of
C#’s history, a sea change began in 2016 when Microsoft released .NET Core 1.0, the
first platform fully supported by Microsoft for running C# on Linux and macOS as
well as Windows. Library and tool support for .NET Core was initially patchy, so
Microsoft continued to ship new versions of its older runtime, the closed-source,
Windows-only .NET Framework, but six years on, that old runtime is effectively
being retired,1 now that the cross-platform version has comprehensively overtaken
it. .NET 5.0 dropped the <Core= from its name to signify that it is now the main event,
but it is with .NET 6.0 that the cross-platform version has really arrived, because this
version enjoys full Long Term Support (LTS) status. For the first time, the platform-
independent version of C# and .NET has superseded the old .NET Framework.
C# and .NET are open source projects, although it didn’t start out that way. In
C#’s early history, Microsoft guarded all of its source code closely, but in 2014,
1 The old .NET Framework will be supported for many years to come, but Microsoft has stated that it will not
get any new features.
1
the .NET Foundation was created to foster the development of open source projects
in the .NET world. Many of Microsoft’s most important C# and .NET projects
are now under the foundation’s governance (in addition to many non-Microsoft
projects). This includes Microsoft’s C# compiler and also the .NET runtime and
libraries. Today, pretty much everything surrounding C# is developed in the open,
with code contributions from outside of Microsoft being welcome. New language
feature proposals are managed on GitHub, enabling community involvement from
the earliest stages.
Why C#?
Although there are many ways you can use C#, other languages are always an option.
Why might you choose C# over those? It will depend on what you need to do
and what you like and dislike in a programming language. I find that C# provides
considerable power, flexibility, and performance and works at a high enough level
of abstraction that I don’t expend vast amounts of effort on little details not directly
related to the problems my programs are trying to solve.
Much of C#’s power comes from the range of programming techniques it supports.
For example, it offers object-oriented features, generics, and functional program‐
ming. It supports both dynamic and static typing. It provides powerful list- and
set-oriented features, thanks to Language Integrated Query (LINQ). It has intrinsic
support for asynchronous programming. Moreover, the various development envi‐
ronments that support C# all offer a wide range of productivity-enhancing features.
C# provides options for balancing ease of development against performance. The
runtime has always provided a garbage collector (GC) that frees developers from
much of the work associated with recovering memory that the program is no longer
using. A GC is a common feature in modern programming languages, and while
it is a boon for most programs, there are some specialized scenarios where its perfor‐
mance implications are problematic, so C# enables more explicit memory manage‐
ment, giving you the option to trade ease of development for runtime performance
but without the loss of type safety. This makes C# suitable for certain performance-
critical applications that for years were the preserve of less safe languages such as C
and C++.
Languages do not exist in a vacuum—high-quality libraries with a broad range of
features are essential. Some elegant and academically beautiful languages are glorious
right up until you want to do something prosaic, such as talking to a database or
determining where to store user settings. No matter how powerful a set of program‐
ming idioms a language offers, it also needs to provide full and convenient access to
the underlying platform’s services. C# is on very strong ground here, thanks to its
runtime, built-in class libraries, and extensive third-party library support.
2 | Chapter 1: Introducing C#
.NET encompasses both the runtime and the main class libraries that C# programs
use. The runtime part is called the Common Language Runtime (usually abbreviated
to CLR) because it supports not just C# but any .NET language. Microsoft also offers
Visual Basic, F#, and .NET extensions for C++, for example. The CLR has a Common
Type System (CTS) that enables code from multiple languages to interoperate freely,
which means that .NET libraries can normally be used from any .NET language—F#
can consume libraries written in C#, C# can use Visual Basic libraries, and so on.
There is an extensive set of class libraries built into .NET. These have gone by a few
names over the years, including Base Class Library (BCL), Framework Class Library,
and framework libraries, but Microsoft now seems to have settled on runtime libraries
as the name for this part of .NET. These libraries provide wrappers for many features
of the underlying operating system (OS), but they also provide a considerable amount
of functionality of their own, such as collection classes and JSON processing.
The .NET runtime class libraries are not the whole story—many other systems
provide their own .NET libraries. For example, there are libraries that enable C#
programs to use popular cloud services. As you’d expect, Microsoft provides com‐
prehensive .NET libraries for working with services in its Azure cloud platform.
Likewise, Amazon provides a fully featured development kit for using Amazon Web
Services (AWS) from C# and other .NET languages. And libraries do not have to be
associated with particular services. There’s a large ecosystem of .NET libraries, some
commercial and some free, including mathematical utilities, parsing libraries, and
user interface (UI) components, to name just a few. Even if you get unlucky and
need to use an OS feature that doesn’t have any .NET library wrappers, C# offers
various mechanisms for working with other kinds of APIs, such as the C-style APIs
available in Win32, macOS, and Linux, or APIs based on the Component Object
Model (COM) in Windows.
In addition to libraries, there are also numerous applications frameworks. .NET has
built-in frameworks for creating web apps and web APIs, desktop applications, and
mobile applications. There are also open source frameworks for various styles of
distributed systems development, such as high-volume event processing with Reaqtor
or high-availability globally distributed systems with Orleans.
Finally, with .NET having been around for over two decades, many organizations
have invested extensively in technology built on this platform. So C# is often the
natural choice for reaping the rewards of these investments.
In summary, with C# we get a strong set of abstractions built into the language,
a powerful runtime, and easy access to an enormous amount of library and
platform functionality.
Why C#? | 3