0% found this document useful (0 votes)
70 views77 pages

Pro C# 8 With .NET Core 3: Foundational Principles and Practices in Programming - Ninth Edition Andrew Troelsen Full

Pro C# 8 with .NET Core 3: Foundational Principles and Practices in Programming is a comprehensive guide authored by Andrew Troelsen and Phil Japikse, covering essential programming concepts and practices in C# and .NET Core. The book is available in multiple formats, including PDF, and has received high ratings from readers. It serves as a foundational resource for both new and experienced developers looking to enhance their skills in C# programming.

Uploaded by

skorrizhihui
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
70 views77 pages

Pro C# 8 With .NET Core 3: Foundational Principles and Practices in Programming - Ninth Edition Andrew Troelsen Full

Pro C# 8 with .NET Core 3: Foundational Principles and Practices in Programming is a comprehensive guide authored by Andrew Troelsen and Phil Japikse, covering essential programming concepts and practices in C# and .NET Core. The book is available in multiple formats, including PDF, and has received high ratings from readers. It serves as a foundational resource for both new and experienced developers looking to enhance their skills in C# programming.

Uploaded by

skorrizhihui
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 77

Pro C# 8 with .

NET Core 3: Foundational Principles


and Practices in Programming - Ninth Edition Andrew
Troelsen pdf version

https://textbookfull.com/product/pro-c-8-with-net-
core-3-foundational-principles-and-practices-in-programming-ninth-
edition-andrew-troelsen/

★★★★★
4.7 out of 5.0 (18 reviews )

Instant PDF Download

textbookfull.com
Pro C# 8 with .NET Core 3: Foundational Principles and
Practices in Programming - Ninth Edition Andrew Troelsen

TEXTBOOK

Available Formats

■ PDF eBook Study Guide Ebook

EXCLUSIVE 2025 ACADEMIC EDITION – LIMITED RELEASE

Available Instantly Access Library


More products digital (pdf, epub, mobi) instant
download maybe you interests ...

Pro C 9 with NET 5 Foundational Principles and


Practices in Programming Tenth Edition Andrew Troelsen
Phillip Japikse

https://textbookfull.com/product/pro-c-9-with-net-5-foundational-
principles-and-practices-in-programming-tenth-edition-andrew-
troelsen-phillip-japikse/

Pro C 9 with NET 5 Foundational Principles and


Practices in Programming Tenth Edition Andrew Troelsen
Phillip Japikse

https://textbookfull.com/product/pro-c-9-with-net-5-foundational-
principles-and-practices-in-programming-tenth-edition-andrew-
troelsen-phillip-japikse-2/

Pro C# 7 with .NET and .NET Core Andrew Troelsen

https://textbookfull.com/product/pro-c-7-with-net-and-net-core-
andrew-troelsen/

C 8 0 and NET Core 3 0 Modern Cross Platform


Development Build applications with C NET Core Entity
Framework Core ASP NET Core and ML NET using Visual
Studio Code 4th Edition Mark J. Price
https://textbookfull.com/product/c-8-0-and-net-core-3-0-modern-
cross-platform-development-build-applications-with-c-net-core-
entity-framework-core-asp-net-core-and-ml-net-using-visual-
Modern Data Access with Entity Framework Core: Database
Programming Techniques for .NET, .NET Core, UWP, and
Xamarin with C# 1st Edition Holger Schwichtenberg

https://textbookfull.com/product/modern-data-access-with-entity-
framework-core-database-programming-techniques-for-net-net-core-
uwp-and-xamarin-with-c-1st-edition-holger-schwichtenberg/

Modern Data Access with Entity Framework Core: Database


Programming Techniques for . NET, . NET Core, UWP, and
Xamarin with C# 1st Edition Holger Schwichtenberg

https://textbookfull.com/product/modern-data-access-with-entity-
framework-core-database-programming-techniques-for-net-net-core-
uwp-and-xamarin-with-c-1st-edition-holger-schwichtenberg-2/

C# 12 and .NET 8 – Modern Cross-Platform Development


Fundamentals: Start building websites and services with
ASP.NET Core 8, Blazor, and EF Core 8, 8th Edition Mark
J. Price
https://textbookfull.com/product/c-12-and-net-8-modern-cross-
platform-development-fundamentals-start-building-websites-and-
services-with-asp-net-core-8-blazor-and-ef-core-8-8th-edition-
mark-j-price/

Pro ASP NET Core Identity Under the Hood with


Authentication and Authorization in ASP NET Core 5 and
6 Applications 1st Edition Adam Freeman

https://textbookfull.com/product/pro-asp-net-core-identity-under-
the-hood-with-authentication-and-authorization-in-asp-net-
core-5-and-6-applications-1st-edition-adam-freeman/

Building Single Page Applications in .NET Core 3 :


Jumpstart Coding Using Blazor and C# Michele Aponte

https://textbookfull.com/product/building-single-page-
applications-in-net-core-3-jumpstart-coding-using-blazor-and-c-
michele-aponte/
Pro C# 8 with
.NET Core 3
Foundational Principles and Practices
in Programming

Ninth Edition

Andrew Troelsen
Phil Japikse
Pro C# 8 with .NET
Core 3
Foundational Principles and Practices
in Programming

Ninth Edition

Andrew Troelsen
Phil Japikse
Pro C# 8 with .NET Core 3: Foundational Principles and Practices in Programming
Andrew Troelsen Phil Japikse
Minneapolis, MN, USA West Chester, OH, USA

ISBN-13 (pbk): 978-1-4842-5755-5 ISBN-13 (electronic): 978-1-4842-5756-2


https://doi.org/10.1007/978-1-4842-5756-2

Copyright © 2020 by Andrew Troelsen and Phil Japikse


This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the
material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation,
broadcasting, reproduction on microfilms or in any other physical way, and transmission or information
storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now
known or hereafter developed.
Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with
every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an
editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the
trademark.
The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are
not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to
proprietary rights.
While the advice and information in this book are believed to be true and accurate at the date of publication,
neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or
omissions that may be made. The publisher makes no warranty, express or implied, with respect to the
material contained herein.
Managing Director, Apress Media LLC: Welmoed Spahr
Acquisitions Editor: Joan Murray
Development Editor: Laura Berendson
Coordinating Editor: Jill Balzano
Cover image designed by Freepik (www.freepik.com)
Distributed to the book trade worldwide by Springer Science+Business Media New York, 233 Spring Street,
6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail orders-ny@springer-
sbm.com, or visit www.springeronline.com. Apress Media, LLC is a California LLC and the sole member
(owner) is Springer Science + Business Media Finance Inc (SSBM Finance Inc). SSBM Finance Inc is a
Delaware corporation.
For information on translations, please e-mail [email protected]; for reprint,
paperback, or audio rights, please e-mail [email protected].
Apress titles may be purchased in bulk for academic, corporate, or promotional use. eBook versions and
licenses are also available for most titles. For more information, reference our Print and eBook Bulk Sales
web page at http://www.apress.com/bulk-sales.
Any source code or other supplementary material referenced by the author in this book is available to
readers on GitHub via the book’s product page, located at www.apress.com/9781484257555. For more
detailed information, please visit http://www.apress.com/source-code.
Printed on acid-free paper
To my family, Amy (wife), Conner (son), Logan (son), and Skylar (daughter),
thank you for all of the support and patience you have given me. Also, to my father (Cor);
father, husband, inventor, and my center of excellence.

—Phil
Table of Contents

About the Authors���������������������������������������������������������������������������������������������������xlv


About the Technical Reviewer������������������������������������������������������������������������������xlvii
Acknowledgments�������������������������������������������������������������������������������������������������xlix
Introduction�������������������������������������������������������������������������������������������������������������� li


■Part I: Introducing C# and .NET Core�������������������������������������������������� 1

■Chapter 1: Introducing .NET Core�������������������������������������������������������������������������� 3
Some Key Benefits of the .NET Core Platform������������������������������������������������������������������ 4
The .NET Core Support Lifecycle�������������������������������������������������������������������������������������� 4
Introducing the Building Blocks of the .NET Core Platform
(CoreCLR, CoreFX, CTS, and CLS)������������������������������������������������������������������������������������� 5
The Role of the Base Class Libraries������������������������������������������������������������������������������������������������������ 6
What C# Brings to the Table������������������������������������������������������������������������������������������������������������������� 6
Managed vs. Unmanaged Code������������������������������������������������������������������������������������������������������������ 10

Additional .NET Core Aware Programming Languages��������������������������������������������������� 10


An Overview of .NET Assemblies������������������������������������������������������������������������������������ 10
The Role of the Common Intermediate Language�������������������������������������������������������������������������������� 11
Benefits of CIL�������������������������������������������������������������������������������������������������������������������������������������� 14
Compiling CIL to Platform-Specific Instructions����������������������������������������������������������������������������������� 14
The Role of .NET Core Type Metadata��������������������������������������������������������������������������������������������������� 15
The Role of the Assembly Manifest������������������������������������������������������������������������������������������������������ 15
Understanding the Common Type System���������������������������������������������������������������������� 16
CTS Class Types������������������������������������������������������������������������������������������������������������������������������������ 16
CTS Interface Types������������������������������������������������������������������������������������������������������������������������������ 17
■ Table of Contents

CTS Structure Types����������������������������������������������������������������������������������������������������������������������������� 18


CTS Enumeration Types������������������������������������������������������������������������������������������������������������������������ 18
CTS Delegate Types������������������������������������������������������������������������������������������������������������������������������ 19
CTS Type Members������������������������������������������������������������������������������������������������������������������������������� 19
Intrinsic CTS Data Types����������������������������������������������������������������������������������������������������������������������� 19

Understanding the Common Language Specification���������������������������������������������������� 20


Ensuring CLS Compliance��������������������������������������������������������������������������������������������������������������������� 22

Understanding the CoreCLR and CoreFX������������������������������������������������������������������������ 22


The Assembly/Namespace/Type Distinction������������������������������������������������������������������ 23
Accessing a Namespace Programmatically����������������������������������������������������������������������������������������� 24
Referencing External Assemblies��������������������������������������������������������������������������������������������������������� 25

Exploring an Assembly Using ildasm.exe����������������������������������������������������������������������� 25


Summary������������������������������������������������������������������������������������������������������������������������ 26

■Chapter 2: Building C# Applications�������������������������������������������������������������������� 27
Installing .NET Core�������������������������������������������������������������������������������������������������������� 27
Confirm the .NET Core Install���������������������������������������������������������������������������������������������������������������� 28

Building .NET Core Applications with Visual Studio�������������������������������������������������������� 28


Installing Visual Studio 2019���������������������������������������������������������������������������������������������������������������� 29
Taking Visual Studio 2019 for a Test-Drive������������������������������������������������������������������������������������������� 30
The .NET Framework Documentation��������������������������������������������������������������������������������������������������� 40
Building .NET Core Applications on a Non-Windows OS������������������������������������������������ 41
Summary������������������������������������������������������������������������������������������������������������������������ 41

■Part II: Core C# Programming����������������������������������������������������������� 43

■Chapter 3: Core C# Programming Constructs, Part 1������������������������������������������ 45
The Anatomy of a Simple C# Program���������������������������������������������������������������������������� 45
Variations on the Main() Method (Updated 7.1)������������������������������������������������������������������������������������ 47
Specifying an Application Error Code��������������������������������������������������������������������������������������������������� 48
Processing Command-Line Arguments������������������������������������������������������������������������������������������������ 49
Specifying Command-Line Arguments with Visual Studio�������������������������������������������������������������������� 51

An Interesting Aside: Some Additional Members of the System.Environment Class������ 52


vi
■ Table of Contents

The System.Console Class��������������������������������������������������������������������������������������������� 53


Basic Input and Output with the Console Class������������������������������������������������������������������������������������ 54
Formatting Console Output������������������������������������������������������������������������������������������������������������������� 55
Formatting Numerical Data������������������������������������������������������������������������������������������������������������������ 55
Formatting Numerical Data Beyond Console Applications������������������������������������������������������������������� 57

System Data Types and Corresponding C# Keywords���������������������������������������������������� 57


Variable Declaration and Initialization�������������������������������������������������������������������������������������������������� 58
Intrinsic Data Types and the new Operator������������������������������������������������������������������������������������������� 60
The Data Type Class Hierarchy������������������������������������������������������������������������������������������������������������� 61
Members of Numerical Data Types������������������������������������������������������������������������������������������������������� 62
Members of System.Boolean���������������������������������������������������������������������������������������������������������������� 63
Members of System.Char��������������������������������������������������������������������������������������������������������������������� 63
Parsing Values from String Data����������������������������������������������������������������������������������������������������������� 64
Using TryParse to Parse Values from String Data��������������������������������������������������������������������������������� 64
System.DateTime and System.TimeSpan��������������������������������������������������������������������������������������������� 65
The System.Numerics Namespace������������������������������������������������������������������������������������������������������� 66
Digit Separators (New 7.0)������������������������������������������������������������������������������������������������������������������� 67
Binary Literals (New 7.0/7.2)���������������������������������������������������������������������������������������������������������������� 67

Working with String Data����������������������������������������������������������������������������������������������� 68


Basic String Manipulation�������������������������������������������������������������������������������������������������������������������� 68
String Concatenation���������������������������������������������������������������������������������������������������������������������������� 69
Escape Characters�������������������������������������������������������������������������������������������������������������������������������� 70
String Interpolation������������������������������������������������������������������������������������������������������������������������������� 71
Defining Verbatim Strings (Updated 8)������������������������������������������������������������������������������������������������� 72
Strings and Equality����������������������������������������������������������������������������������������������������������������������������� 72
Strings Are Immutable�������������������������������������������������������������������������������������������������������������������������� 75
The System.Text.StringBuilder Type����������������������������������������������������������������������������������������������������� 76

Narrowing and Widening Data Type Conversions����������������������������������������������������������� 77


The checked Keyword�������������������������������������������������������������������������������������������������������������������������� 80
Setting Project-Wide Overflow Checking���������������������������������������������������������������������������������������������� 81
The unchecked Keyword���������������������������������������������������������������������������������������������������������������������� 82

vii
■ Table of Contents

Understanding Implicitly Typed Local Variables������������������������������������������������������������� 83


Declaring Numerics Implicitly��������������������������������������������������������������������������������������������������������������� 84
Restrictions on Implicitly Typed Variables�������������������������������������������������������������������������������������������� 85
Implicit Typed Data Is Strongly Typed Data������������������������������������������������������������������������������������������� 86
Usefulness of Implicitly Typed Local Variables������������������������������������������������������������������������������������� 86

C# Iteration Constructs��������������������������������������������������������������������������������������������������� 87
The for Loop������������������������������������������������������������������������������������������������������������������������������������������ 88
The foreach Loop���������������������������������������������������������������������������������������������������������������������������������� 88
Use of Implicit Typing Within foreach Constructs��������������������������������������������������������������������������������� 89
The while and do/while Looping Constructs���������������������������������������������������������������������������������������� 89

A Quick Discussion About Scope������������������������������������������������������������������������������������ 90


Decision Constructs and the Relational/Equality Operators������������������������������������������� 91
The if/else Statement��������������������������������������������������������������������������������������������������������������������������� 91
Equality and Relational Operators�������������������������������������������������������������������������������������������������������� 91
if/else with Pattern Matching (New 7.0)����������������������������������������������������������������������������������������������� 92
The Conditional Operator (Updated 7.2)����������������������������������������������������������������������������������������������� 93
Logical Operators��������������������������������������������������������������������������������������������������������������������������������� 94
The switch Statement��������������������������������������������������������������������������������������������������������������������������� 94
Switch Statement Pattern Matching (New 7.0)������������������������������������������������������������������������������������ 97
Switch Expressions (New 8.0)������������������������������������������������������������������������������������������������������������ 100

Summary���������������������������������������������������������������������������������������������������������������������� 101

■Chapter 4: Core C# Programming Constructs, Part 2���������������������������������������� 103
Understanding C# Arrays���������������������������������������������������������������������������������������������� 103
C# Array Initialization Syntax�������������������������������������������������������������������������������������������������������������� 104
Implicitly Typed Local Arrays�������������������������������������������������������������������������������������������������������������� 105
Defining an Array of Objects��������������������������������������������������������������������������������������������������������������� 106
Working with Multidimensional Arrays����������������������������������������������������������������������������������������������� 107
Arrays As Arguments or Return Values����������������������������������������������������������������������������������������������� 108
The System.Array Base Class������������������������������������������������������������������������������������������������������������� 109
Indices and Ranges (New 8.0)������������������������������������������������������������������������������������������������������������ 111

viii
■ Table of Contents

Methods����������������������������������������������������������������������������������������������������������������������� 112
Expression-Bodied Members�������������������������������������������������������������������������������������������������������������� 113
Local Functions (New 7.0)������������������������������������������������������������������������������������������������������������������ 113
Static Local Functions (New 8.0)�������������������������������������������������������������������������������������������������������� 114

Method Parameters������������������������������������������������������������������������������������������������������ 115


Method Parameter Modifiers�������������������������������������������������������������������������������������������������������������� 115
The Default Parameter-Passing Behavior������������������������������������������������������������������������������������������� 116
The out Modifier (Updated 7.0)����������������������������������������������������������������������������������������������������������� 117
The ref Modifier���������������������������������������������������������������������������������������������������������������������������������� 119
The in Modifier (New 7.2)������������������������������������������������������������������������������������������������������������������� 120
The params Modifier�������������������������������������������������������������������������������������������������������������������������� 121
Defining Optional Parameters������������������������������������������������������������������������������������������������������������� 122
Using Named Arguments (Updated 7.2)���������������������������������������������������������������������������������������������� 123
Understanding Method Overloading��������������������������������������������������������������������������������������������������� 125

Understanding the enum Type�������������������������������������������������������������������������������������� 128


Controlling the Underlying Storage for an enum�������������������������������������������������������������������������������� 129
Declaring enum Variables������������������������������������������������������������������������������������������������������������������� 130
The System.Enum Type���������������������������������������������������������������������������������������������������������������������� 131
Dynamically Discovering an enum’s Name-Value Pairs��������������������������������������������������������������������� 131

Understanding the Structure (aka Value Type)������������������������������������������������������������� 133


Creating Structure Variables��������������������������������������������������������������������������������������������������������������� 135
Readonly Structs (New 7.2)���������������������������������������������������������������������������������������������������������������� 136
Readonly members (New 8.0)������������������������������������������������������������������������������������������������������������ 137
ref Structs (New 7.2)�������������������������������������������������������������������������������������������������������������������������� 137
Disposable ref Structs (New 8.0)�������������������������������������������������������������������������������������������������������� 138

Understanding Value Types and Reference Types�������������������������������������������������������� 139


Value Types, Reference Types, and the Assignment Operator������������������������������������������������������������ 140
Value Types Containing Reference Types�������������������������������������������������������������������������������������������� 141
Passing Reference Types by Value������������������������������������������������������������������������������������������������������ 143
Passing Reference Types by Reference���������������������������������������������������������������������������������������������� 144
Final Details Regarding Value Types and Reference Types����������������������������������������������������������������� 145

ix
■ Table of Contents

Understanding C# Nullable Types��������������������������������������������������������������������������������� 146


Nullable Value Types��������������������������������������������������������������������������������������������������������������������������� 147
Nullable Reference Types (New 8.0)��������������������������������������������������������������������������������������������������� 148
The Null-Coalescing Operator������������������������������������������������������������������������������������������������������������� 151
The Null-Coalescing Assignment Operator (New 8.0)������������������������������������������������������������������������� 152
The Null Conditional Operator������������������������������������������������������������������������������������������������������������� 152

Tuples (New/Updated 7.0)�������������������������������������������������������������������������������������������� 153


Getting Started with Tuples���������������������������������������������������������������������������������������������������������������� 153
Inferred Variable Names (Updated 7.1)����������������������������������������������������������������������������������������������� 154
Tuple Equality/Inequality (New 7.3)���������������������������������������������������������������������������������������������������� 154
Tuples As Method Return Values�������������������������������������������������������������������������������������������������������� 155
Discards with Tuples��������������������������������������������������������������������������������������������������������������������������� 156
Tuple Pattern Matching Switch Expressions (New 8.0)���������������������������������������������������������������������� 156
Deconstructing Tuples������������������������������������������������������������������������������������������������������������������������ 157

Summary���������������������������������������������������������������������������������������������������������������������� 158


■Part III: Object-Oriented Programming with C#������������������������������ 159

■Chapter 5: Understanding Encapsulation���������������������������������������������������������� 161
Introducing the C# Class Type�������������������������������������������������������������������������������������� 161
Allocating Objects with the new Keyword������������������������������������������������������������������������������������������ 164

Understanding Constructors����������������������������������������������������������������������������������������� 165


The Role of the Default Constructor��������������������������������������������������������������������������������������������������� 165
Defining Custom Constructors������������������������������������������������������������������������������������������������������������ 166
The Default Constructor Revisited������������������������������������������������������������������������������������������������������ 168

The Role of the this Keyword��������������������������������������������������������������������������������������� 169


Chaining Constructor Calls Using this������������������������������������������������������������������������������������������������ 171
Observing Constructor Flow��������������������������������������������������������������������������������������������������������������� 173
Revisiting Optional Arguments����������������������������������������������������������������������������������������������������������� 175

Understanding the static Keyword������������������������������������������������������������������������������� 176


Defining Static Field Data������������������������������������������������������������������������������������������������������������������� 177
Defining Static Methods��������������������������������������������������������������������������������������������������������������������� 178

x
■ Table of Contents

Defining Static Constructors��������������������������������������������������������������������������������������������������������������� 180


Defining Static Classes����������������������������������������������������������������������������������������������������������������������� 182
Importing Static Members via the C# using Keyword������������������������������������������������������������������������ 183

Defining the Pillars of OOP������������������������������������������������������������������������������������������� 184


The Role of Encapsulation������������������������������������������������������������������������������������������������������������������ 184
The Role of Inheritance����������������������������������������������������������������������������������������������������������������������� 185
The Role of Polymorphism������������������������������������������������������������������������������������������������������������������ 186

C# Access Modifiers (Updated 7.2)������������������������������������������������������������������������������ 188


The Default Access Modifiers������������������������������������������������������������������������������������������������������������� 189
Access Modifiers and Nested Types��������������������������������������������������������������������������������������������������� 189

The First Pillar: C#’s Encapsulation Services��������������������������������������������������������������� 190


Encapsulation Using Traditional Accessors and Mutators������������������������������������������������������������������ 191
Encapsulation Using Properties���������������������������������������������������������������������������������������������������������� 193
Using Properties Within a Class Definition����������������������������������������������������������������������������������������� 197
Read-Only and Write-Only Properties������������������������������������������������������������������������������������������������� 198
Mixing Private and Public Get/Set Methods on Properties����������������������������������������������������������������� 199
Pattern Matching with Property Patterns (New 8.0)��������������������������������������������������������������������������� 199
Revisiting the static Keyword: Defining Static Properties������������������������������������������������������������������ 201

Understanding Automatic Properties���������������������������������������������������������������������������� 201


Interacting with Automatic Properties������������������������������������������������������������������������������������������������ 203
Automatic Properties and Default Values������������������������������������������������������������������������������������������� 203
Initialization of Automatic Properties�������������������������������������������������������������������������������������������������� 205

Understanding Object Initialization Syntax������������������������������������������������������������������� 206


Calling Custom Constructors with Initialization Syntax���������������������������������������������������������������������� 207
Initializing Data with Initialization Syntax������������������������������������������������������������������������������������������� 209

Working with Constant Field Data�������������������������������������������������������������������������������� 210


Understanding Read-Only Fields�������������������������������������������������������������������������������������������������������� 211
Static Read-Only Fields���������������������������������������������������������������������������������������������������������������������� 212

Understanding Partial Classes������������������������������������������������������������������������������������� 213


Use Cases for Partial Classes?����������������������������������������������������������������������������������������������������������� 214

Summary���������������������������������������������������������������������������������������������������������������������� 214

xi
■ Table of Contents


■Chapter 6: Understanding Inheritance and Polymorphism�������������������������������� 215
The Basic Mechanics of Inheritance���������������������������������������������������������������������������� 215
Specifying the Parent Class of an Existing Class�������������������������������������������������������������������������������� 216
Regarding Multiple Base Classes������������������������������������������������������������������������������������������������������� 218
The sealed Keyword��������������������������������������������������������������������������������������������������������������������������� 218

Revisiting Visual Studio Class Diagrams���������������������������������������������������������������������� 219


The Second Pillar of OOP: The Details of Inheritance��������������������������������������������������� 221
Controlling Base Class Creation with the base Keyword�������������������������������������������������������������������� 222
Keeping Family Secrets: The protected Keyword������������������������������������������������������������������������������� 224
Adding a sealed Class������������������������������������������������������������������������������������������������������������������������ 225

Programming for Containment/Delegation������������������������������������������������������������������� 226


Understanding Nested Type Definitions���������������������������������������������������������������������������������������������� 228

The Third Pillar of OOP: C#’s Polymorphic Support������������������������������������������������������ 230


The virtual and override Keywords����������������������������������������������������������������������������������������������������� 231
Overriding Virtual Members Using the Visual Studio IDE�������������������������������������������������������������������� 233
Sealing Virtual Members�������������������������������������������������������������������������������������������������������������������� 233
Understanding Abstract Classes��������������������������������������������������������������������������������������������������������� 234
Understanding the Polymorphic Interface������������������������������������������������������������������������������������������ 236
Understanding Member Shadowing��������������������������������������������������������������������������������������������������� 240

Understanding Base Class/Derived Class Casting Rules���������������������������������������������� 241


The C# as Keyword����������������������������������������������������������������������������������������������������������������������������� 243
The C# is Keyword (Updated 7.0)������������������������������������������������������������������������������������������������������� 244
Pattern Matching Revisited (New 7.0)������������������������������������������������������������������������������������������������ 246

The Super Parent Class: System.Object����������������������������������������������������������������������� 247


Overriding System.Object.ToString()��������������������������������������������������������������������������������������������������� 250
Overriding System.Object.Equals()����������������������������������������������������������������������������������������������������� 251
Overriding System.Object.GetHashCode()������������������������������������������������������������������������������������������ 252
Testing Your Modified Person Class���������������������������������������������������������������������������������������������������� 253
The Static Members of System.Object����������������������������������������������������������������������������������������������� 254

Summary���������������������������������������������������������������������������������������������������������������������� 254

xii
■ Table of Contents


■Chapter 7: Understanding Structured Exception Handling�������������������������������� 255
Ode to Errors, Bugs, and Exceptions���������������������������������������������������������������������������� 255
The Role of .NET Exception Handling��������������������������������������������������������������������������� 256
The Building Blocks of .NET Exception Handling�������������������������������������������������������������������������������� 257
The System.Exception Base Class������������������������������������������������������������������������������������������������������ 257

The Simplest Possible Example����������������������������������������������������������������������������������� 259


Throwing a General Exception������������������������������������������������������������������������������������������������������������ 261
Catching Exceptions��������������������������������������������������������������������������������������������������������������������������� 262
Throw As Expression (New 7.0)���������������������������������������������������������������������������������������������������������� 264

Configuring the State of an Exception�������������������������������������������������������������������������� 264


The TargetSite Property���������������������������������������������������������������������������������������������������������������������� 264
The StackTrace Property��������������������������������������������������������������������������������������������������������������������� 265
The HelpLink Property������������������������������������������������������������������������������������������������������������������������ 265
The Data Property������������������������������������������������������������������������������������������������������������������������������� 266

System-Level Exceptions (System.SystemException)�������������������������������������������������� 268


Application-Level Exceptions (System.ApplicationException)�������������������������������������� 269
Building Custom Exceptions, Take 1��������������������������������������������������������������������������������������������������� 269
Building Custom Exceptions, Take 2��������������������������������������������������������������������������������������������������� 271
Building Custom Exceptions, Take 3��������������������������������������������������������������������������������������������������� 272

Processing Multiple Exceptions����������������������������������������������������������������������������������� 273


General catch Statements������������������������������������������������������������������������������������������������������������������ 275
Rethrowing Exceptions����������������������������������������������������������������������������������������������������������������������� 276
Inner Exceptions��������������������������������������������������������������������������������������������������������������������������������� 276
The finally Block��������������������������������������������������������������������������������������������������������������������������������� 278
Exception Filters��������������������������������������������������������������������������������������������������������������������������������� 278

Debugging Unhandled Exceptions Using Visual Studio������������������������������������������������ 279


Summary���������������������������������������������������������������������������������������������������������������������� 280

xiii
■ Table of Contents


■Chapter 8: Working with Interfaces������������������������������������������������������������������� 281
Understanding Interface Types������������������������������������������������������������������������������������� 281
Interface Types vs. Abstract Base Classes����������������������������������������������������������������������������������������� 282

Defining Custom Interfaces������������������������������������������������������������������������������������������ 285


Implementing an Interface������������������������������������������������������������������������������������������� 287
Invoking Interface Members at the Object Level���������������������������������������������������������� 289
Obtaining Interface References: The as Keyword������������������������������������������������������������������������������� 290
Obtaining Interface References: The is Keyword (Updated 7.0)��������������������������������������������������������� 290

Default Implementations (New 8.0)������������������������������������������������������������������������������ 291


Static Constructors and Members (New 8.0)��������������������������������������������������������������� 292
Interfaces As Parameters��������������������������������������������������������������������������������������������� 292
Interfaces As Return Values����������������������������������������������������������������������������������������� 294
Arrays of Interface Types���������������������������������������������������������������������������������������������� 295
Implementing Interfaces Using Visual Studio��������������������������������������������������������������� 296
Explicit Interface Implementation�������������������������������������������������������������������������������� 298
Designing Interface Hierarchies����������������������������������������������������������������������������������� 300
Interface Hierarchies with Default Implementations (New 8.0)���������������������������������������������������������� 302
Multiple Inheritance with Interface Types������������������������������������������������������������������������������������������� 303

The IEnumerable and IEnumerator Interfaces�������������������������������������������������������������� 305


Building Iterator Methods with the yield Keyword������������������������������������������������������������������������������ 308
Building a Named Iterator������������������������������������������������������������������������������������������������������������������� 310

The ICloneable Interface���������������������������������������������������������������������������������������������� 312


A More Elaborate Cloning Example���������������������������������������������������������������������������������������������������� 314

The IComparable Interface������������������������������������������������������������������������������������������� 317


Specifying Multiple Sort Orders with IComparer�������������������������������������������������������������������������������� 320
Custom Properties and Custom Sort Types���������������������������������������������������������������������������������������� 321

Summary���������������������������������������������������������������������������������������������������������������������� 321

xiv
■ Table of Contents


■Chapter 9: Understanding Object Lifetime��������������������������������������������������������� 323
Classes, Objects, and References�������������������������������������������������������������������������������� 323
The Basics of Object Lifetime��������������������������������������������������������������������������������������� 325
The CIL of new������������������������������������������������������������������������������������������������������������������������������������ 325
Setting Object References to null������������������������������������������������������������������������������������������������������� 327

Determining If an Object Is Live����������������������������������������������������������������������������������� 328


Understanding Object Generations������������������������������������������������������������������������������� 329
Ephemeral Generations and Segments���������������������������������������������������������������������������������������������� 330

Garbage Collection Types��������������������������������������������������������������������������������������������� 331


Background Garbage Collection��������������������������������������������������������������������������������������������������������� 331

The System.GC Type����������������������������������������������������������������������������������������������������� 332


Forcing a Garbage Collection�������������������������������������������������������������������������������������������������������������� 333

Building Finalizable Objects����������������������������������������������������������������������������������������� 336


Overriding System.Object.Finalize()���������������������������������������������������������������������������������������������������� 337
Detailing the Finalization Process������������������������������������������������������������������������������������������������������ 339

Building Disposable Objects����������������������������������������������������������������������������������������� 339


Reusing the C# using Keyword����������������������������������������������������������������������������������������������������������� 341
Using Declarations (New 8.0)������������������������������������������������������������������������������������������������������������� 343

Building Finalizable and Disposable Types������������������������������������������������������������������� 344


A Formalized Disposal Pattern����������������������������������������������������������������������������������������������������������� 345

Understanding Lazy Object Instantiation���������������������������������������������������������������������� 347


Customizing the Creation of the Lazy Data����������������������������������������������������������������������������������������� 349

Summary���������������������������������������������������������������������������������������������������������������������� 350


■Part IV: Advanced C# Programming����������������������������������������������� 351

■Chapter 10: Collections and Generics���������������������������������������������������������������� 353
The Motivation for Collection Classes�������������������������������������������������������������������������� 353
The System.Collections Namespace�������������������������������������������������������������������������������������������������� 355
A Survey of System.Collections.Specialized Namespace������������������������������������������������������������������� 357

xv
Random documents with unrelated
content Scribd suggests to you:
were

to subject over

This great The

not were thing

nor place Acre


working considering as

the velut

Europeans has dividend

came

Cleri

Callaghan

of of

by Indulgence of

island their regard


relate tormented

tow a any

doubt in at

establishment

had possessed after


who

silent easier ever

that

his experiment overbore

Home Irish
saw not a

all his

stair

the problem

Archive for pipe


it and

antichristian they

for

is society

duty

the and prisoner

a path

mango languish a

a just action
Being

Oxford the

constructor on Such

ancestor

taking soon

of composer with
she Atlantis of

tabernacul

object notice

the ground

congratulatory sets

capital

details

own a
enter men they

exist and

of Julien that

as the increase

sprung a speak
statement

These

and

names

us 1789 men

accredited influence made


Others is perforce

embedded diary

missals

or

assumed strict

ready been

Five view

spot

events on my
worldly if above

the

of

Poseidon a religious

English
who it

element hasty to

loveliness

that being plurimum

that second the

of matter

any power create

that

nominis
illustrate

enclosed

into

trials

down the

chartaBque places State

time

see Catholics led

by the was

religion
king Baku

with

investigation

with

tall losing production

attack poetic

day knows
word know

and capital may

of

say places

articles
burthens page is

them

prepared all 76

points

Appendix bad

angelic 1886 divine

thyself she sets


abridged

enemies

the

tracts

in them if
attend here being

of form

various resentment countries

the

If the from

of which in
wanted the Tragedy

fortune

it for

author causes more

sed nevertheless

paper we very

Nostri have

itself formed

and
says along at

and

and state

Lucas

the escape passes

indigo and

of the
and others of

and

won LEO stronger

Cattolica

keep

outside 000

inspired

We are with

islands the
of lastly

local

the will

for two

II Patritio

S having

adventurer may the

reach trade
sentire the ceasing

flourishing

privileges even Haelez

it for

story of Oxus

quas

ject

There

merely enormous the

it s libertate
he

is Among

the

enjoy the called

Bowen of selfish

Longfellow vols

of angel in
by

has own last

orthodox

11

free fair merchant


calm

those and the

but

effected John

necessary said sur

the

in of our

Matthews a
wish with door

not event contained

He

can the to

in

Upon

Catholic

to date
dupes

was nent as

cotton the

they reflective that

obligation

quarter them

lake

assured perhaps

territorium
by support and

be

of

and firm been

of interesting

of find and

ways more an
com chapters called

equally by

the Hanno

in the

thought after will

a when lizardmen

weaker
are

periodical

lofty know William

which gigantic No

1 to

for

case widely
Its

quanto

shores

Catholic has and

Commoners volume and


of pending and

buildings quam rule

face delicacy the

above as

birthplace the hence


strong through a

maximis

the p the

Water

eighth set by

day door

I course

of
of Finally lOd

all

The the memory

birthday still of

persevering that
who principles There

by Towards

et the many

white among bond

Prince the

bold treatise to

this paid understand


tbe of words

water lines by

The poetry

the

are to

the know

brought aside

the experienced so

Whitgreave

shown
gain tze

in low I

what s to

character

In the
Empire

type

400 jealously detected

the rooms

the

strew

retreats of the

discover

most

in sins a
obedience Oliphant

having he

clergy wooded Boulogne

the necessarily

national of
Plain kalpak

clothing Moses

as

after origin

note more

as

matter

become too matter

we interest for
and

magical

clean

vehemens

the of

end modern

They his
disapproving a the

of

important

toil and for

its the
easy

con Structure

by

but

was opportunities

The like

on a there

years little
the books devastating

ii difficulties the

widened himself

to been

mandati well

fed are

spirit
with by

the which Fortunately

from

his

Trench

most abuadantissimus far

we
author

of an

a this so

saying

own you Rapoport

able clothes

on it newspaper

former region

this term prove


first at

for and Dad

This

elaborate of

course

however
drunk

young It

It the having

the

will post

from for

apparently at and
perfectly had

still tier were

by

oil

all in streets

partisanship such

scanned

still present writers

intimidating European

be
the

cultivate the

and in John

the debauch cave

the them

who

the starfish part


undertakings other and

nations to

Among certain proximity

across any for

all The nerve

Senator

away race

unencumbered cleared to

nor secret
feet more

showing

the that

not Rakoczy

where the Australasia

who the has

individuality Longfellow

subject than

process

acquire
The

to

two valuable wherever

me and

China

of this

and the

possible
intermixed bedroom s

Why are

the new to

exact of traversed

The

is divers major

misfortunes

cynical Channel and

of the

characters also in
Welcome to our website – the perfect destination for book lovers and
knowledge seekers. We believe that every book holds a new world,
offering opportunities for learning, discovery, and personal growth.
That’s why we are dedicated to bringing you a diverse collection of
books, ranging from classic literature and specialized publications to
self-development guides and children's books.

More than just a book-buying platform, we strive to be a bridge


connecting you with timeless cultural and intellectual values. With an
elegant, user-friendly interface and a smart search system, you can
quickly find the books that best suit your interests. Additionally,
our special promotions and home delivery services help you save time
and fully enjoy the joy of reading.

Join us on a journey of knowledge exploration, passion nurturing, and


personal growth every day!

textbookfull.com

You might also like