+353-1-416-8900REST OF WORLD
+44-20-3973-8888REST OF WORLD
1-917-300-0470EAST COAST U.S
1-800-526-8630U.S. (TOLL FREE)

Beginning C# and .NET. 2021 Edition

  • Book

  • 864 Pages
  • September 2021
  • John Wiley and Sons Ltd
  • ID: 5840420

Get a running start to learning C# programming with this fun and easy-to-read guide 

As one of the most versatile and powerful programming languages around, you might think C# would be an intimidating language to learn. It doesn’t have to be! 

In Beginning C# and .NET: 2021 Edition, expert Microsoft programmer and engineer Benjamin Perkins and program manager Jon D. Reid walk you through the precise, step-by-step directions you’ll need to follow to become fluent in the C# language and .NET. 

Using the proven WROX method, you’ll discover how to understand and write simple expressions and functions, debug programs, work with classes and class members, work with Windows forms, program for the web, and access data. You’ll even learn about some of the new features included in the latest releases of C# and .NET, including data consumption, code simplification, and performance. 

The book also offers: 

  • Detailed discussions of programming basics, like variables, flow control, and object-oriented programming that assume no previous programming experience 
  • “Try it Out” sections to help you write useful programming code using the steps you’ve learned in the book 
  • Downloadable code examples from wrox.com 

Perfect for beginning-level programmers who are completely new to C#, Beginning C# and .NET: 2021 Edition is a must-have resource for anyone interested in learning programming and looking for a fun and intuitive place to start. 

Table of Contents

Introduction xxvii

Part I: The C# Language

Chapter 1: Introducing C# 3

What Is .NET? 3

.NET Framework, .NET Standard, and .NET Core 4

Writing Programs Using .NET 5

CIL and JIT 5

Assemblies 6

Managed Code 7

Garbage Collection 7

Fitting It Together 7

Linking 8

What Is C#? 8

Applications You Can Write with C# 9

C# in This Book 10

Visual Studio 10

Visual Studio Products 10

Solutions 11

Chapter 2: Writing a C# Program 13

The Visual Studio Development Environment 14

Console Applications 18

The Solution Explorer 21

The Properties Window 22

The Error List Window 23

Desktop Applications 23

Chapter 3: Variables and Expressions 29

Basic C# Syntax 30

Basic C# Console Application Structure 33

Variables 34

Simple Types 34

Variable Naming 39

Literal Values 39

Binary Literals and Digit Separators 40

String Literals 41

Expressions 42

Mathematical Operators 43

Assignment Operators 48

Operator Precedence 49

Namespaces 50

Chapter 4: Flow Control 55

Boolean Logic 56

Boolean Bitwise and Assignment Operators 58

Operator Precedence Updated 60

Branching 60

The Ternary Operator 61

The if Statement 61

Checking More Conditions Using if Statements 64

The switch Statement 65

Looping 68

do Loops 68

while Loops 71

for Loops 73

Interrupting Loops 74

Infinite Loops 75

Chapter 5: More About Variables 79

Type Conversion 80

Implicit Conversions 80

Explicit Conversions 82

Explicit Conversions Using the Convert Commands 84

Complex Variable Types 87

Enumerations 87

Defining Enumerations 88

Structs 91

Defining Structs 92

Arrays 94

Declaring Arrays 95

foreach Loops 98

Pattern Matching with switch case Expression 98

Multidimensional Arrays 102

Arrays of Arrays 104

String Manipulation 105

Chapter 6: Functions 113

Defining and Using Functions 114

Return Values 117

Parameters 118

Parameter Matching 121

Parameter Arrays 121

Reference and Value Parameters 123

Out Parameters 125

Tuples 126

Variable Scope 128

Variable Scope in Other Structures 131

Parameters and Return Values versus Global Data 132

Local Functions 134

The Main() Function 135

Struct Functions 137

Overloading Functions 138

Using Delegates 140

Chapter 7: Debugging and Error Handling 145

Debugging in Visual Studio 146

Debugging in Nonbreak (Normal) Mode 147

Outputting Debugging Information 148

Tracepoints 152

Diagnostics Output versus Tracepoints 154

Debugging in Break Mode 155

Entering Break Mode 155

Monitoring Variable Content 158

Stepping through Code 161

Immediate and Command Windows 162

The Call Stack Window 163

Error Handling 163

try catch finally 164

Throw Expressions 172

Listing and Configuring Exceptions 172

Chapter 8: Introduction to Object-oriented Programming 175

What Is Object-Oriented Programming? 176

What Is an Object? 177

Properties and Fields 178

Methods 179

Everything’s an Object 180

The Life Cycle of an Object 180

Constructors 180

Destructors 181

Static and Instance Class Members 181

Static Constructors 181

Static Classes 182

OOP Techniques 182

Interfaces 182

Disposable Objects 184

Inheritance 184

Polymorphism 187

Interface Polymorphism 188

Relationships between Objects 189

Containment 189

Collections 190

Operator Overloading 191

Events 191

Reference Types versus Value Types 192

OOP in Desktop Applications 192

Chapter 9: Defining Classes 199

Class Definitions in C# 200

Interface Definitions 202

System.Object 205

Constructors and Destructors 207

Constructor Execution Sequence 209

OOP Tools in Visual Studio 212

The Class View Window 212

The Object Browser 214

Adding Classes 216

Class Diagrams 217

Class Library Projects 219

Interfaces versus Abstract Classes 223

Struct Types 225

Shallow Copying versus Deep Copying 227

Chapter 10: Defining Class Members 231

Member Definitions 232

Defining Fields 232

Defining Methods 233

Defining Properties 234

Tuple Deconstruction 239

Refactoring Members 240

Automatic Properties 241

Additional Class Member Topics 242

Hiding Base Class Methods 242

Calling Overridden or Hidden Base Class Methods 244

The this Keyword 244

Using Nested Type Definitions 245

Interface Implementation 247

Implementing Interfaces in Classes 248

Explicit Interface Member Implementation 249

Additional Property Accessors 249

Partial Class Definitions 250

Partial Method Definitions 251

Example Application 252

Planning the Application 253

The Card Class 253

The Deck Class 253

Writing the Class Library 253

Adding the Suit and Rank Enumerations 254

Adding the Card Class 256

Adding the Deck Class 258

A Client Application for the Class Library 261

The Call Hierarchy Window 262

Chapter 11: Collections, Comparisons, and Conversions 265

Collections 266

Using Collections 267

Defining Collections 272

Indexers 273

Adding a CardCollection to CardLib 275

Keyed Collections and IDictionary 278

Iterators 279

Iterators and Collections 284

Deep Copying 285

Adding Deep Copying to CardLib 286

Comparisons 288

Type Comparisons 288

Boxing and Unboxing 289

The is Operator 290

Pattern Matching with the is Operator Pattern Expression 293

Value Comparisons 294

Operator Overloading 294

Adding Operator Overloads to CardLib 299

The IComparable and IComparer Interfaces 304

Sorting Collections 306

Conversions 309

Overloading Conversion Operators 310

The as Operator 311

Chapter 12: Generics 315

What Are Generics? 316

Using Generics 317

Nullable Types 317

Operators and Nullable Types 318

The ?? Operator 319

The ?. Operator 320

Working with Nullable Types 321

The System.Collections.Generic Namespace 325

List 326

Sorting and Searching Generic Lists 327

Dictionary 333

Modifying CardLib to Use a Generic Collection Class 334

Defining Generic Types 335

Defining Generic Classes 336

The default Keyword 338

Constraining Types 338

Inheriting from Generic Classes 344

Generic Operators 345

Generic Structs 346

Defining Generic Interfaces 346

Defining Generic Methods 346

Defining Generic Delegates 348

Variance 348

Covariance 349

Contravariance 350

Chapter 13: Additional C# Techniques 355

The :: Operator and the Global Namespace Qualifier 356

Custom Exceptions 357

Adding Custom Exceptions to CardLib 358

Events 359

What Is an Event? 359

Handling Events 361

Defining Events 363

Multipurpose Event Handlers 367

The EventHandler and Generic EventHandler Types 370

Return Values and Event Handlers 370

Anonymous Methods 370

Expanding and Using CardLib 371

Attributes 380

Reading Attributes 380

Creating Attributes 381

Initializers 382

Object Initializers 383

Collection Initializers 385

Type Inference 388

Anonymous Types 390

Dynamic Lookup 394

The dynamic Type 395

Advanced Method Parameters 399

Optional Parameters 399

Optional Parameter Values 400

The OptionalAttribute Attribute 400

Optional Parameter Order 401

Named Parameters 401

Lambda Expressions 405

Anonymous Methods Recap 405

Lambda Expressions for Anonymous Methods 407

Lambda Expression Parameters 410

Lambda Expression Statement Bodies 411

Lambda Expressions as Delegates and Expression Trees 412

Lambda Expressions and Collections 413

Part II: Data Access

Chapter 14: Files 423

File Classes for Input and Output 424

The File and Directory Classes 424

The FileInfo Class 426

The DirectoryInfo Class 428

Path Names and Relative Paths 428

Streams 429

Classes for Using Streams 429

The FileStream Object 429

File Position 430

Reading Data 432

Writing Data 434

The StreamWriter Object 436

The StreamReader Object 439

Reading Data 440

Asynchronous File Access 441

Reading and Writing Compressed Files 442

Monitoring the File System 445

Chapter 15: XML and JSON 453

XML Basics 454

JSON Basics 455

XML Schemas 455

XML Document Object Model 458

The XmlDocument Class 458

The XmlElement Class 459

Changing the Values of Nodes 463

Inserting New Nodes 465

Deleting Nodes 467

Selecting Nodes 469

Searching XML with XPath 469

JSON Serialization and Deserialization 473

Chapter 16: LINQ 479

LINQ to XML 480

LINQ to XML Functional Constructors 480

Working with XML Fragments 483

LINQ Providers 486

LINQ Query Syntax 486

Declaring a Variable for Results Using the var Keyword 488

Specifying the Data Source: from Clause 489

Specify Condition: where Clause 489

Selecting Items: select Clause 490

Finishing Up: Using the foreach Loop 490

Deferred Query Execution 490

LINQ Method Syntax 490

LINQ Extension Methods 491

Query Syntax versus Method Syntax 491

Lambda Expressions 492

Ordering Query Results 494

Understanding the orderby Clause 495

Querying a Large Data Set 496

Using Aggregate Operators 498

Using the Select Distinct Query 502

Ordering by Multiple Levels 504

Using Group Queries 506

Using Joins 508

Chapter 17: Databases 513

Using Databases 514

Entity Framework 514

Code- First versus Database- First 514

Migrations and Scaffolding 515

Install SQL Server Express LocalDB 515

A Code- First Database 516

Exploring Your Database 527

Navigating Database Relationships 530

Creating and Querying XML from an Existing Database 536

Part III: Additional Techniques

Chapter 18: .NET and ASP.NET 545

Cross- Platform Basics and Key “Must Know” Terms 547

What was .NET Standard? 549

Shared Project, PCL, and .NET Standard 551

Building and Packaging a .NET Standard Library 553

Referencing and Targeting .NET 557

What was .NET Core? 558

Cross Platform 558

Open Source 559

Optimized for the Cloud 560

Performance 560

Modular Design 561

Self- Contained Deployment Model 562

Porting from .NET Framework to .NET 565

Identifying Third- Party Dependencies 567

Understanding Which Features Are Not Available 567

Upgrading the Current .NET Framework Target 567

Overview of Web Applications 568

Which ASP.NET to Use and Why 569

ASP.NET Web Forms 570

Server Controls 572

Input Validation 573

State Management 574

Authentication and Authorization 575

ASP.NET Web Site versus ASP.NET Web Applications 575

ASP.NET MVC/ASP.NET Core Web App MVC 577

ASP.NET Core Web API 579

ASP.NET Core Web App 580

IIS and Kestrel 581

Blazor App and Razor Pages 582

Input Validation 584

State Management 585

Authentication and Authorization 586

Dependency Injection 586

Chapter 19: Basic Cloud Programming 591

The Cloud, Cloud Computing, and the Cloud Optimized Stack 592

Cloud Patterns and Best Practices 595

Using Microsoft Azure C# Libraries to Create a Storage Container 597

Creating an ASP.NET Core Web Application That Uses the Storage Container 607

Chapter 20: Basic Web API and WCF Programming 617

Creating an ASP.NET Core Web API 617

Consuming an ASP.NET Core Web API 622

What Is REST? 629

What Is WCF? 630

WCF Concepts 631

WCF Communication Protocols 631

Addresses, Endpoints, and Bindings 632

Contracts 634

Message Patterns 634

Behaviors 635

Hosting 635

WCF Programming 635

The WCF Test Client 642

Defining WCF Service Contracts 644

Data Contracts 645

Service Contracts 645

Operation Contracts 646

Message Contracts 647

Fault Contracts 647

Self- Hosted WCF Services 652

Chapter 21: Basic Desktop Programming 663

XAML 664

Separation of Concerns 665

XAML in Action 665

Namespaces 666

Code- Behind Files 667

The Playground 667

WPF Controls 669

Properties 670

Dependency Properties 673

Attached Properties 673

Events 674

Handling Events 675

Routed Events 676

Routed Commands 676

Control Types 679

Control Layout 679

Basic Layout Concepts 679

Stack Order 679

Alignment, Margins, Padding, and Dimensions 680

Border 680

Visual Debugging Tools 681

Layout Panels 682

Canvas 682

DockPanel 684

StackPanel 686

WrapPanel 687

Grid 688

The Game Client 691

The About Window 691

Designing the User Interface 692

The Image Control 692

The Label Control 692

The TextBlock Control 693

The Button Control 693

The Options Window 696

The TextBox Control 697

The CheckBox Control 698

The RadioButton Control 699

The ComboBox Control 700

The TabControl 701

Handling Events in the Options Window 705

Data Binding 707

The DataContext 708

Binding to Local Objects 708

Static Binding to External Objects 709

Dynamic Binding to External Objects 710

Starting a Game with the ListBox Control 712

Creating and Styling Controls 716

Styles 716

Templates 717

Triggers 719

Animations 720

WPF User Controls 721

Implementing Dependency Properties 722

The Main Window 736

The Menu Control 736

Routed Commands with Menus 736

Putting It All Together 741

Refactoring the Domain Model 741

The View Model 748

Completing the Game 757

Appendix: Exercise Solutions 769

Index 809

Authors

Benjamin Perkins Jon D. Reid