Head First C# is a complete learning experience for learning how to program with C#, XAML, the .NET Framework, and Visual Studio. Fun and highly visual, this introduction to C# is designed to keep you engaged and entertained from first page to last.
You’ll build a fully functional video game in the opening chapter, and then learn how to use classes and object-oriented programming, draw graphics and animation, and query data with LINQ and serialize it to files. And you'll do it all by creating games, solving puzzles, and doing hands-on projects. By the time you're done, you'll be a solid C# programmer—and you'll have a great time along the way!
Create a fun arcade game in the first chapter, and build games and other projects throughout the book
Learn how to use XAML to design attractive and interactive pages and windows
Build modern Windows Store apps using the latest Microsoft technology
Learn WPF (Windows Presentation Foundation) using the downloadable WPF Learner's Guide
Using the Model-View-ViewModel (MVVM) pattern to create robust architecture
Build a bonus Windows Phone project and run it in the Visual Studio Windows Phone emulator
Projects in the book work with all editions of Visual Studio, including the free Express editions.
Chapter 1 Start Building With c#: Build something cool, fast!
Why you should learn C#
C# and the Visual Studio IDE make lots of things easy
What you do in Visual Studio...
What Visual Studio does for you...
Only you can help save the Earth
Here’s what you’re going to build
Start with a blank application
Set up the grid for your page
Add controls to your grid
Use properties to change how the controls look
Controls make the game work
You’ve set the stage for the game
What you’ll do next
Add a method that does something
Fill in the code for your method
Finish the method and run your program
Here’s what you’ve done so far
Add timers to manage the gameplay
Make the Start button work
Run the program to see your progress
Add code to make your controls interact with the player
Dragging humans onto enemies ends the game
Your game is now playable
Make your enemies look like aliens
Add a splash screen and a tile
Publish your app
Use the Remote Debugger to sideload your app
Start remote debugging
Chapter 2 It’s all Just Code: Under the hood
When you’re doing this...
...the IDE does this
Where programs come from
The IDE helps you code
Anatomy of a program
Two classes can be in the same namespace
Your programs use variables to work with data
C# uses familiar math symbols
Use the debugger to see your variables change
Loops perform an action over and over
if/else statements make decisions
Build an app from the ground up
Make each button do something
Set up conditions and see if they’re true
Windows Desktop apps are easy to build
Rebuild your app for Windows Desktop
Your desktop app knows where to start
You can change your program’s entry point
When you change things in the IDE, you’re also changing your code
Chapter 3 Objects: Get Oriented!: Making code make sense
How Mike thinks about his problems
How Mike’s car navigation system thinks about his problems
Mike’s Navigator class has methods to set and modify routes
Use what you’ve learned to build a program that uses a class
Mike gets an idea
Mike can use objects to solve his problem
You use a class to build an object
When you create a new object from a class, it’s called an instance of that class
A better solution...brought to you by objects!
An instance uses fields to keep track of things
Let’s create some instances!
Thanks for the memory
What’s on your program’s mind
You can use class and method names to make your code intuitive
Give your classes a natural structure
Class diagrams help you organize your classes so they make sense
Build a class to work with some guys
Create a project for your guys
Build a form to interact with the guys
There’s an easier way to initialize objects
A few ideas for designing intuitive classes
Chapter 4 Types and References: It’s 10:00. Do you know where your data is?
The variable’s type determines what kind of data it can store
A variable is like a data to-go cup
10 pounds of data in a 5-pound bag
Even when a number is the right size, you can’t just assign it to any variable
When you cast a value that’s too big, C# will adjust it automatically
C# does some casting automatically
When you call a method, the arguments must be compatible with the types of the parameters
Debug the mileage calculator
Combining = with an operator
Objects use variables, too
Refer to your objects with reference variables
References are like labels for your object
If there aren’t any more references, your object gets garbage-collected
Multiple references and their side effects
Two references means TWO ways to change an object’s data
A special case: arrays
Arrays can contain a bunch of reference variables, too
Welcome to Sloppy Joe’s Budget House o’ Discount Sandwiches!
Objects use references to talk to each other
Where no object has gone before
Build a typing game
Controls are objects, just like any other object
Chapter C# Lab A Day at the Races
The Spec: Build a Racetrack Simulator
You’ll need three classes and a form
Here’s your application architecture
Here’s what your GUI should look like
The Finished Product
Chapter 5 Encapsulation: Keep your privates... private
Kathleen is an event planner
What does the estimator do?
You’re going to build a program for Kathleen
Kathleen’s test drive
Each option should be calculated individually
It’s easy to accidentally misuse your objects
Encapsulation means keeping some of the data in a class private
Use encapsulation to control access to your class’s methods and fields
But is the RealName field REALLY protected?
Private fields and methods can only be accessed from inside the class
A few ideas for encapsulating classes
Encapsulation keeps your data pristine
Properties make encapsulation easier
Build an application to test the Farmer class
Use automatic properties to finish the class
What if we want to change the feed multiplier?
Use a constructor to initialize private fields
Chapter 6 Inheritance: Your object’s family tree
Kathleen does birthday parties, too
We need a BirthdayParty class
Build the Party Planner version 2.0
One more thing...can you add a $100 fee for parties over 12?
When your classes use inheritance, you only need to write your code once
Build up your class model by starting general and getting more specific
How would you design a zoo simulator?
Use inheritance to avoid duplicate code in subclasses
Different animals make different noises
Think about how to group the animals
Create the class hierarchy
Every subclass extends its base class
Use a colon to inherit from a base class
We know that inheritance adds the base class fields, properties, and methods to the subclass...
A subclass can override methods to change or replace methods it inherited
Any place where you can use a base class, you can use one of its subclasses instead
A subclass can hide methods in the superclass
Use the override and virtual keywords to inherit behavior
A subclass can access its base class using the base keyword
When a base class has a constructor, your subclass needs one, too
Now you’re ready to finish the job for Kathleen!
Build a beehive management system
How you’ll build the beehive management system
Use inheritance to extend the bee management system
Chapter 7 Interfaces and Abstract Classes: Making classes keep their promises
Let’s get back to bee-sics
We can use inheritance to create classes for different types of bees
An interface tells a class that it must implement certain methods and properties
Use the interface keyword to define an interface
Now you can create an instance of NectarStinger that does both jobs
Classes that implement interfaces have to include ALL of the interface’s methods
Get a little practice using interfaces
You can’t instantiate an interface, but you can reference an interface
Interface references work just like object references
You can find out if a class implements a certain interface with “is”
Interfaces can inherit from other interfaces
The RoboBee 4000 can do a worker bee’s job without using valuable honey
is tells you what an object implements; as tells the compiler how to treat your object
A CoffeeMaker is also an Appliance
Upcasting works with both objects and interfaces
Downcasting lets you turn your appliance back into a coffee maker
Upcasting and downcasting work with interfaces, too
There’s more than just public and private
Access modifiers change visibility
Some classes should never be instantiated
An abstract class is like a cross between a class and an interface
Like we said, some classes should never be instantiated
An abstract method doesn’t have a body
The Deadly Diamond of Death!
Polymorphism means that one object can take many different forms
Chapter 8 Enums and Collections: Storing Lots of Data
Strings don’t always work for storing categories of data
Enums let you work with a set of valid values
Enums let you represent numbers with names
We could use an array to create a deck of cards...
Arrays are hard to work with
Lists make it easy to store collections of...anything
Lists are more flexible than arrays
Lists shrink and grow dynamically
Generics can store any type
Collection initializers are similar to object initializers
Let’s create a List of Ducks
Lists are easy, but SORTING can be tricky
IComparable<Duck> helps your list sort its ducks
Use IComparer to tell your List how to sort
Create an instance of your comparer object
IComparer can do complex comparisons
Overriding a ToString() method lets an object describe itself
Update your foreach loops to let your Ducks and Cards print themselves
You can upcast an entire list using IEnumerable
You can build your own overloaded methods
Use a dictionary to store keys and values
The dictionary functionality rundown
Build a program that uses a dictionary
And yet MORE collection types...
A queue is FIFO—First In, First Out
A stack is LIFO—Last In, First Out
Chapter 9 Reading and Writing Files: Save the last byte for me!
.NET uses streams to read and write data
Different streams read and write different things
A FileStream reads and writes bytes to a file
Write text to a file in three simple steps
The Swindler launches another diabolical plan
Reading and writing using two objects
Data can go through more than one stream
Use built-in objects to pop up standard dialog boxes
Dialog boxes are just another WinForms control
Dialog boxes are objects, too
Use the built-in File and Directory classes to work with files and directories
Use file dialogs to open and save files (all with just a few lines of code)
IDisposable makes sure your objects are disposed of properly
Avoid filesystem errors with using statements
Trouble at work
Writing files usually involves making a lot of decisions
Use a switch statement to choose the right option
Use a switch statement to let your deck of cards read from a file or write itself out to one
Add an overloaded Deck() constructor that reads a deck of cards in from a file
What happens to an object when it’s serialized?
But what exactly IS an object’s state? What needs to be saved?
When an object is serialized, all of the objects it refers to get serialized, too...
Serialization lets you read or write a whole object graph all at once
If you want your class to be serializable, mark it with the [Serializable] attribute
Let’s serialize and deserialize a deck of cards
.NET uses Unicode to store characters and text
C# can use byte arrays to move data around
Use a BinaryWriter to write binary data
You can read and write serialized files manually, too
Find where the files differ, and use that information to alter them
Working with binary files can be tricky
Use file streams to build a hex dumper
StreamReader and StreamWriter will do just fine (for now)
Use Stream.Read() to read bytes from a stream
Chapter C# Lab The Quest
The spec: build an adventure game
The design: building the form
The architecture: using the objects
Gameplay concerns are separated into the Game object
Building the Game class
Finding common behavior: movement
The Mover class source code
The Player class keeps track of the player
Write the Move() method for the Player
Add an Attack() method, too
Bats, ghosts, and ghouls inherit from the Enemy class
Write the different Enemy subclasses
Weapon inherits from Mover; each weapon inherits from Weapon
Different weapons attack in different ways
Potions implement the IPotion interface
The form brings it all together
The form’s UpdateCharacters() method moves the PictureBoxes into position
The fun’s just beginning!
Chapter 10 Designing Windows Store Apps with Xaml: Taking your apps to the next level
Brian’s running Windows 8
Windows Forms use an object graph set up by the IDE
Use the IDE to explore the object graph
Windows Store apps use XAML to create UI objects
Redesign the Go Fish! form as a Windows Store app page
Page layout starts with controls
Rows and columns can resize to match the page size
Use the grid system to lay out app pages
Data binding connects your XAML pages to your classes
XAML controls can contain text...and more
Use data binding to build Sloppy Joe a better menu
Use static resources to declare your objects in XAML
Bestselling O'Reilly authors Jennifer Greene and Andrew Stellman have been building software and writing about software engineering together since they first met in 1998. Their first book, Applied Software Project Management, was published by O’Reilly in 2005. Other Stellman and Greene books for O’Reilly include Beautiful Teams (2009), and their first book in the Head First series, Head First PMP (2007). They founded Stellman & Greene Consulting in 2003 to build a really neat software project for scientists studying herbicide exposure in Vietnam vets. In addition to building software and writing books, they’ve provided training and consulted for companies and spoken at conferences and meetings of software engineers, architects and project managers.
Jenny studied philosophy in college but, like everyone else in the field, couldn’t find a job doing it. Luckily, she’s a great software engineer, so she started out working at an online service, and that’s the first time she really got a good sense of what good software development looked like. She moved to New York in 1998 to work on software quality at a financial software company. She’s managed a teams of developers, testers and PMs on software projects in media and finance since then. She’s traveled all over the world to work with different software teams and build all kinds of cool projects.
Andrew, despite being raised a New Yorker, has lived in Minneapolis, Geneva, and Pittsburgh... twice. The first time was when he graduated from Carnegie Mellon’s School of Computer Science, and then again when he and Jenny were starting their consulting business and writing their first book for O’Reilly. He and Jenny first worked together at a company on Wall Street that built financial software, where he was managing a team of programmers. Over the years he’s been a Vice President at a major investment bank, architected large-scale real-time back end systems, managed large international software teams, and consulted for companies, schools, and organizations, including Microsoft, the National Bureau of Economic Research, and MIT. He’s had the privilege working with some pretty amazing programmers during that time, and likes to think that he’s learned a few things from them.
Comments about oreilly Head First C#, 3rd Edition:
I have been impressed by these books as they make tough subjects interesting. I had bought the previous edition as an e book and was struggling with the format (finding the vital code text very small and a bit awkward to navigate). So I thought I would update to the latest edition on paper also so I could use it as a reference more easily. Unfortunately the first few chapters are entirely devoted to Windows 8 format. There is a PDF which you are encouraged to download that allows you to build the same projects on earlier operating systems, but this does mean I have ended up reading from a screen again, just what I was trying to avoid by buying the book. It even says in the Appendix that it would be a good idea for Windows 8 users to go back and build the projects using the older (WPF) way. It would have seemed a better idea to make that the content of the book and release a PDF for Windows 8 users, rather than exclude the many of us who have not updated yet.
Comments about oreilly Head First C#, 3rd Edition:
I have read other Head First books before and this one was very much in line with those others. For those who are not familiar, the Head First Labs concept is to turn the reading experience around and deliver the information with a lot of other goodies to keep your brain engaged by not having line after line of text blurring into one another. For some people, myself included, this is a fantastic delivery mechanism. Their Head First Statistics book probably helped me with the class more than the textbook did.
This book is no exception. It is well researched and well written. The examples are entertaining and keep you engaged with the book and with the concepts as they build layer by layer until you have a very solid understanding of the basics of C#. This is a book geared to someone picking the language up for the first time. My only complaint is that they are so centered on Windows 8 and the Windows App Store that they had to put out a separate pdf file for people who don't have or don't develop for Window 8. For me, I had not yet made the jump, so I needed the conversions in places. However, the book was so new, it wasn't immediately available.
I would have preferred them target the .NET 4 and 4.5 frameworks from a Windows 7 perspective only because it's more in use in the enterprise where many programmers are working. They could also use this to isolate the 4.5 improvements a little better and then demonstrate how those improvements are enhanced in the Windows 8 experience. However, I also admit that being able to build a Windows Phone application with C# and XAML was a fun exercise.
Overall, this book is perfect for the slight to moderate ADHD programmer/student who wants to learn, not want to be boredthumb_hfcs, and tries to cram as much as possible in the shortest time available. The whole "series" is fantastic and well worth anyone's time to use as a good learning tool.
Bottom Line Yes, I would recommend this to a friend