Begin To Code With C#

Transcription

Beginto CodewithC#Rob Miles

PUBLISHED BYMicrosoft PressA Division of Microsoft CorporationOne Microsoft WayRedmond, Washington 98052-6399Copyright 2016 by Rob Miles.All rights reserved.No part of the contents of this book may be reproduced or transmitted in any form or byany means without the written permission of the publisher.Library of Congress Control Number: 2015942036ISBN: 978-1-5093-0115-7Printed and bound in the United States of America.First PrintingMicrosoft Press books are available through booksellers and distributors worldwide.If you need support related to this book, email Microsoft Press Support at mspinput@microsoft.com. Please tell us what you think of this book at http://aka.ms/tellpress.This book is provided “as-is” and expresses the authors’ views and opinions. The views,opinions, and information expressed in this book, including URL and other Internet websitereferences, may change without notice.Some examples depicted herein are provided for illustration only and are fictitious.No real association or connection is intended or should be inferred.Microsoft and the trademarks listed at http://www.microsoft.com on the “Trademarks”webpage are trademarks of the Microsoft group of companies. All other marks are theproperty of their respective owners.Acquisitions and Developmental Editor: Devon MusgraveProject Editor: John PierceEditorial Production: Rob Nance and John PierceTechnical Reviewer: Lance McCarthy; Technical Review services provided by ContentMaster, a member of CM Group, Ltd.Copyeditor: John PierceIndexer: Christina Palaia, Emerald Editorial ServicesCover: Twist Creative Seattle

To Mary

Contents at a glancePart 1: Programming fundamentalsChapter 1Starting out . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2Chapter 2What is programming? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18Chapter 3Writing programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42Chapter 4Working with data in a program . . . . . . . . . . . . . . . . . . . . 68Chapter 5Making decisions in a program . . . . . . . . . . . . . . . . . . . . . 100Chapter 6Repeating actions with loops . . . . . . . . . . . . . . . . . . . . . . . 134Chapter 7Using arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172Part 2: Advanced programmingChapter 8Using methods to simplify programs . . . . . . . . . . . . . . . . 212Chapter 9Creating structured data types . . . . . . . . . . . . . . . . . . . . 246Chapter 10Classes and references . . . . . . . . . . . . . . . . . . . . . . . . . . . . 288Chapter 11Making solutions with objects . . . . . . . . . . . . . . . . . . . . . 336Part 3: Making gamesivChapter 12What makes a game? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 374Chapter 13Creating gameplay . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 394Chapter 14Games and object hierarchies . . . . . . . . . . . . . . . . . . . . . . 416Chapter 15Games and software components . . . . . . . . . . . . . . . . . 446

This page intentionally left blank

ContentsIntroduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xviPart 1: Programming fundamentals1Starting out . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2Building a place to work . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4Getting the tools and demos . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4Using the tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5Visual Studio projects and solutions . . . . . . . . . . . . . . . . . . . . 6Running a program with Visual Studio . . . . . . . . . . . . . . . . . . 7Stopping a program running in Visual Studio . . . . . . . . . . . 10The MyProgram application . . . . . . . . . . . . . . . . . . . . . . . . . . 11What you have learned . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152What is programming? . . . . . . . . . . . . . . . . . . . . . . . 18What makes a programmer? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20Programming and party planning . . . . . . . . . . . . . . . . . . . . 20Give us feedbackTell us what you think of this book and helpMicrosoft improve our products for you. Thankyou!http://aka.ms/tellpressvi

Programming and problems . . . . . . . . . . . . . . . . . . . . . . . . . . 21Programmers and people . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22Computers as data processors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23Machines and computers and us . . . . . . . . . . . . . . . . . . . . . . 23Making programs work . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26Programs as data processors . . . . . . . . . . . . . . . . . . . . . . . . . 27Data and information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35What you have learned . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 393Writing programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42C# program structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44Identify resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44Start a class definition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45Declare the StartProgram method . . . . . . . . . . . . . . . . . . 46Set the title and display a message . . . . . . . . . . . . . . . . . . . 47Extra Snaps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50SpeakString . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50Creating new program files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52Extra Snaps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61Delay . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61SetTextColor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61SetTitleColor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62SetBackgroundColor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63Creating your own colors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63What you have learned . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 664Working with data in a program . . . . . . . . . . . . . . . 68Starting with variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70Variables and computer storage . . . . . . . . . . . . . . . . . . . . . . . 71vii

Declaring a variable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71Simple assignment statements . . . . . . . . . . . . . . . . . . . . . . . 73Using a variable in a program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74Assigning values in a declaration . . . . . . . . . . . . . . . . . . . . . 76Adding strings together . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77Working with numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80Whole numbers and real numbers . . . . . . . . . . . . . . . . . . . . 80Performing calculations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83Working with different types of data . . . . . . . . . . . . . . . . . . . . . . . . . . 85Converting numbers into text . . . . . . . . . . . . . . . . . . . . . . . . 86Whole numbers and real numbers in programs . . . . . . . . . . . . . . . . 89Variable types and expressions . . . . . . . . . . . . . . . . . . . . . . . 89Precision and accuracy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91Converting types by casting . . . . . . . . . . . . . . . . . . . . . . . . . . 92Using casting on operands in an expression . . . . . . . . . . . 93Types and errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94Extra Snaps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95Weather snaps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95ThrowDice . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96What you have learned . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 975Making decisions in a program . . . . . . . . . . . . . . . 100Understanding the Boolean type . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102Declaring a Boolean variable . . . . . . . . . . . . . . . . . . . . . . . . 102Boolean expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103Using if constructions and operators . . . . . . . . . . . . . . . . . . . . . . . . 104Relational operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106Equality operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107Comparing strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109Creating blocks of statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110viii

Local variables in blocks of code . . . . . . . . . . . . . . . . . . . . . . 111Creating complex conditions using logical operators . . . . . . . . . . . 113Working with logic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116Adding comments to make a program clearer . . . . . . . . . . . . . . . . . 117Funfair rides and programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119Reading in numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122Building logic using if conditions . . . . . . . . . . . . . . . . . . . 124Completing the program . . . . . . . . . . . . . . . . . . . . . . . . . . . 125Working with program assets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127Asset management in Visual Studio . . . . . . . . . . . . . . . . . . . 127Playing sound assets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128Displaying image content . . . . . . . . . . . . . . . . . . . . . . . . . . . 129What you have learned . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1326Repeating actions with loops . . . . . . . . . . . . . . . . . 134Using a loop to make a pizza picker . . . . . . . . . . . . . . . . . . . . . . . . . . 136Counting selections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136Displaying the totals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139Getting user options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139Adding a while loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142Performing input validation with a while loop . . . . . . . . . . . . . . . 149Using Visual Studio to follow the execution of your programs . . 151Counting in a loop to make a times-table tutor . . . . . . . . . . . . . . . . 157Using a for loop construction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160Breaking out of loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163Going back to the top of a loop by using continue . . . . . . . . . . 165Extra Snaps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168Voice input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168Secret data entry . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169What you have learned . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170ix

7Using arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172Have an ice cream . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174Storing the data in single variables . . . . . . . . . . . . . . . . . . . . 175Making an array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176Using an index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177Working with arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179Displaying the contents of the array by using afor loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184Displaying a user menu . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186Sorting an array using the Bubble Sort . . . . . . . . . . . . . . . . 187Finding the highest and lowest sales values . . . . . . . . . . . 194Working out the total and the average sales . . . . . . . . . . 196Completing the program . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198Multiple dimensions in arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199Using nested for loops to work with two-dimensionalarrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201Making test versions of programs . . . . . . . . . . . . . . . . . . . . 203Finding the length of an array dimension . . . . . . . . . . . . . 204Using arrays as lookup tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206What you have learned . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208Part 2: Advanced programming8Using methods to simplify programs . . . . . . . . . . 212What makes a method? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214Adding a method to a class . . . . . . . . . . . . . . . . . . . . . . . . . . 215Feeding information to methods by usingparameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217Returning values from method calls . . . . . . . . . . . . . . . . . . 222x

Making a tiny contacts app . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224Reading in contact details . . . . . . . . . . . . . . . . . . . . . . . . . . . 227Storing contact information . . . . . . . . . . . . . . . . . . . . . . . . . 228Using Windows local storage . . . . . . . . . . . . . . . . . . . . . . . 229Using reference parameters to deliver results from amethod call . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231Displaying the contact details . . . . . . . . . . . . . . . . . . . . . . . 237Adding IntelliSense comments to your methods . . . . . . . . . . . . . . 241What you have learned . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2439Creating structured data types . . . . . . . . . . . . . . . 246Storing music notes by using a structure . . . . . . . . . . . . . . . . . . . . . 248Creating and declaring a structure . . . . . . . . . . . . . . . . . . . 250Creating arrays of structure values . . . . . . . . . . . . . . . . . . . 252Structures and methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253Constructing structure values . . . . . . . . . . . . . . . . . . . . . . . 256Making a music recorder . . . . . . . . . . . . . . . . . . . . . . . . . . . . 260Creating preset arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262Objects and responsibilities: Making a SongNote play itself . . . 263Protecting values held in a structure . . . . . . . . . . . . . . . . . . . . . . . . . 264Making a drawing program with Snaps . . . . . . . . . . . . . . . . . . . . . . 267Drawing dots on the screen . . . . . . . . . . . . . . . . . . . . . . . . . 268Using the DrawDot Snap to draw a dot on the screen . 269The SnapsCoordinate structure . . . . . . . . . . . . . . . . . . . . 270Using the GetDraggedCoordinate Snap to detecta drawing position . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 272Using the SetDrawingColor Snap to set thedrawing color . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 274Using the ClearGraphics Snap to clear the screen . . . 276The SnapsColor structure . . . . . . . . . . . . . . . . . . . . . . . . . . 277Creating enumerated types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 278xi

Making decisions with the switch construction . . . . . . . . . . . . . . 280Extra Snaps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 282GetTappedCoordinate . . . . . . . . . . . . . . . . . . . . . . . . . . . . 282DrawLine . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283GetScreenSize . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 284PickImage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285What you have learned . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28510Classes and references . . . . . . . . . . . . . . . . . . . . . . . 288Making a time tracker . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 290Creating a structure to hold contact information . . . . . 290Using the this reference when working with objects . 292Managing lots of contacts . . . . . . . . . . . . . . . . . . . . . . . . . . . 294Making test data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 296Designing the Time Tracker user interface . . . . . . . . . . . . 297Structuring the Time Tracker program . . . . . . . . . . . . . . . . 298Creating a new contact . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 299Finding customer details . . . . . . . . . . . . . . . . . . . . . . . . . . . . 300Adding minutes to a contact . . . . . . . . . . . . . . . . . . . . . . . . 302Display a summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 304Structures and classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 306Sorting and structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 306Sorting and references . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 307Reference and value types . . . . . . . . . . . . . . . . . . . . . . . . . . 308References and assignments . . . . . . . . . . . . . . . . . . . . . . . . . . 311Classes and constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . 316Arrays of class references . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317From arrays to lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319Working through lists of data . . . . . . . . . . . . . . . . . . . . . . . . 321Lists and the index value . . . . . . . . . . . . . . . . . . . . . . . . . . . . 322xii

Lists of structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 322Storing data using JSON . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 323The Newtonsoft JSON library . . . . . . . . . . . . . . . . . . . . . . . . 324Storing and recovering lists . . . . . . . . . . . . . . . . . . . . . . . . . . 326Fetching data using XML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 329What you have learned . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33411Making solutions with objects . . . . . . . . . . . . . . . . 336Creating objects with integrity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 338Protecting data held inside an object . . . . . . . . . . . . . . . . 338Providing Get and Set methods for private data . . . . . 341Providing methods that reflect the use of an object . . . 343Using properties to manage access to data . . . . . . . . . . . . . . . . . . . 346Using properties to enforce business rules . . . . . . . . . . . . 349Managing the object construction process . . . . . . . . . . . . . . . . . . . . 351Catching and dealing with exceptions . . . . . . . . . . . . . . . . 353Creating user-friendly applications . . . . . . . . . . . . . . . . . . 355Saving drawings in files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 356SaveGraphicsImageToFileAsPNG . . . . . . . . . . . . . . . . . 357SaveGraphicsImageToLocalStoreAsPNG . . . . . . . . . . 358LoadGraphicsPNGImageFromLocalStore . . . . . . . . . . 358The DateTime structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 359Getting the current date and time . . . . . . . . . . . . . . . . . . . 360Fading date and time displays . . . . . . . . . . . . . . . . . . . . . . . 360Using the date and time to make a file name . . . . . . . . . 361Creating a Drawing class . . . . . . . . . . . . . . . . . . . . . . . . . . . . 362Creating a list of drawings . . . . . . . . . . . . . . . . . . . . . . . . . . . 364Making the drawing diary methods . . . . . . . . . . . . . . . . . . 365What you have learned . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 368xiii

Part 3: Making games12What makes a game? . . . . . . . . . . . . . . . . . . . . . . . . 374Creating a video game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 376Games and game engines . . . . . . . . . . . . . . . . . . . . . . . . . . . 376Games and sprites . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 378What you have learned . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39213Creating gameplay . . . . . . . . . . . . . . . . . . . . . . . . . . 394Creating a player-controlled paddle . . . . . . . . . . . . . . . . . . . . . . . . . 396Adding sound to games . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 401Displaying text in a game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 403Making a complete game . . . . . . . . . . . . . . . . . . . . . . . . . . . 408What you have learned . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41414Games and object hierarchies . . . . . . . . . . . . . . . . 416Games and objects: Space Rockets in Space . . . . . . . . . . . . . . . . . . 418Constructing a star sprite that moves . . . . . . . . . . . . . . . . 419Allowing methods to be overridden . . . . . . . . . . . . . . . . . . 427Creating a moving star field . . . . . . . . . . . . . . . . . . . . . . . . . 428Creating a rocket based on a MovingSprite . . . . . . . . . 430Adding some aliens . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 432Designing a class hierarchy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 440What you have learned . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 443xiv

15Games and software components . . . . . . . . . . . . 446Games and objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 448Creating cooperating objects . . . . . . . . . . . . . . . . . . . . . . . . 448Objects and state . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 456Interfaces and components . . . . . . . . . . . . . . . . . . . . . . . . . 465What you have learned . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 471Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 474xv

IntroductionI think that programming is the most creative thing you can learn howto do. If you learn to paint, you can make pictures. If you learn the violin,you can make music. But if you learn to program, you can create experiences that are entirely new (and you can make pictures and music too ifyou want to). Once you have started on the programming path, there’sno limit to where you can go. There are always new devices, technologies,and marketplaces where you can use your programming skills.You can think of this book as your first step on a journey to programmingenlightenment. The best journeys are undertaken with a destination inmind, and this one is no different. I’d like to describe the destination as“usefulness.” By the end of this book you won’t be the best programmerin the world (unless I retire, of course), but you will have enough skills andknowledge to write properly useful programs. And maybe you can haveat least one of your programs available worldwide for download from theMicrosoft Store.However, before we start off, I’d like to issue a small word of warning. Inthe same way that a guide would want to tell you about the lions, tigers,and crocodiles that you might encounter if you went on a safari adventure, I feel that I must let you know that our journey might not be allsmooth sailing. Programmers have to learn to think slightly differentlyabout problem solving because a computer just doesn’t work the sameway that we do. Humans can do complex things rather slowly. Computerscan do simple things really quickly. It is the job of the programmer to harness the simple abilities of the machine to solve complicated problems.This is what we are going to learn how to do.The key to success as a programmer is pretty much the same as for lots ofother endeavors. If you want to become a world-renowned violin player,you will have to practice a lot. The same is true for programming. Youwill have to spend quite a bit of time working on your programs to getcode-writing skills. But the good news is that, just as a violin player reallyenjoys making the instrument sing, making a computer do exactly whatyou want turns out to be a really satisfying experience. And it gets evenmore enjoyable when you see other people using programs that youhave written and finding them useful and fun to use.xviIntroduction

How this book fits togetherI’ve organized this book in four parts. Each part builds on the previousone with the aim of turning you into a successful programmer. We startoff considering the low-level programing instructions that programs useto tell the computer what to do, and we finish by looking at professionalsoftware practices.Part 1: Coding fundamentalsThe first part gets you started. It points you to where you will install anduse the programming tools that you will need, and it introduces you tothe fundamental elements of the C# programming language that areused by all programs.Part 2: Advanced programmingPart 2 describes the features of the C# programming language that areused to create more complex applications. It shows you how to breaklarge programs into smaller elements and how you can create customdata types that reflect the specific problem being solved. You’ll alsofind out how programs can maintain data in storage when they are notrunning.Part 3: Making gamesMaking games is great fun. And it turns out that it is also a great way tolearn how to use object-oriented programming techniques. In this part,you’ll build some playable games and at the same time learn the fundamentals of how to extend programming objects through inheritance andcomponent-based software design.Part 4: Creating applicationsPart 4 is where you find out how to create fully fledged applications.You’ll discover how to design graphical user interfaces and how to connect program code to the elements on the display. You’ll also learn howmodern applications are structured. Part 4 doesn’t appear in this printedIntroductionxvii

book but is available as an ebook, free to download from this book’swebpage at https://aka.ms/BeginCodeCSharp/downloads.How you will learnIn each chapter, I will tell you a bit more about programming. I’ll showyou how to do something, and then I’ll invite you make something ofyour own by using what you’ve learned. You’ll never be more than a pageor so away from doing something or making something unique andpersonal. In each chapter we will use Snaps, prebuilt bits of functionalitythat I’ll show you how to use. After that, it’s up to you to make somethingamazing!You can read the book straight through if you like, but you’ll learn muchmore if you slow down and work with the practical parts along the way.This book can’t really teach you how to program, any more than a bookabout bicycles can teach you how to ride a bike. You have to put in thetime and practice to learn how to do it. But this book will give you theknowledge and confidence to try your hand at programming, and it willalso be around to help you if your programming doesn’t turn out as youexpected. Here are the elements in the book that will help you reallylearn, by doing!MAKE SOMETHING HAPPENYes, the best way to learn things is by doing, so you’ll find “Make SomethingHappen” elements throughout the text. These elements offer ways for you topractice your programming skills. Each of them starts with an example andthen introduces some steps you can try on your own. Everything you create willrun on a Windows PC, tablet, or phone. You can even publish your creations tothe whole wide world via the Windows Store.CODE ANALYSISA great way to learn how to program is by looking at code written by otherpeople and working out what it does (and sometimes why it doesn’t do whatit should). In this book’s “Code Analysis” challenges, you’ll use your deductive skills to figure out the behavior of a program, fix bugs, and suggestimprovements.xviiiIntroduction

WHAT COULD GO WRONGIf you don’t already know that programs can fail, you will learn this hard lessonvery soon after you start writing your first program. To help you deal with thisin advance, I’ve included “What Could Go Wrong?” elements, which anticipate problems you might have and provide solutions to those problems. Forexample, when I introduce something new, I’ll sometimes spend some timeconsidering how it can fail and what you need to worry about when you use thenew feature.PROGRAMMER’S POINTSI’ve spent a lot of my time teaching programming. But I’ve also written manyp

the fundamental elements of the C# programming language that are Part 2 describes the features of the C# programming language that are large pro