Joseph Albahari & Ben Albahari
C # 7. 0
in a Nutshell
THE DEFINITIVE REFERENCE
7th Edition
Covers .NET Standard 2
C# 7.0
IN A NUTSHELL
Joseph Albahari and Ben Albahari
978-1-491-98765-0
[M]
C# 7.0 in a Nutshell
by Joseph Albahari and Ben Albahari
Copyright © 2018 Joseph Albahari, Ben Albahari. All rights reserved.
Printed in the United States of America.
Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472.
O’Reilly books may be purchased for educational, business, or sales promotional use. Online
editions are also available for most titles (http://oreilly.com/safari). For more information,
contact our corporate/institutional sales department: 800-998-9938 or corporate@oreilly.com.
Editors:
Rachel Roumeliotis Indexer: Judith McConville
Production Editor: Colleen Cole Interior Designer: David Futato
Copyeditor: Jasmine Kwityn Cover Designer: Karen Montgomery
Proofreader: Kim Cofer Illustrator: Rebecca Demarest
October 2017: First Edition
Revision History for the First Edition
2017-10-03:
First Release
2017-11-03: Second Release
See http://oreilly.com/catalog/errata.csp?isbn=9781491987650 for release details.
The O’Reilly logo is a registered trademark of O’Reilly Media, Inc. C# 7.0 in a Nutshell, the
cover image, and related trade dress are trademarks of O’Reilly Media, Inc.
While the publisher and the authors have used good faith efforts to ensure that the informa‐
tion and instructions contained in this work are accurate, the publisher and the authors dis‐
claim all responsibility for errors or omissions, including without limitation responsibility for
damages resulting from the use of or reliance on this work. Use of the information and
instructions contained in this work is at your own risk. If any code samples or other technol‐
ogy this work contains or describes is subject to open source licenses or the intellectual prop‐
erty rights of others, it is your responsibility to ensure that your use thereof complies with
such licenses and/or rights.
Table of Contents
Preface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xi
1.
Introducing C# and the .NET Framework. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Object Orientation 1
Type Safety 2
Memory Management 3
Platform Support 3
C# and the CLR 3
The CLR and .NET Framework 4
Other Frameworks 5
A Brief History of C# 8
2.
C# Language Basics. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
A First C# Program 15
Syntax 18
Type Basics 21
Numeric Types 30
Boolean Type and Operators 37
Strings and Characters 39
Arrays 42
Variables and Parameters 46
Expressions and Operators 56
Null Operators 60
Statements 62
Namespaces 71
3.
Creating Types in C#. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
Classes 79
Inheritance 96
The object Type 105
iii