Cover....1
Title Page....7
Copyright Page....8
About The Authors....9
About The Technical Editor....11
Acknowledgments....13
Contents....15
Introduction....29
Who This Book is For....29
What This Book Covers....30
How This Book is Structured....31
The C# Language (Chapters 1–13)....31
Data Access (Chapters 14–17)....32
Additional Techniques (Chapters 18–21)....32
What You Need To Use This Book....33
Conventions....33
Source Code....34
ERRATA....35
Part I The C# Language....37
Chapter 1: Introducing C#....39
What Is .NET?....39
.NET Framework, .NET Standard, and .NET Core....40
Writing Programs Using .NET....41
CIL and JIT....41
Assemblies....42
Managed Code....43
Garbage Collection....43
Fitting It Together....43
Linking....44
What Is C#?....44
Applications You Can Write with C#....45
C# in This Book....46
Visual Studio....46
Visual Studio Products....46
Solutions....47
Chapter 2: Writing a C# Program....49
The Visual Studio Development Environment....50
Console Applications....54
The Solution Explorer....57
The Properties Window....58
The Error List Window....59
Desktop Applications....59
Chapter 3: Variables and Expressions....65
Basic C# Syntax....66
Basic C# Console Application Structure....69
Variables....70
Simple Types....70
Variable Naming....75
Literal Values....75
Binary Literals and Digit Separators....76
String Literals....77
Expressions....78
Mathematical Operators....79
Assignment Operators....84
Operator Precedence....85
Namespaces....86
Chapter 4: Flow Control....91
Boolean Logic....92
Boolean Bitwise and Assignment Operators....94
Operator Precedence Updated....96
Branching ....96
The Ternary Operator....97
The if Statement....97
Checking More Conditions Using if Statements....100
The switch Statement....101
Looping....104
do Loops....104
while Loops....107
for Loops....109
Interrupting Loops....110
Infinite Loops....111
Chapter 5: More about Variables....115
Type Conversion....116
Implicit Conversions....116
Explicit Conversions....118
Explicit Conversions Using the Convert Commands....120
Complex Variable Types....123
Enumerations....123
Defining Enumerations....124
Structs....127
Defining Structs....128
Arrays....130
Declaring Arrays....131
foreach Loops....134
Pattern Matching with switch case Expression....134
Multidimensional Arrays....138
Arrays of Arrays....140
String Manipulation....141
Chapter 6: Functions....149
Defining and Using Functions....150
Return Values....153
Parameters....154
Parameter Matching....157
Parameter Arrays....157
Reference and Value Parameters....159
Out Parameters....161
Tuples....162
Variable Scope....164
Variable Scope in Other Structures....167
Parameters and Return Values versus Global Data....168
Local Functions....170
The Main() Function....171
Struct Functions....173
Overloading Functions....174
Using Delegates....176
Chapter 7: Debugging and Error Handling....181
Debugging in Visual Studio....182
Debugging in Nonbreak (Normal) Mode....183
Outputting Debugging Information....184
Tracepoints....188
Diagnostics Output versus Tracepoints....190
Debugging in Break Mode....191
Entering Break Mode....191
Monitoring Variable Content....194
Stepping through Code....197
Immediate and Command Windows....198
The Call Stack Window....199
Error Handling....199
try. . .catch. . .finally....200
Throw Expressions....208
Listing and Configuring Exceptions....208
Chapter 8: Introduction to Object-Oriented Programming....211
What Is Object-Oriented Programming?....212
What Is an Object?....213
Properties and Fields....214
Methods....215
Everything’s an Object....216
The Life Cycle of an Object....216
Constructors....216
Destructors....217
Static and Instance Class Members....217
Static Constructors....217
Static Classes....218
OOP Techniques....218
Interfaces....218
Disposable Objects....220
Inheritance....220
Polymorphism....223
Interface Polymorphism....224
Relationships between Objects....225
Containment....225
Collections....226
Operator Overloading....227
Events....227
Reference Types versus Value Types....228
OOP in Desktop Applications....228
Chapter 9: Defining Classes....235
Class Definitions in C#....236
Interface Definitions....238
System.Object....241
Constructors and Destructors....243
Constructor Execution Sequence....245
OOP Tools in Visual Studio....248
The Class View Window....248
The Object Browser....250
Adding Classes....252
Class Diagrams....253
Class Library Projects....255
Interfaces versus Abstract Classes....259
Struct Types....261
Shallow Copying versus Deep Copying....263
Chapter 10: Defining Class Members....267
Member Definitions....268
Defining Fields....268
Defining Methods....269
Defining Properties....270
Tuple Deconstruction....275
Refactoring Members....276
Automatic Properties....277
Additional Class Member Topics....278
Hiding Base Class Methods....278
Calling Overridden or Hidden Base Class Methods....280
The this Keyword....280
Using Nested Type Definitions....281
Interface Implementation....283
Implementing Interfaces in Classes....284
Explicit Interface Member Implementation....285
Additional Property Accessors....285
Partial Class Definitions....286
Partial Method Definitions....287
Example Application....288
Planning the Application....289
The Card Class....289
The Deck Class....289
Writing the Class Library....289
Adding the Suit and Rank Enumerations....290
Adding the Card Class....292
Adding the Deck Class....294
A Client Application for the Class Library....297
The Call Hierarchy Window....298
Chapter 11: Collections, Comparisons, and Conversions....301
Collections....302
Using Collections....303
Defining Collections....308
Indexers....309
Adding a CardCollection to CardLib....311
Keyed Collections and IDictionary....314
Iterators....315
Iterators and Collections....320
Deep Copying....321
Adding Deep Copying to CardLib....322
Comparisons....324
Type Comparisons....324
Boxing and Unboxing....325
The is Operator....326
Pattern Matching with the is Operator Pattern Expression....329
Value Comparisons....330
Operator Overloading....330
Adding Operator Overloads to CardLib....335
The IComparable and IComparer Interfaces....340
Sorting Collections....342
Conversions....345
Overloading Conversion Operators....346
The as Operator....347
Chapter 12: Generics....351
What Are Generics?....352
Using Generics....353
Nullable Types....353
Operators and Nullable Types....354
The ?? Operator....355
The ?. Operator....356
Working with Nullable Types....357
The System.Collections.Generic Namespace....361
List....362
Sorting and Searching Generic Lists....363
Dictionary....369
Modifying CardLib to Use a Generic Collection Class ....370
Defining Generic Types....371
Defining Generic Classes....372
The default Keyword....374
Constraining Types....374
Inheriting from Generic Classes....380
Generic Operators....381
Generic Structs....382
Defining Generic Interfaces....382
Defining Generic Methods....382
Defining Generic Delegates....384
Variance....384
Covariance....385
Contravariance....386
Chapter 13: Additional C# Techniques....391
The :: Operator and the Global Namespace Qualifier....392
Custom Exceptions....393
Adding Custom Exceptions to CardLib....394
Events....395
What Is an Event?....395
Handling Events....397
Defining Events....399
Multipurpose Event Handlers....403
The EventHandler and Generic EventHandler Types....406
Return Values and Event Handlers....406
Anonymous Methods....406
Expanding and Using CardLib....407
Attributes....416
Reading Attributes....416
Creating Attributes....417
Initializers....418
Object Initializers....419
Collection Initializers....421
Type Inference....424
Anonymous Types....426
Dynamic Lookup....430
The dynamic Type....431
Advanced Method Parameters....435
Optional Parameters....435
Optional Parameter Values....436
The OptionalAttribute Attribute....436
Optional Parameter Order....437
Named Parameters....437
Lambda Expressions....441
Anonymous Methods Recap....441
Lambda Expressions for Anonymous Methods....443
Lambda Expression Parameters....446
Lambda Expression Statement Bodies....447
Lambda Expressions as Delegates and Expression Trees....448
Lambda Expressions and Collections....449
Part II Data Access....457
Chapter 14: Files....459
File Classes for Input and Output....460
The File and Directory Classes....460
The FileInfo Class....462
The DirectoryInfo Class....464
Path Names and Relative Paths....464
Streams....465
Classes for Using Streams....465
The FileStream Object....465
File Position....466
Reading Data....468
Writing Data....470
The StreamWriter Object....472
The StreamReader Object....475
Reading Data....476
Asynchronous File Access....477
Reading and Writing Compressed Files....478
Monitoring the File System....481
Chapter 15: XML AND JSON....489
XML Basics....490
JSON Basics....491
XML Schemas....491
XML Document Object Model....494
The XmlDocument Class....494
The XmlElement Class....495
Changing the Values of Nodes....499
Inserting New Nodes....501
Deleting Nodes....503
Selecting Nodes....505
Searching XML with XPath....505
JSON Serialization and Deserialization....509
Chapter 16: LINQ....515
LINQ to XML....516
LINQ to XML Functional Constructors....516
Working with XML Fragments....519
LINQ Providers....522
LINQ Query Syntax....522
Declaring a Variable for Results Using the var Keyword....524
Specifying the Data Source: from Clause....525
Specify Condition: where Clause....525
Selecting Items: select Clause....526
Finishing Up: Using the foreach Loop....526
Deferred Query Execution....526
LINQ Method Syntax....526
LINQ Extension Methods....527
Query Syntax versus Method Syntax....527
Lambda Expressions....528
Ordering Query Results....530
Understanding the orderby Clause....531
Querying a Large Data Set....532
Using Aggregate Operators....534
Using the Select Distinct Query....538
Ordering by Multiple Levels....540
Using Group Queries....542
Using Joins....544
Chapter 17: Databases....549
Using Databases....550
Entity Framework....550
Code- First versus Database- First....550
Migrations and Scaffolding....551
Install SQL Server Express LocalDB....551
A Code- First Database....552
Exploring Your Database....563
Navigating Database Relationships....566
Creating and Querying XML from an Existing Database....572
Part III Additional Techniques....579
Chapter 18: .NET and ASP.NET....581
Cross- Platform Basics and Key “Must Know” Terms ....583
What was .NET Standard? ....585
Shared Project, PCL, and .NET Standard....587
Building and Packaging a .NET Standard Library....589
Referencing and Targeting .NET....593
What was .NET Core?....594
Cross Platform....594
Open Source....595
Optimized for the Cloud....596
Performance....596
Modular Design....597
Self- Contained Deployment Model....598
Porting from .NET Framework to .NET....601
Identifying Third- Party Dependencies....603
Understanding Which Features Are Not Available....603
Upgrading the Current .NET Framework Target....603
Overview of Web Applications....604
Which ASP.NET to Use and Why....605
ASP.NET Web Forms....606
Server Controls....608
Input Validation....609
State Management....610
Authentication and Authorization....611
ASP.NET Web Site versus ASP.NET Web Applications....611
ASP.NET MVC/ASP.NET Core Web App MVC....613
ASP.NET Core Web API....615
ASP.NET Core Web App....616
IIS and Kestrel ....617
Blazor App and Razor Pages....618
Input Validation....620
State Management....621
Authentication and Authorization....622
Dependency Injection....622
Chapter 19: Basic Cloud Programming....627
The Cloud, Cloud Computing, and the Cloud Optimized Stack....628
Cloud Patterns and Best Practices....631
Using Microsoft Azure C# Libraries to Create a Storage Container....633
Creating an ASP.NET Core Web Application That Uses the Storage Container....643
Chapter 20: Basic Web API and WCF Programming....653
Creating an ASP.NET Core Web API....653
Consuming an ASP.NET Core Web API ....658
What Is REST?....665
What Is WCF?....666
WCF Concepts....667
WCF Communication Protocols....667
Addresses, Endpoints, and Bindings....668
Contracts....670
Message Patterns....670
Behaviors....671
Hosting....671
WCF Programming....671
The WCF Test Client....678
Defining WCF Service Contracts....680
Data Contracts....681
Service Contracts....681
Operation Contracts....682
Message Contracts....683
Fault Contracts....683
Self- Hosted WCF Services....688
Chapter 21: Basic Desktop Programming....699
XAML....700
Separation of Concerns....701
XAML in Action....701
Namespaces....702
Code- Behind Files....703
The Playground....703
WPF Controls....705
Properties....706
Dependency Properties ....709
Attached Properties....709
Events....710
Handling Events....711
Routed Events....712
Routed Commands....712
Control Types....715
Control Layout....715
Basic Layout Concepts....715
Stack Order....715
Alignment, Margins, Padding, and Dimensions....716
Border....716
Visual Debugging Tools....717
Layout Panels....718
Canvas....718
DockPanel....720
StackPanel....722
WrapPanel....723
Grid....724
The Game Client....727
The About Window....727
Designing the User Interface....728
The Image Control....728
The Label Control....728
The TextBlock Control....729
The Button Control....729
The Options Window....732
The TextBox Control....733
The CheckBox Control....734
The RadioButton Control....735
The ComboBox Control....736
The TabControl....737
Handling Events in the Options Window....741
Data Binding....743
The DataContext....744
Binding to Local Objects....744
Static Binding to External Objects....745
Dynamic Binding to External Objects....746
Starting a Game with the ListBox Control....748
Creating and Styling Controls....752
Styles....752
Templates....753
Triggers....755
Animations....756
WPF User Controls....757
Implementing Dependency Properties....758
The Main Window....772
The Menu Control....772
Routed Commands with Menus....772
Putting It All Together....777
Refactoring the Domain Model....777
The View Model....784
Completing the Game....793
Appendix: Exercise Solutions....805
Index....845
EULA....862
THE C# LANGUAGE WAS UNVEILED TO THE WORLD when Microsoft announced the first version of its .NET Framework in 2002. Since then, its popularity has rocketed, and it has arguably become the language of choice for desktop, web, cloud, and cross-platform developers who use .NET. Part of the appeal of C# comes from its clear syntax, which derives from C/C++ but simplifies some things that have previously discouraged some programmers. Despite this simplification, C# has retained the power of C++, and there is no reason now not to move into C#. The language is not difficult, and it is a great one with which to learn elementary programming techniques. This ease of learning combined with the capabilities of the .NET Framework make C# an excellent way to start your programming career.
The latest release of C# is C# 9 (included with.NET 5.0 and .NET Framework 4.8), which builds on the existing successes and adds even more attractive features. The latest releases of both Visual Studio and Visual Studio Code line of development tools also bring many tweaks and improvements to make your life easier and to dramatically increase your productivity.