Begin to Code with C#
Rob Miles
PUBLISHED BY
Microsoft Press
A Division of Microsoft Corporation
One Microsoft Way
Redmond, Washington 98052-6399
Copyright 2016 by Rob Miles.
All rights reserved.
No part of the contents of this book may be reproduced or transmitted in any form or by any means without the written permission of the publisher.
Library of Congress Control Number: 2015942036
ISBN: 978-1-5093-0115-7
Printed and bound in the United States of America.
First Printing
Microsoft Press books are available through booksellers and distributors worldwide. If you need support related to this book, email Microsoft Press Support at .
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 website references, 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 the property of their respective owners.
Acquisitions and Developmental Editor: Devon Musgrave
Project Editor: John Pierce
Editorial Production: Rob Nance and John Pierce
Technical Reviewer: Lance McCarthy; Technical Review services provided by Content Master, a member of CM Group, Ltd.
Copyeditor: John Pierce
Indexer: Christina Palaia, Emerald Editorial Services
Cover: Twist Creative Seattle
To Mary
Introduction
I think that programming is the most creative thing you can learn how to 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 if you want to). Once you have started on the programming path, theres no 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 programming enlightenment. The best journeys are undertaken with a destination in mind, and this one is no different. Id like to describe the destination as usefulness. By the end of this book you wont be the best programmer in the world (unless I retire, of course), but you will have enough skills and knowledge to write properly useful programs. And maybe you can have at least one of your programs available worldwide for download from the Microsoft Store.
However, before we start off, Id like to issue a small word of warning. In the 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 all smooth sailing. Programmers have to learn to think slightly differently about problem solving because a computer just doesnt work the same way that we do. Humans can do complex things rather slowly. Computers can 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 of other endeavors. If you want to become a world-renowned violin player, you will have to practice a lot. The same is true for programming. You will have to spend quite a bit of time working on your programs to get code-writing skills. But the good news is that, just as a violin player really enjoys making the instrument sing, making a computer do exactly what you want turns out to be a really satisfying experience. And it gets even more enjoyable when you see other people using programs that you have written and finding them useful and fun to use.
How this book fits together
Ive organized this book in four parts. Each part builds on the previous one with the aim of turning you into a successful programmer. We start off considering the low-level programing instructions that programs use to tell the computer what to do, and we finish by looking at professional software practices.
Part 1: Coding fundamentals
The first part gets you started. It points you to where you will install and use the programming tools that you will need, and it introduces you to the fundamental elements of the C# programming language that are used by all programs.
Part 2: Advanced programming
Part 2 describes the features of the C# programming language that are used to create more complex applications. It shows you how to break large programs into smaller elements and how you can create custom data types that reflect the specific problem being solved. Youll also find out how programs can maintain data in storage when they are not running.
Part 3: Making games
Making games is great fun. And it turns out that it is also a great way to learn how to use object-oriented programming techniques. In this part, youll build some playable games and at the same time learn the fundamentals of how to extend programming objects through inheritance and component-based software design.
Part 4: Creating applications
Part 4 is where you find out how to create fully fledged applications. Youll discover how to design graphical user interfaces and how to connect program code to the elements on the display. Youll also learn how modern applications are structured. Part 4 doesnt appear in this printed book but is available as an ebook, free to download from this books webpage at https://aka.ms/BeginCodeCSharp/downloads.
How you will learn
In each chapter, I will tell you a bit more about programming. Ill show you how to do something, and then Ill invite you make something of your own by using what youve learned. Youll never be more than a page or so away from doing something or making something unique and personal. In each chapter we will use Snaps, prebuilt bits of functionality that Ill show you how to use. After that, its up to you to make something amazing!
You can read the book straight through if you like, but youll learn much more if you slow down and work with the practical parts along the way. This book cant really teach you how to program, any more than a book about bicycles can teach you how to ride a bike. You have to put in the time and practice to learn how to do it. But this book will give you the knowledge and confidence to try your hand at programming, and it will also be around to help you if your programming doesnt turn out as you expected. Here are the elements in the book that will help you really learn, by doing!
Make Something Happen
Yes, the best way to learn things is by doing, so youll find Make Something Happen elements throughout the text. These elements offer ways for you to practice your programming skills. Each of them starts with an example and then introduces some steps you can try on your own. Everything you create will run on a Windows PC, tablet, or phone. You can even publish your creations to the whole wide world via the Windows Store.