Beginning C# 6 Programming with Visual Studio 2015 - Benjamin Perkins - E-Book

Beginning C# 6 Programming with Visual Studio 2015 E-Book

Benjamin Perkins

4,7
32,99 €

-100%
Sammeln Sie Punkte in unserem Gutscheinprogramm und kaufen Sie E-Books und Hörbücher mit bis zu 100% Rabatt.
Mehr erfahren.
Beschreibung

Get started with Visual C# programming with this great beginner's guide Beginning C# 6 Programming with Visual Studio 2015 provides step-by-step directions for programming with C# in the .NET framework. Beginning with programming essentials, such as variables, flow control, and object-oriented programming, this authoritative text moves into more complicated topics, such as web and Windows programming and data access within both database and XML environments. After your introduction to each of the chapters, you are invited to apply your newfound knowledge in Try it Out sections, which reinforce learning and help you understand the practical applications of the new concepts you have explored. Through this approach, you can write useful programming code following each of the steps that you explore in this essential text. * Discover the basics of programming with C#, such as variables, expressions, flow control, and functions * Discuss how to keep your program running smoothly through debugging and error handling * Understand how to navigate your way through key programming elements, such as classes, class members, collections, comparisons, and conversions * Explore object-oriented programming, web programming, and Windows programming Beginning C# 6 Programming with Visual Studio 2015 is a fundamental resource for any programmers who are new to the C# language.

Sie lesen das E-Book in den Legimi-Apps auf:

Android
iOS
von Legimi
zertifizierten E-Readern

Seitenzahl: 1139

Veröffentlichungsjahr: 2016

Bewertungen
4,7 (16 Bewertungen)
11
5
0
0
0
Mehr Informationen
Mehr Informationen
Legimi prüft nicht, ob Rezensionen von Nutzern stammen, die den betreffenden Titel tatsächlich gekauft oder gelesen/gehört haben. Wir entfernen aber gefälschte Rezensionen.



Table of Contents

Title Page

Introduction

Who This Book Is For

What This Book Covers

How This Book Is Structured

What You Need to Use This Book

Conventions

Source Code

Errata

p2p.wrox.com

Part I: The OOP Language

Chapter 1: Introducing C#

What Is the .NET Framework?

What Is C#?

Visual Studio 2015

Chapter 2: Writing a C# Program

The Visual Studio 2015 Development Environment

Console Applications

Desktop Applications

Chapter 3: Variables and Expressions

Basic C# Syntax

Basic C# Console Application Structure

Variables

Expressions

Chapter 4: Flow Control

Boolean Logic

Branching

Looping

Chapter 5: More about Variables

Type Conversion

Complex Variable Types

String Manipulation

Chapter 6: Functions

Defining and Using Functions

Variable Scope

The Main() Function

Struct Functions

Overloading Functions

Using Delegates

Chapter 7: Debugging and Error Handling

Debugging in Visual Studio

Error Handling

Chapter 8: Introduction to Object-Oriented Programming

What Is Object-Oriented Programming?

OOP Techniques

OOP in Desktop Applications

Chapter 9: Defining Classes

Class Definitions in C#

System.Object

Constructors and Destructors

OOP Tools in Visual Studio

Class Library Projects

Interfaces Versus Abstract Classes

Struct Types

Shallow Copying Versus Deep Copying

Chapter 10: Defining Class Members

Member Definitions

Additional Class Member Topics

Interface Implementation

Partial Class Definitions

Partial Method Definitions

Example Application

The Call Hierarchy Window

Chapter 11: Collections, Comparisons, and Conversions

Collections

Comparisons

Conversions

Chapter 12: Generics

What Are Generics?

Using Generics

Defining Generic Types

Variance

Chapter 13: Additional C# Techniques

The : : Operator and the Global Namespace Qualifier

Custom Exceptions

Events

Expanding and Using CardLib

Attributes

Initializers

Type Inference

Anonymous Types

Dynamic Lookup

Advanced Method Parameters

Lambda Expressions

Part II: Windows Programming

Chapter 14: Basic Desktop Programming

XAML

The Playground

Control Layout

The Game Client

Chapter 15: Advanced Desktop Programming

Creating and Styling Controls

WPF User Controls

Putting It All Together

Part III: Cloud Programming

Chapter 16: Basic Cloud Programming

The Cloud, Cloud Computing, and the Cloud Optimized Stack

Cloud Patterns and Best Practices

Using Microsoft Azure C# Libraries to Create a Storage Container

Creating an ASP.NET 4.6 Web Site that Uses the Storage Container

How It Works

Chapter 17: Advanced Cloud Programing and Deployment

Creating an ASP.NET Web API

Deploying and Consuming an ASP.NET Web API on Microsoft Azure

Scaling an ASP.NET Web API on Microsoft Azure

Part IV: Data Access

Chapter 18: Files

File Classes for Input and Output

Streams

Monitoring the File System

Chapter 19: XML and JSON

JSON Basics

XML Schemas

Converting XML to JSON

Chapter 20: LINQ

LINQ to XML

LINQ Query Syntax

LINQ Method Syntax

Ordering Query Results

Understanding the orderby Clause

Querying a Large Data Set

Using Aggregate Operators

Using the Select Distinct Query

Ordering by Multiple Levels

Using Group Queries

Using Joins

Chapter 21: Databases

Using Databases

Installing SQL Server Express

Entity Framework

A Code First Database

But Where Is My Database?

Navigating Database Relationships

Handling Migrations

Creating and Querying XML from an Existing Database

Part V: Additional Techniques

Chapter 22: Windows Communication Foundation

What Is WCF?

WCF Concepts

WCF Programming

Chapter 23: Universal Apps

Getting Started

Universal Apps

App Concepts and Design

App Development

Common Elements of Windows Store Apps

The Windows Store

Appendix: Exercise Solutions

Chapter 4 Solutions

Chapter 5 Solutions

Chapter 6 Solutions

Chapter 7 Solutions

Chapter 8 Solutions

Chapter 9 Solutions

Chapter 10 Solutions

Chapter 11 Solutions

Chapter 12 Solutions

Chapter 13 Solutions

Chapter 14 Solutions

Chapter 15 Solutions

Chapter 16 Solutions

Chapter 17 Solutions

Chapter 18 Solutions

Chapter 19 Solutions

Chapter 20 Solutions

Chapter 21 Solutions

Chapter 22 Solutions

Chapter 23 Solutions

End User License Agreement

Pages

v

vi

vii

viii

xix

xx

xxi

xxii

xxiii

xxiv

xxv

xxvi

1

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

372

373

374

375

376

377

378

379

380

381

382

383

384

385

386

387

388

389

390

391

392

393

394

395

396

397

398

399

400

401

402

403

404

405

407

408

409

410

411

412

413

414

415

416

417

418

419

420

421

422

423

424

425

426

427

428

429

430

431

432

433

434

435

436

437

438

439

440

441

442

443

444

445

446

447

448

449

450

451

452

453

454

455

456

457

458

459

461

462

463

464

465

466

467

468

469

470

471

472

473

474

475

476

477

478

479

480

481

482

483

484

485

486

487

488

489

490

491

492

493

494

495

496

497

498

499

500

501

502

503

504

505

506

507

508

509

510

511

512

513

515

516

517

518

519

520

521

522

523

524

525

526

527

528

529

530

531

532

533

534

535

536

537

538

539

540

541

542

543

544

545

546

547

548

549

550

551

552

553

554

555

556

557

559

561

562

563

564

565

566

567

568

569

570

571

572

573

574

575

576

577

578

579

580

581

582

583

584

585

586

587

588

589

590

591

593

594

595

596

597

598

599

600

601

602

603

604

605

606

607

608

609

610

611

612

613

614

615

616

617

618

619

620

621

622

623

624

625

626

627

628

629

630

631

632

633

634

635

636

637

638

639

640

641

642

643

644

645

646

647

648

649

651

652

653

654

655

656

657

658

659

660

661

662

663

664

665

666

667

668

669

670

671

672

673

674

675

677

678

679

680

681

682

683

684

685

686

687

688

689

690

691

692

693

694

695

696

697

698

699

700

701

702

703

704

705

706

707

708

709

710

711

712

713

714

715

716

717

718

719

720

721

722

723

724

725

726

727

728

729

730

731

732

733

734

735

736

Guide

Table of Contents

Begin Reading

List of Illustrations

Chapter 1: Introducing C#

Figure 1.1

Figure 1.2

Figure 1.3

Figure 1.4

Chapter 2: Writing a C# Program

Figure 2.1

Figure 2.2

Figure 2.3

Figure 2.3

Figure 2.4

Figure 2.5

Figure 2.6

Figure 2.7

Figure 2.8

Figure 2.9

Figure 2.10

Figure 2.11

Figure 2.12

Figure 2.13

Chapter 3: Variables and Expressions

Figure 3.1

Figure 3.2

Figure 3.3

Figure 3.4

Chapter 4: Flow Control

Figure 4.1

Figure 4.2

Figure 4.3

Figure 4.4

Figure 4.5

Figure 4.6

Figure 4.7

Chapter 5: More about Variables

Figure 5.1

Figure 5.2

Figure 5.3

Figure 5.4

Figure 5.5

Figure 5.6

Figure 5.7

Figure 5.8

Figure 5.9

Figure 5.10

Figure 5.11

Figure 5.12

Figure 5.13

Figure 5.14

Chapter 6: Functions

Figure 6.1

Figure 6.2

Figure 6.3

Figure 6.4

Figure 6.5

Figure 6.6

Figure 6.7

Figure 6.8

Figure 6.9

Figure 6.10

Chapter 7: Debugging and Error Handling

Figure 7.1

Figure 7.2

Figure 7.3

Figure 7.4

Figure 7.5

Figure 7.6

Figure 7.7

Figure 7.8

Figure 7.9

Figure 7.10

Figure 7.11

Figure 7.12

Figure 7.13

Figure 7.14

Figure 7.15

Figure 7.16

Figure 7.17

Figure 7.18

Figure 7.19

Figure 7.20

Chapter 8: Introduction to Object-Oriented Programming

Figure 8.1

Figure 8.2

Figure 8.3

Figure 8.4

Figure 8.5

Figure 8.6

Figure 8.7

Figure 8.8

Figure 8.9

Figure 8.10

Figure 8.11

Figure 8.12

Figure 8.13

Figure 8.14

Figure 8.15

Chapter 9: Defining Classes

Figure 9.1

Figure 9.2

Figure 9.3

Figure 9.4

Figure 9.5

Figure 9.6

Figure 9.7

Figure 9.8

Figure 9.9

Figure 9.10

Figure 9.11

Figure 9.12

Figure 9.13

Figure 9.14

Chapter 10: Defining Class Members

Figure 10.1

Figure 10.2

Figure 10.3

Figure 10.4

Figure 10.5

Figure 10.6

Figure 10.7

Figure 10.8

Figure 10.9

Figure 10.10

Figure 10.11

Figure 10.12

Figure 10.13

Figure 10.14

Chapter 11: Collections, Comparisons, and Conversions

Figure 11.1

Figure 11.2

Figure 11.3

Figure 11.4

Figure 11.5

Figure 11.6

Figure 11.7

Figure 11.8

Chapter 12: Generics

Figure 12.1

Figure 12.2

Figure 12.3

Figure 12.4

Figure 12.5

Chapter 13: Additional C# Techniques

Figure 13.1

Figure 13.2

Figure 13.3

Figure 13.4

Figure 13.5

Figure 13.6

Figure 13.7

Figure 13.8

Figure 13.9

Figure 13.10

Figure 13.11

Figure 13.12

Figure 13.13

Figure 13.14

Figure 13.15

Figure 13.16

Figure 13.17

Figure 13.18

Figure 13.19

Figure 13.20

Figure 13.21

Chapter 14: Basic Desktop Programming

Figure 14.1

Figure 14.2

Figure 14.3

Figure 14.4

Figure 14.5

Figure 14.6

Figure 14.7

Figure 14.8

Figure 14.9

Figure 14.10

Figure 14.11

Figure 14.12

Chapter 15: Advanced Desktop Programming

Figure 15.1

Figure 15.2

Figure 15.3

Figure 15.4

Chapter 16: Basic Cloud Programming

Figure 16.1

Figure 16.2

Figure 16.3

Figure 16.4

Figure 16.5

Figure 16.6

Figure 16.7

Figure 16.8

Figure 16.9

Figure 16.10

Figure 16.11

Figure 16.12

Figure 16.13

Figure 16.14

Figure 16.15

Chapter 17: Advanced Cloud Programing and Deployment

Figure 17.1

Figure 17.2

Figure 17.3

Figure 17.2

Figure 17.4

Figure 17.5

Figure 17.6

Figure 17.7

Figure 17.8

Figure 17.9

Figure 17.10

Figure 17.11

Figure 17.12

Figure 17.13

Chapter 18: Files

Figure 18.1

Figure 18.2

Figure 18.3

Figure 18.4

Figure 18.5

Figure 18.6

Figure 18.7

Figure 18.8

Chapter 19: XML and JSON

Figure 19.1

Figure 19.2

Figure 19.3

Figure 19.4

Figure 19.5

Figure 19.6

Figure 19.7

Figure 19.8

Chapter 21: Databases

Figure 21.1

Figure 21.2

Figure 21.3

Figure 21.3

Figure 21.5

Figure 21.7

Figure 21.8

Figure 21.9

Figure 21.10

Figure 21.11

Figure 21.12

Figure 21.13

Figure 21.14

Figure 21.15

Figure 21.16

Figure 21.17

Figure 21.18

Figure 21.19

Chapter 22: Windows Communication Foundation

Figure 22.1

Figure 22.2

Figure 22.3

Figure 22.4

Figure 22.5

Chapter 23: Universal Apps

Figure 23.1

Figure 23.2

List of Tables

Chapter 3: Variables and Expressions

Table 3.1 Integer Types

Table 3.2 Floating-point Types

Table 3.3 Text and Boolean Types

Table 3.4 Literal Values

Table 3.5 Escape Sequences for String Literals

Table 3.6 Simple Mathematical Operators

Table 3.7 The String Concatenation Operator

Table 3.8 Increment and Decrement Operators

Table 3.9 Assignment Operators

Table 3.10 Operator Precedence

Chapter 4: Flow Control

Table 4.1 Boolean Comparison Operators

Table 4.2 Conditional Boolean Operators

Table 4.3 Boolean Assignment Operators

Table 4.4 Operator Precedence (Updated)

Chapter 5: More about Variables

Table 5.1 Implicit Numeric Conversions

Chapter 9: Defining Classes

Table 9.1 Access Modifiers for Class Definitions

Table 9.2 Methods of System.Object

Table 9.3 Class View Icons

Table 9.4 Additional Class View Icons

Chapter 12: Generics

Table 12.1 Generic Collection Type

Table 12.2 Sorting with Generic Types

Table 12.3 Generic Type Constraints

Chapter 14: Basic Desktop Programming

Table 14.1 Common Control Events

Table 14.2 Image Control

Table 14.3 TextBox Properties

Table 14.4 CheckBox Properties

Table 14.5 RadioButton Properties

Table 14.6 IsReadOnly and IsEdiTable Combinations

Table 14.7 Other ComboBox Properties

Table 14.8 Interesting ListBox Properties

Table 14.9 The Name Property

Chapter 15: Advanced Desktop Programming

Table 15.1 Displaying MenuItem Properties

Table 15.2 The Timeline Properties

Table 15.3 Properties of the Spline Key Frame Classes

Table 15.4 The Register( ) Method's Parameters

Table 15.5 Overloads for the FrameworkPropertyMetadata Constructor

Table 15.6 Property and Field Names

Table 15.7 Cards in Hand Dependency Properties

Table 15.8 Game Decks Dependency Properties

Chapter 18: Files

Table 18.1 File System Access Classes

Table 18.2 Static Methods of the File Class

Table 18.3 Static Methods of the Directory Class

Table 18.4 FileSystemInfo Properties

Table 18.5 FileInfo Properties

Table 18.6 Properties Unique to the DirectoryInfo Class

Table 18.7 Stream Classes

Table 18.8 FileAccess Enumeration Members

Table 18.9 FileMode Enumeration Members

Table 18.10 FileSystemWatcher Properties

Chapter 19: XML and JSON

Table 19.1 Common DOM Classes

Table 19.2 XmlElement Properties

Table 19.3 Three Ways to Get the Value of a Node

Table 19.4 Methods for Creating Nodes

Table 19.5 Methods for Inserting Nodes

Table 19.6 Methods for Removing Nodes

Table 19.7 Methods for Selecting Nodes

Table 19.8 Common XPath Operations

Chapter 20: LINQ

Table 20.1 Aggregate Operators for Numeric Results

Chapter 22: Windows Communication Foundation

Table 22.1 Binding Types

Table 22.2 NET Default Bindings

Table 22.3 DataContractAttribute Properties

Table 22.4 DataMemberAttribute Properties

Table 22.5 ServiceContractAttribute Properties

Table 22.6 OperationContractAttribute Properties

Introduction

C# is a relatively new language that was unveiled to the world when Microsoft announced the first version of its .NET Framework in July 2000. Since then its popularity has rocketed, and it has arguably become the language of choice for desktop, web, and cloud developers who use the .NET Framework. 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 now no reason not to move into C#. The language is not difficult and it's a great one to learn elementary programming techniques with. 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#, C# 6, which is included with version 4.6 of the .NET Framework, builds on the existing successes and adds even more attractive features. The latest release of Visual Studio (Visual Studio 2015) and the Visual Studio Express/Community 2015 line of development tools also bring many tweaks and improvements to make your life easier and to dramatically increase your productivity.

This book is intended to teach you about all aspects of C# programming, including the language itself, desktop and cloud programming, making use of data sources, and some new and advanced techniques. You'll also learn about the capabilities of Visual Studio 2015 and all the ways that this product can aid your application development.

The book is written in a friendly, mentor-style fashion, with each chapter building on previous ones, and every effort is made to ease you into advanced techniques painlessly. At no point will technical terms appear from nowhere to discourage you from continuing; every concept is introduced and discussed as required. Technical jargon is kept to a minimum; but where it is necessary, it, too, is properly defined and laid out in context.

The authors of this book are all experts in their field and are all enthusiastic in their passion for both the C# language and the .NET Framework. Nowhere will you find a group of people better qualified to take you under their collective wing and nurture your understanding of C# from first principles to advanced techniques. Along with the fundamental knowledge it provides, this book is packed full of helpful hints, tips, exercises, and full-fledged example code (available for download at p2p.wrox.com) that you will find yourself returning to repeatedly as your career progresses.

We pass this knowledge on without begrudging it and hope that you will be able to use it to become the best programmer you can be. Good luck, and all the best!

Who This Book Is For

This book is for everyone who wants to learn how to program in C# using the .NET Framework. It is for absolute beginners who want to give programming a try by learning a clean, modern, elegant programming language. But it is also for people familiar with other programming languages who want to explore the .NET platform, as well as for existing .NET developers who want to give Microsoft's .NET flagship language a try.

What This Book Covers

The early chapters cover the language itself, assuming no prior programming experience. If you have programmed in other languages before, much of the material in these chapters will be familiar. Many aspects of C# syntax are shared with other languages, and many structures are common to practically all programming languages (such as looping and branching structures). However, even if you are an experienced programmer, you will benefit from looking through these chapters to learn the specifics of how these techniques apply to C#.

If you are new to programming, you should start from the beginning, where you will learn basic programming concepts and become acquainted with both C# and the .NET platform that underpins it. If you are new to the .NET Framework but know how to program, you should read Chapter 1 and then skim through the next few chapters before continuing with the application of the C# language. If you know how to program but haven't encountered an object-oriented programming language before, you should read the chapters from Chapter 8 onward.

Alternatively, if you already know the C# language, you might want to concentrate on the chapters dealing with the most recent .NET Framework and C# language developments, specifically the chapters on collections, generics, and C# language enhancements (Chapters 11 to 13), or skip the first section of the book completely and start with Chapter 14.

The chapters in this book have been written with a dual purpose in mind: They can be read sequentially to provide a complete tutorial in the C# language, and they can be dipped into as required reference material.

In addition to the core material, starting with Chapter 3 each chapter also includes a selection of exercises at the end, which you can work through to ensure that you have understood the material. The exercises range from simple multiple choice or true/false questions to more complex exercises that require you to modify or build applications. The answers to all the exercises are provided in Appendix A. You can also find these exercises as part of the wrox.com code downloads for this book at www.wrox.com/go/beginningvisualc#2015programming.

This book also gives plenty of love and attention to coincide with the release of C# 6 and .NET 4.6. Every chapter received an overhaul, with less relevant material removed, and new material added. All of the code has been tested against the latest version of the development tools used, and all of the screenshots have been retaken in Windows 8.1/10 to provide the most current windows and dialog boxes.

New highlights of this edition include the following:

Additional and improved code examples for you to try out

Coverage of everything that's new in C# 6 and .NET 4.6, including how to create Universal Windows Apps

Examples of programming cloud applications and using Azure SDK to create and access cloud resources

How This Book Is Structured

This book is divided into six sections:

Introduction

— Purpose and general outline of the book's contents

The C# Language

— Covers all aspects of the C# language, from the fundamentals to object-oriented techniques

Windows Programming

— How to write and deploy desktop applications with the Windows Presentation Foundation library (WPF)

Cloud Programming

— Cloud application development and deployment, including the creation and consumption of a Web API

Data Access

— How to use data in your applications, including data stored in files on your hard disk, data stored in XML format, and data in databases

Additional Techniques

— An examination of some extra ways to use C# and the .NET Framework, including Windows Communication Foundation (WCF) and Universal Windows Applications

The following sections describe the chapters in the five major parts of this book.

The C# Language (Chapters 1–13)

Chapter 1 introduces you to C# and how it fits into the .NET landscape. You'll learn the fundamentals of programming in this environment and how Visual Studio 2015 (VS) fits in.

Chapter 2 starts you off with writing C# applications. You'll look at the syntax of C# and put the language to use with sample command-line and Windows applications. These examples demonstrate just how quick and easy it can be to get up and running, and along the way you'll be introduced to the Visual Studio development environment and the basic windows and tools that you'll be using throughout the book.

Next you'll learn more about the basics of the C# language. You'll learn what variables are and how to manipulate them in Chapter 3. You'll enhance the structure of your applications with flow control (looping and branching) in Chapter 4, and you'll see some more advanced variable types such as arrays in Chapter 5. In Chapter 6 you'll start to encapsulate your code in the form of functions, which makes it much easier to perform repetitive operations and makes your code much more readable.

By the beginning of Chapter 7 you'll have a handle on the fundamentals of the C# language, and you will focus on debugging your applications. This involves looking at outputting trace information as your applications are executed, and at how Visual Studio can be used to trap errors and lead you to solutions for them with its powerful debugging environment.

From Chapter 8 onward you'll learn about object-oriented programming (OOP), starting with a look at what this term means and an answer to the eternal question, “What is an object?” OOP can seem quite difficult at first. The whole of Chapter 8 is devoted to demystifying it and explaining what makes it so great, and you won't actually deal with much C# code until the very end of the chapter.

Everything changes in Chapter 9, when you put theory into practice and start using OOP in your C# applications. This is where the true power of C# lies. You'll start by looking at how to define classes and interfaces, and then move on to class members (including fields, properties, and methods) in Chapter 10. At the end of that chapter you'll start to assemble a card game application, which is developed over several chapters and will help to illustrate OOP.

Once you've learned how OOP works in C#, Chapter 11 moves on to look at common OOP scenarios, including dealing with collections of objects, and comparing and converting objects. Chapter 12 takes a look at a very useful feature of C# that was introduced in .NET 2.0: generics, which enable you to create very flexible classes. Next, Chapter 13 continues the discussion of the C# language and OOP with some additional techniques, notably events, which become very important in, for example, Windows programming. Chapter 13 wraps up the fundamentals by focusing on C# language features that were introduced with versions 3.0, 4, 5, and 6 of the language.

Windows Programming (Chapters 14–15)

Chapter 14 starts by introducing you to what is meant by Windows programming and looks at how this is achieved in Visual Studio. It focuses on WPF as a tool that enables you to build desktop applications in a graphical way and assemble advanced applications with the minimum of effort and time. You'll start with the basics of WPF programming and build up your knowledge in both this chapter and Chapter 15, which demonstrates how you can use the wealth of controls supplied by the .NET Framework in your applications.

Cloud Programming (Chapters 16–17)

Chapter 16 starts by describing what cloud programming is and discusses the cloud optimized stack. The cloud environment is not identical to the way programs have been traditionally coded, so a few cloud programming patterns are discussed and defined. To complete this chapter, you require an Azure account, which is free, so that you can create an App Services Web App, then using the Azure SDK with C#, you create and access a storage account from an ASP.NET 4.6 web application.

In Chapter 17, you learn how to create and deploy an ASP.NET Web API to the cloud and then consume the Web API from a similar ASP.NET 4.6 web application. The chapter ends discussing two of the most valuable features in the cloud, scaling and the optimal utilization of hardware resources.

Data Access (Chapters 18–21)

Chapter 18 looks at how your applications can save and retrieve data to disk, both as simple text files and as more complex representations of data. You'll also learn how to compress data and how to monitor and act on file system changes.

In Chapter 19 you'll learn about the de facto standard for data exchange — namely, XML — and a rapidly emerging format called JSON. By this point in the book, you'll have touched on XML several times in preceding chapters, but this chapter lays out the ground rules and shows you what all the excitement is about.

The remainder of this part looks at LINQ, which is a query language built in to the latest versions of the .NET Framework. You start in Chapter 20 with a general introduction to LINQ, and then you will use LINQ to access a database and other data in Chapter 21.

Additional Techniques (Chapters 22–23)

Chapter 22 is an introduction to Windows Communication Foundation (WCF), which provides you with the tools you need for enterprise-level programmatic access to information and capabilities across local networks and the Internet. You will see how you can use WCF to expose complex data and functionality to web and desktop applications in a platform-independent way.

Chapter 23 shows you how you can create Universal Windows Apps, which are new to Windows. This chapter builds on the foundation of Chapters 14 and 15 to show you how to create Windows Apps that can run on all windows platforms.

What You Need to Use This Book

The code and descriptions of C# and the .NET Framework in this book apply to C# 6 and .NET 4.6. You don't need anything other than the Framework to understand this aspect of the book, but many of the examples require a development tool. This book uses Visual Studio 2015 as its primary development tool; however, if you don't have this, you will be able to use the free Visual Studio Express/Community 2015 line of products. For the first part of the book, Visual Studio Express/Community 2012 for Windows Desktop will enable you to create desktop and console applications. For later chapters, you may also use Visual Studio Express/Community 2015 for Windows 10 in order to create Universal Windows Apps, Visual Studio Express/Community 2015 for Cloud to create cloud applications, and SQL Server Express 2014 for applications that access databases. Some functionality is available only in Visual Studio 2015, but this won't stop you from working through any of the examples in this book.

The source code for the samples is available for download from the Wrox website at:

www.wrox.com/go/beginningvisualc#2015programming

Conventions

To help you get the most from the text and keep track of what's happening, we've used a number of conventions throughout the book.

TRY IT OUT

The Try It Out is an exercise you should work through, following the text in the book.

They usually consist of a set of steps.

Each step has a number.

Follow the steps through with your copy of the database.

How It Works

After each Try It Out, the code you've typed will be explained in detail.

WARNING

Warnings hold important, not-to-be-forgotten information that is directly relevant to the surrounding text.

NOTE

Notes indicates notes, tips, hints, tricks, or and asides to the current discussion.

As for styles in the text:

We

highlight

new terms and important words when we introduce them.

We show keyboard strokes like this: Ctrl+A.

We show file names, URLs, and code within the text like so:

persistence.properties

.

We present code in two different ways:

We use a monofont type with no highlighting for most code examples.

We use bold to emphasize code that is particularly important in the present context or to show changes from a previous code snippet.

Source Code

As you work through the examples in this book, you may choose either to type in all the code manually, or to use the source code files that accompany the book. All the source code used in this book is available for download at www.wrox.com. Specifically for this book, the code download is on the Download Code tab at:

www.wrox.com/go/beginningvisualc#2015programming

You can also search for the book at www.wrox.com by ISBN (the ISBN for this book is 978-1-119-09668-9) to find the code. And a complete list of code downloads for all current Wrox books is available at www.wrox.com/dynamic/books/download.aspx.

Most of the code on www.wrox.com is compressed in a .ZIP, .RAR archive or similar archive format appropriate to the platform. Once you download the code, just decompress it with an appropriate compression tool.

NOTE

Because many books have similar titles, you may find it easiest to search by ISBN; this book's ISBN is 978-1-119-09668-9.

Alternately, you can go to the main Wrox code download page at www.wrox.com/dynamic/books/download.aspx to see the code available for this book and all other Wrox books.

Errata

We make every effort to ensure that there are no errors in the text or in the code. However, no one is perfect, and mistakes do occur. If you find an error in one of our books, like a spelling mistake or faulty piece of code, we would be very grateful for your feedback. By sending in errata, you may save another reader hours of frustration, and at the same time, you will be helping us provide even higher quality information.

To find the errata page for this book, go to

www.wrox.com/go/beginningvisualc#2015programming

And click the Errata link. On this page you can view all errata that has been submitted for this book and posted by Wrox editors.

If you don't spot “your” error on the Book Errata page, go to www.wrox.com/contact/techsupport.shtml and complete the form there to send us the error you have found. We'll check the information and, if appropriate, post a message to the book's errata page and fix the problem in subsequent editions of the book.

p2p.wrox.com

For author and peer discussion, join the P2P forums at http://p2p.wrox.com. The forums are a Web-based system for you to post messages relating to Wrox books and related technologies and interact with other readers and technology users. The forums offer a subscription feature to e-mail you topics of interest of your choosing when new posts are made to the forums. Wrox authors, editors, other industry experts, and your fellow readers are present on these forums.

At http://p2p.wrox.com, you will find a number of different forums that will help you, not only as you read this book, but also as you develop your own applications. To join the forums, just follow these steps:

Go to

http://p2p.wrox.com

and click the Register link.

Read the terms of use and click Agree.

Complete the required information to join, as well as any optional information you wish to provide, and click Submit.

You will receive an e-mail with information describing how to verify your account and complete the joining process.

NOTE

You can read messages in the forums without joining P2P, but in order to post your own messages, you must join.

Once you join, you can post new messages and respond to messages other users post. You can read messages at any time on the Web. If you would like to have new messages from a particular forum e-mailed to you, click the Subscribe to this Forum icon by the forum name in the forum listing.

For more information about how to use the Wrox P2P, be sure to read the P2P FAQs for answers to questions about how the forum software works, as well as many common questions specific to P2P and Wrox books. To read the FAQs, click the FAQ link on any P2P page.

Part I

The OOP Language

Chapter 1

: Introducing C#

Chapter 2

: Writing a C# Program

Chapter 3

: Variables and Expressions

Chapter 4

: Flow Control

Chapter 5

: More about Variables

Chapter 6

: Functions

Chapter 7

: Debugging and Error Handling

Chapter 8

: Introduction to Object-Oriented Programming

Chapter 9

: Defining Classes

Chapter 10

: Defining Class Members

Chapter 11

: Collections, Comparisons, and Conversions

Chapter 12

: Generics

Chapter 13

: Additional C# Techniques

Chapter 1Introducing C#

What you will learn in this chapter

Exploring the .NET Framework

Learning how .NET applications work

Exploring C# and how it relates to the .NET Framework

Discovering tools for creating .NET applications with C#

Wrox.com Code Downloads for this Chapter

The wrox.com code downloads for this chapter are found at www.wrox.com/go/beginningvisualc#2015programming on the Download Code tab. The code is in the Chapter 1 download and individually named according to the names throughout the chapter.

Welcome to the first chapter of the first section of this book. This section provides you with the basic knowledge you need to get up and running with the most recent version of C#. Specifically, this chapter provides an overview of C# and the .NET Framework, including what these technologies are, the motivation for using them, and how they relate to each other.

It begins with a general discussion of the .NET Framework. This technology contains many concepts that are tricky to come to grips with initially. This means that the discussion, by necessity, covers many concepts in a short amount of space. However, a quick look at the basics is essential to understanding how to program in C#. Later in the book, you revisit many of the topics covered here, exploring them in more detail.

After that general introduction, the chapter provides a basic description of C# itself, including its origins and similarities to C++. Finally, you look at the primary tool used throughout this book: Visual Studio 2015 (VS). Visual Studio 2015 is the latest in a long line of development environments that Microsoft has produced, and it includes all sorts of features (including full support for Windows Store applications) that you will learn about throughout this book.

What Is the .NET Framework?

The .NET Framework (now at version 4.6) is a revolutionary platform created by Microsoft for developing applications. The most interesting thing about this statement is how vague it is — but there are good reasons for this. For a start, note that it doesn't “develop applications on the Windows operating system.” Although the Microsoft release of the .NET Framework runs on the Windows and Windows Phone operating systems, it is possible to find alternative versions that will work on other systems. One example of this is Mono, an open-source version of the .NET Framework (including a C# compiler) that runs on several operating systems, including various ­flavors of Linux and Mac OS; you can read more about it at http://www.mono-project.com. There are also variants of Mono that run on iPhone (MonoTouch) and Android (Mono for Android, a.k.a. MonoDroid) smartphones. One of the key motivations behind the .NET Framework is its intended use as a means of integrating disparate operating systems.

In addition, the preceding definition of the .NET Framework includes no restriction on the type of applications that are possible. That's because there is no restriction — the .NET Framework enables the creation of desktop applications, Windows Store applications, cloud/web applications, Web APIs, and pretty much anything else you can think of. Also, with web, cloud and Web API ­applications it's worth noting that these are, by definition, multi-platform applications, since any system with a web browser can access them.

The .NET Framework has been designed so that it can be used from any language, including C# (the subject of this book) as well as C++, Visual Basic, JScript, and even older languages such as COBOL. For this to work, .NET-specific versions of these languages have also appeared, and more are being released all the time. For a list of languages, see this site https://msdn.microsoft.com/library/aa292164.aspx. Not only do all of these have access to the .NET Framework, but they can also communicate with each other. It is possible for C# developers to make use of code written by Visual Basic programmers, and vice versa.

All of this provides an extremely high level of versatility and is part of what makes using the .NET Framework such an attractive prospect.

What's in the .NET Framework?

The .NET Framework consists primarily of a gigantic library of code that you use from your ­client languages (such as C#) using object-oriented programming (OOP) techniques. This library is ­categorized into different modules — you use portions of it depending on the results you want to achieve. For example, one module contains the building blocks for Windows applications, another for network programming, and another for web development. Some modules are divided into more specific submodules, such as a module for building web services within the module for web development.

The intention is for different operating systems to support some or all of these modules, depending on their characteristics. A smartphone, for example, includes support for all the core .NET functionality but is unlikely to require some of the more esoteric modules.

Part of the .NET Framework library defines some basic types. A type is a representation of data, and specifying some of the most fundamental of these (such as “a 32-bit signed integer”) facilitates interoperability between languages using the .NET Framework. This is called the Common Type System (CTS).

As well as supplying this library, the .Net Framework also includes the .NET Common Language Runtime (CLR), which is responsible for the execution of all applications developed using the.NET library.

Writing Applications Using the .NET Framework

Writing an application using the .NET Framework means writing code (using any of the languages that support the Framework) using the .NET code library. In this book you use Visual Studio for your development. Visual Studio is a powerful, integrated development environment that supports C# (as well as managed and unmanaged C++, Visual Basic, and some others). The advantage of this environment is the ease with which .NET features can be integrated into your code. The code that you create will be entirely C# but use the .NET Framework throughout, and you'll make use of the additional tools in Visual Studio where necessary.

In order for C# code to execute, it must be converted into a language that the target operating ­system understands, known as native code. This conversion is called compiling code, an act that is ­performed by a compiler. Under the .NET Framework, this is a two-stage process.

CIL and JIT

When you compile code that uses the .NET Framework library, you don't immediately create operating system–specific native code. Instead, you compile your code into Common Intermediate Language (CIL) code. This code isn't specific to any operating system (OS) and isn't specific to C#. Other .NET languages — Visual Basic .NET, for example — also compile to this language as a first stage. This compilation step is carried out by Visual Studio when you develop C# applications.

Obviously, more work is necessary to execute an application. That is the job of a just-in-time (JIT) compiler, which compiles CIL into native code that is specific to the OS and machine architecture being targeted. Only at this point can the OS execute the application. The just-in-time part of the name reflects the fact that CIL code is compiled only when it is needed. This compilation can ­happen on the fly while your application is running, although luckily this isn't something that you ­normally need to worry about as a developer. Unless you are writing extremely advanced code where performance is critical, it's enough to know that this compilation process will churn along merrily in the background, without interfering.

In the past, it was often necessary to compile your code into several applications, each of which targeted a specific operating system and CPU architecture. Typically, this was a form of optimization (to get code to run faster on an AMD chipset, for example), but at times it was critical (for applications to work in both Win9x and WinNT/2000 environments, for example). This is now unnecessary, because JIT compilers (as their name suggests) use CIL code, which is independent of the machine, operating system, and CPU. Several JIT compilers exist, each targeting a different architecture, and the CLR uses the appropriate one to create the native code required.

The beauty of all this is that it requires a lot less work on your part — in fact, you can forget about system-dependent details and concentrate on the more interesting functionality of your code.

NOTE

You might come across references to Microsoft Intermediate Language (MSIL) or just IL. MSIL was the original name for CIL, and many developers still use this terminology.

Assemblies

When you compile an application, the CIL code is stored in an assembly. Assemblies include both executable application files that you can run directly from Windows without the need for any other programs (these have a .exe file extension) and libraries (which have a .dll extension) for use by other applications.

In addition to containing CIL, assemblies also include meta information (that is, information about the information contained in the assembly, also known as metadata) and optional resources (additional data used by the CIL, such as sound files and pictures). The meta information enables assemblies to be fully self-descriptive. You need no other information to use an assembly, meaning you avoid situations such as failing to add required data to the system registry and so on, which was often a problem when developing with other platforms.

This means that deploying applications is often as simple as copying the files into a directory on a remote computer. Because no additional information is required on the target systems, you can just run an executable file from this directory and (assuming the .NET CLR is installed) you're good to go.

Of course, you won't necessarily want to include everything required to run an application in one place. You might write some code that performs tasks required by multiple applications. In situations like that, it is often useful to place the reusable code in a place accessible to all applications. In the .NET Framework, this is the global assembly cache (GAC). Placing code in the GAC is simple — you just place the assembly containing the code in the directory containing this cache.

Managed Code

The role of the CLR doesn't end after you have compiled your code to CIL and a JIT compiler has compiled that to native code. Code written using the .NET Framework is managed when it is executed (a stage usually referred to as runtime). This means that the CLR looks after your applications by managing memory, handling security, allowing cross-language debugging, and so on. By contrast, applications that do not run under the control of the CLR are said to be unmanaged, and certain languages such as C++ can be used to write such applications, which, for example, access low-level functions of the operating system. However, in C# you can write only code that runs in a managed environment. You will make use of the managed features of the CLR and allow .NET itself to handle any interaction with the operating system.

Garbage Collection

One of the most important features of managed code is the concept of garbage collection. This is the .NET method of making sure that the memory used by an application is freed up completely when the application is no longer in use. Prior to .NET this was mostly the responsibility of programmers, and a few simple errors in code could result in large blocks of memory mysteriously disappearing as a result of being allocated to the wrong place in memory. That usually meant a progressive slowdown of your computer, followed by a system crash.

.NET garbage collection works by periodically inspecting the memory of your computer and ­removing anything from it that is no longer needed. There is no set time frame for this; it might ­happen thousands of times a second, once every few seconds, or whenever, but you can rest assured that it will happen.

There are some implications for programmers here. Because this work is done for you at an ­unpredictable time, applications have to be designed with this in mind. Code that requires a lot of ­memory to run should tidy itself up, rather than wait for garbage collection to happen, but that isn't as tricky as it sounds.

Fitting It Together

Before moving on, let's summarize the steps required to create a .NET application as discussed previously:

Application code is written using a .NET-compatible language such as C# (see

Figure 1.1

).

Figure 1.1

That code is compiled into CIL, which is stored in an assembly (see

Figure 1.2

).

Figure 1.2

When this code is executed (either in its own right if it is an executable or when it is used from other code), it must first be compiled into native code using a JIT compiler (see

Figure 1.3

).

Figure 1.3

The native code is executed in the context of the managed CLR, along with any other running applications or processes, as shown in

Figure 1.4

.

Figure 1.4

Linking

Note one additional point concerning this process. The C# code that compiles into CIL in step 2 needn't be contained in a single file. It's possible to split application code across multiple source-code files, which are then compiled together into a single assembly. This extremely useful process is known as linking. It is required because it is far easier to work with several smaller files than one enormous one. You can separate logically related code into an individual file so that it can be worked on independently and then practically forgotten about when completed. This also makes it easy to locate specific pieces of code when you need them and enables teams of developers to divide the programming burden into manageable chunks, whereby individuals can “check out” pieces of code to work on without risking damage to otherwise satisfactory sections or sections other people are working on.

What Is C#?

C#, as mentioned earlier, is one of the languages you can use to create applications that will run in the .NET CLR. It is an evolution of the C and C++ languages and has been created by Microsoft specifically to work with the .NET platform. The C# language has been designed to incorporate many of the best features from other languages, while clearing up their problems.

Developing applications using C# is simpler than using C++, because the language syntax is simpler. Still, C# is a powerful language, and there is little you might want to do in C++ that you can't do in C#. Having said that, those features of C# that parallel the more advanced features of C++, such as directly accessing and manipulating system memory, can be carried out only by using code marked as unsafe. This advanced programmatic technique is potentially dangerous (hence its name) because it is possible to overwrite system-critical blocks of memory with potentially catastrophic results. For this reason, and others, this book does not cover that topic.

At times, C# code is slightly more verbose than C++. This is a consequence of C# being a typesafe language (unlike C++). In layperson's terms, this means that once some data has been assigned to a type, it cannot subsequently transform itself into another unrelated type. Consequently, strict rules must be adhered to when converting between types, which means you will often need to write more code to carry out the same task in C# than you might write in C++. However, there are benefits to this — the code is more robust, debugging is simpler, and .NET can always track the type of a piece of data at any time. In C#, you therefore might not be able to do things such as “take the region of memory 4 bytes into this data and 10 bytes long and interpret it as X,” but that's not necessarily a bad thing.

C# is just one of the languages available for .NET development, but it is certainly the best. It has the advantage of being the only language designed from the ground up for the .NET Framework and is the principal language used in versions of .NET that are ported to other operating systems. To keep languages such as the .NET version of Visual Basic as similar as possible to their predecessors yet compliant with the CLR, certain features of the .NET code library are not fully supported, or at least require unusual syntax.

By contrast, C# can make use of every feature that the .NET Framework code library has to offer. Also, each new version of .NET has included additions to the C# language, partly in response to requests from developers, making it even more powerful.

Applications You Can Write with C#

The .NET Framework has no restrictions on the types of applications that are possible, as discussed earlier. C# uses the framework and therefore has no restrictions on possible applications. However, here are a few of the more common application types:

Desktop applications

— Applications, such as Microsoft Office, that have a familiar Windows look and feel about them. This is made simple by using the Windows Presentation Foundation (WPF) module of the .NET Framework, which is a library of

controls

(such as buttons, toolbars, menus, and so on) that you can use to build a Windows user interface (UI).

Windows Store applications

— Windows 8 introduced a new type of application, known as a Windows Store application. This type of application is designed primarily for touch devices, and it is usually run full-screen, with a minimum of clutter, and an emphasis on simplicity. You can create these applications in several ways, including using WPF.

Cloud/Web applications

— The .NET Framework includes a powerful system named ASP.NET, for generating web content dynamically, enabling personalization, security, and much more. Additionally, these applications can be hosted and accessed in the Cloud, for example on the Microsoft Azure platform.

Web APIs

— An ideal framework for building RESTful HTTP services that support a broad variety of clients, including mobile devices and browsers.

WCF services

— A way to create versatile distributed applications. Using WCF you can exchange virtually any data over local networks or the Internet, using the same simple syntax regardless of the language used to create a service or the system on which it resides.

Any of these types might also require some form of database access, which can be achieved using the ADO.NET (Active Data Objects .NET) section of the .NET Framework, through the ADO.NET Entity Framework, or through the LINQ (Language Integrated Query) capabilities of C#. Many other resources can be drawn on, such as tools for creating networking components, outputting graphics, performing complex mathematical tasks, and so on.

C# in this Book

The first part of this book deals with the syntax and usage of the C# language without too much emphasis on the .NET Framework. This is necessary because you can't use the .NET Framework at all without a firm grounding in C# programming. You'll start off even simpler, in fact, and leave the more involved topic of OOP until you've covered the basics. These are taught from first principles, assuming no programming knowledge at all.