C# in Depth, Fourth Edition
Jon Skeet
  • MEAP began March 2017
  • Publication in Early 2019 (estimated)
  • ISBN 9781617294532
  • 400 pages (estimated)
  • printed in black & white


An eBook copy of the previous edition, C# in Depth, Third Edition, is included at no additional cost. It will be automatically added to your Manning account within 24 hours of purchase.

C# is an amazing language that's up to any challenge you can throw at it. As a C# developer, you also need to be up to the task. C# in Depth, Fourth Edition is your key to unlocking the powerful new features added to the language in C# 5, 6, and 7. Following the expert guidance of C# legend Jon Skeet, you'll master asynchronous functions, expression-bodied members, interpolated strings, tuples, and much more.
Table of Contents detailed table of contents

Part 1: Introduction

1 Survival of the sharpest

Part 2: C# 2-4

2 C# 2


Introduction by example: collections before generics

Generics save the day

What can be generic?

Type inference for type arguments to methods

Type constraints

The default and typeof operators

Generic type initialization and state

Nullable value types

Aim: expressing an absence of information

CLR and Framework support: the Nullable<T> struct

Language support

Simplified delegate creation

Method group conversions

Anonymous methods

Delegate compatibility


Introduction to iterators

Lazy execution

Evaluation of yield statements

The importance of being lazy

Evaluation of finally blocks

Implementation sketch

Minor features

Partial types

Static classes

Separate getter/setter access for properties

Namespace aliases

Pragma directives

Fixed-size buffers



3 C# 3: LINQ and everything that comes with it

Automatically implemented properties

Implicit typing

Typing terminology

Implicitly typed local variables (var)

Implicitly typed arrays

Object and collection initializers

Introduction to object and collection initializers

Object initializers

Collection initializers

The benefits of single expressions for initialization

Anonymous types

Syntax and basic behavior

The compiler-generated type


Lambda expressions

Lambda expression syntax

Capturing variables

Expression trees

Extension methods

Declaring an extension method

Invoking an extension method

Chaining method calls

Query expressions

Query expressions translate from C# to C#

Range variables and transparent identifiers

Deciding when to use which syntax for LINQ

The end result: LINQ


4 C# 4

5 Writing asynchronous code

Introducing asynchronous functions

First encounters of the asynchronous kind

Breaking down the first example

Thinking about asynchrony

Fundamentals of asynchronous execution

Synchronization contexts

Modeling asynchronous methods

Async method declarations

Return types from async methods

Parameters in async methods

Await expressions

The awaitable pattern

Restrictions on await expressions

Wrapping of return values

Asynchronous method flow

What is awaited and when?

Evaluation of await expressions

The use of awaitable pattern members

Exception unwrapping

Method completion

Asynchronous anonymous functions

Custom task types in C# 7

The 99.9% case: ValueTask<TResult>

The 0.1% case: building your own custom task type

Async main methods in C# 7.1

Usage tips

Avoid context capture using ConfigureAwait (where appropriate)

Enable parallelism by starting multiple independent tasks

Avoid mixing synchronous and asynchronous code

Allow cancellation wherever possible

Testing asynchrony


6 Async implementation

Structure of the generated code

The stub method: preparation and taking the first step

Structure of the state machine

The MoveNext() method (high level)

The SetStateMachine method and the state machine boxing dance

A “simple” MoveNext() implementation

A full concrete example

MoveNext() method general structure

Zooming into an await expression

How control flow affects MoveNext()

Control flow between await expressions is simple

Awaiting within a loop

Awaiting within a try/finally block

Execution contexts and flow

Custom task types revisited


7 C# 5 bonus features

Capturing variables in foreach loops

Caller information attributes

Basic behavior


Simplifying INotifyPropertyChanged implementations

Corner cases of caller information attributes

Using caller information attributes with old versions of .NET


Part 3: C# 6

8 Super-sleek properties and expression-bodied members

A brief history of properties

Upgrades to automatically implemented properties

Read-only automatically implemented properties

Initializing automatically implemented properties

Automatically implemented properties in structs

Expression-bodied members

Even simpler read-only computed properties

Expression-bodied methods, indexers and operators

Restrictions on expression-bodied members in C# 6

Guidelines for using expression-bodied members


9 Stringy features

A recap on string formatting in .NET

Simple string formatting

Custom formatting with format strings


Introducing interpolated string literals

Simple interpolation

Format strings in interpolated string literals

Interpolated verbatim string literals

Compiler handling of interpolated string literals (part 1)

Localization using FormattableString

Compiler handling of interpolated string literals (part 2)

Formatting a FormattableString in a specific culture

Other uses for FormattableString

Using FormattableString with older versions of .NET

Uses, guidelines and limitations

Developers and machines, but maybe not end users

Hard limitations of interpolated string literals

When you can, but really shouldn’t

Accessing identifiers with nameof

Common uses of nameof

Extra details


10 A smörgåsbord of features for concise code

“Using static” directives

Importing static members

Extension methods and using static

Object and collection initializer enhancements

Indexers in object initializers

Using extension methods in collection initializers

Test code vs production code

The null conditional operator

Simple and safe property dereferencing

The null conditional operator in more detail

Handling Boolean comparisons

Indexers and the null conditional operator

Working effectively with the null conditional operator

Limitations of the null conditional operator

Exception filters

Syntax and semantics of exception filters

Retrying operations

Logging as a side-effect

Individual, case-specific exception filters

Why not just throw?


Part 4: C# 7 and beyond

11 Composition using tuples

Introduction to tuples

Tuple literals and tuple types


Inferred element names for tuple literals (C# 7.1)

Tuples as bags of variables

Tuple types and conversions

Types of tuple literals

Conversions from tuple literals to tuple types

Conversions between tuple types

Uses of conversions

Element name checking in inheritance

Tuples in the CLR

Introducing System.ValueTuple<…​>

Element name handling

Tuple conversion implementations

String representations of tuples

Regular equality and ordering comparisons

Structural equality and ordering comparisons

Womples and large tuples

The non-generic ValueTuple struct

Extension methods

Alternative options


Anonymous types

Named types

Uses and recommendations

Non-public APIs and easily-changed code

Local variables


Tuples and dynamic don’t play together nicely


12 Deconstruction and pattern matching

Deconstruction of tuples

Deconstruction to new variables

Deconstruction assignments to existing variables and properties

Details of tuple literal deconstruction

Deconstruction of non-tuple types

Instance deconstruction methods

Extension deconstruction methods and overloading

Compiler handling of Deconstruct calls

Introduction to pattern matching

Patterns available in C# 7.0

Constant patterns

Type patterns

The var pattern

Using patterns with the is operator

Using patterns with switch statements

Guard clauses

Pattern variable scope for case labels

Evaluation order of pattern-based switch statements

Thoughts on usage

Spotting deconstruction opportunities

Spotting pattern matching opportunities


13 Improving efficiency with more pass-by-reference

Recap: what do we know about ref?

Ref locals and ref returns

Ref locals

Ref return

The conditional ?: operator and ref values (C# 7.2)

Ref readonly (C# 7.2)

in parameters (C# 7.2)

Compatibility considerations

Just because the method can’t change an in parameter doesn’t mean nothing else can

Overloading with in parameters

Guidance for in parameters

Declaring structs as readonly (C# 7.2)

Background: implicit copying with read-only variables

The readonly modifier for structs

XML serialization is implicitly read-write

Extension methods with ref or in parameters (C# 7.2)

Restrictions on ref and in extension methods

Ref-like structs (C# 7.2)

Rules for ref-like structs

Span<T> and stackalloc

IL representation of ref-like structs


14 Concise code

Local methods

Variable access within local methods

Local method implementations

Usage guidelines

Out variables

Improvements to numeric literals

Binary integer literals

Underscore separators

Throw expressions

Default literals (C# 7.1)

Default literals and optional parameters

Non-trailing named arguments (C# 7.2)


15 The Future

C# 7.3 (items not currently covered by placeholders elsewhere)

Blittable types

C# release cadence and community involvement

C# 8

Nullable reference types

Features around type shapes and extensions

More async language integration

Record types



Appendix A: Features by language and framework versions

About the Technology

If you're a .NET developer, you'll use C# whether you're building an advanced enterprise application or just pushing out a quick ASP.NET app. C# 5, 6 and 7 have added a host of new features to help you write better code with tuples, string interpolation, pattern matching, and more. To really succeed with these powerful new features, however, you need to learn them in depth. This book is your ticket!

What's inside

  • The latest changes and updates for C# 5, 6, and 7
  • How C# works and why
  • Using asynchronous functions
  • Simpler string formatting with interpolation
  • Composition with tuples
  • Decomposition and pattern matching

About the reader

If you're a C# developer who's comfortable working with the language and wants to really dig in deep, then this book is for you.

About the author

Jon Skeet is a senior software engineer at Google. He studied mathematics and computer science at Cambridge, is a recognized authority in Java and C#, and maintains the position of top contributor to Stack Overflow.

Manning Early Access Program (MEAP) Read chapters as they are written, get the finished eBook as soon as it’s ready, and receive the pBook long before it's in bookstores.
MEAP combo $49.99 pBook + eBook + liveBook
C# in Depth, Fourth Edition (eBook) added to cart
continue shopping
go to cart

MEAP eBook $39.99 pdf + ePub + kindle + liveBook

FREE domestic shipping on three or more pBooks