Synopses & Reviews
Do you want to learn C#? Programmers around the world have learned that C# lets them design great-looking programs and build them fast. With C#, you ve got a powerful programming language and a valuable tool at your fingertips. And with the Visual Studio IDE, you ll never have to spend hours writing obscure code just to get a button working. C#, Visual Studio and .NET take care of the grunt-work, and let you focus on the interesting parts of getting your programs written. Sound appealing?
Unlike other C# books, which just show you examples and expect you to just memorize them and move on, Head First C# gets you writing code from the beginning. You're given the tools you need, and then you're guided through fun and engaging programming projects. You'll build programs to play a card game, explore a house, and help lazy programmers manage their sick day excuses. But it's not all fun and games: you'll build business applications too, like a contact database and a program to help a party planner estimate her dinner parties. You'll build a dungeon role-playing game and a fully animated, colorful simulation of a beehive. And by the end of the book, you'll build a fast-paced, full-featured retro Invaders arcade game.
Make no mistake: by the time you're done with Head First C#, you'll be able to build full-scale, complex, and highly visual programs. And you'll have all of the C# tools you need to tackle almost any programming problem that comes your way.
Head First C# is built for your brain, using the revolutionary approach that was pioneered by the highly acclaimed and popular Head First series. You'll never get that bored, eyes glazed over feeling from Head First C#, because it guides you through one challenging project after another until, by the end of the book, you're a C# rock star
Here's what you'll learn: Core C# programming concepts How to use the Visual Studio 2008 IDE to build, debug and run your programs Important .NET 3.5 features, including generic collections, Windows forms, GDI+ graphics, streams, serialization and more Using object oriented programming concepts to help you build well-designed programs How to build robust applications with good error handling The latest C# 3.0 features, including LINQ, object and collection initializers, automatic properties, extension methods and more
Throughout the book, you'll confront and conquer advanced C# concepts. Some of the most mysterious ideas are demystified and explained with clear examples: how Unicode works, events and delegates, references versus value types, the stack versus the heap, what's really going on with garbage collection, and more.
Thousands of readers have learned C# using this innovative book, including: Beginning programmers who want to learn programming from the ground up More advanced programmers who are proficient in another language (like Visual Basic, Java, SQL, FoxPro) and want to add C# to their toolbox Programmers who understand basic C# syntax, but are still looking to get a handle on how objects work Anyone who's tried to learn C#, but had to deal with books full of dull examples and nothing but boring console applications Lots of people who just want to learn how to build cool games
Head First C# is built to work with any version of Visual Studio 2008, including the free express edition. (It can also can be used with any version of Visual Studio 2005.)
You want to learn C# programming stuff don't you? Everyone's doing it. Come on, it'll be fun. No, really, it will be fun... Oh, you're thinking this is another mind-numbingly dry technical book that will bore you to tears. We know you've suffered through tedious technical books before. This is not one of those books. We'll take you on a journey and show you C# in a way that it's never been seen before, from creating your first program, to learning better C# coding skills to avoid the common errors that besiege students learning a new language. From objects to garbage (collection that is), and from errors to interactions, we'll show you the ins-and-outs of C# to engage and entertain your brain.
Head First C# is a complete learning experience for object-oriented programming, C#, and the Visual Studio IDE. Built for your brain, this book covers C# 3.0 and Visual Studio 2008, and teaches everything from language fundamentals to advanced topics including garbage collection, extension methods, and double-buffered animation. You'll also master C#'s hottest and newest syntax, LINQ, for querying SQL databases, .NET collections, and XML documents. By the time you're through, you'll be a proficient C# programmer, designing and coding large-scale applications.
Every few chapters you will come across a lab that lets you apply what you've learned up to that point. Each lab is designed to simulate a professional programming task, increasing in complexity until-at last-you build a working Invaders game, complete with shooting ships, aliens descending while firing, and an animated death sequence for unlucky starfighters. This remarkably engaging book will have you going from zero to 60 with C# in no time flat.
About the Author
Andrew, despite being raised a New Yorker, has lived in Minneapolis, Geneva, and Pittsburgh... twice. The first time was when he graduated from Carnegie Mellons School of Computer Science, and then again when he and Jenny were starting their consulting business and writing their first book for OReilly. 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 hes 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. Hes had the privilege working with some pretty amazing programmers during that time, and likes to think that hes learned a few things from them.
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 OReilly in 2005. Other Stellman and Greene books for OReilly 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, theyve 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, couldnt find a job doing it. Luckily, shes a great software engineer, so she started out working at an online service, and thats 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. Shes managed a teams of developers, testers and PMs on software projects in media and finance since then. Shes traveled all over the world to work with different software teams and build all kinds of cool projects.
Table of Contents
; Advance Praise for Head First C#; Praise for other Head First books; ; How to Use this Book: Intro; Who is this book for?; Who should probably back away from this book?; We know what you're thinking.; And we know what your brain is thinking.; Metacognition: thinking about thinking; Here's what WE did:; Here's what YOU can do to bend your brain into submission; What you need for this book:; Read me; The technical review team; Acknowledgments; Safari® Books Online; Chapter 1: Get Productive With c#: Visual Applications, in 10 minutes or less; 1.1 Why you should learn C#; 1.2 C# and the Visual Studio IDE make lots of things easy; 1.3 Help the CEO go paperless; 1.4 Get to know your users' needs before you start building your program; 1.5 Here's what you're going to build; 1.6 What you do in Visual Studio...; 1.7 What Visual Studio does for you...; 1.8 Develop the user interface; 1.9 Visual Studio, behind the scenes; 1.10 Add to the auto-generated code; 1.11 You can already run your application; 1.12 Where are my files?; 1.13 Here's what we've done so far; 1.14 We need a database to store our information; 1.15 The IDE created a database; 1.16 SQL is its own language; 1.17 Creating the table for the Contact List; 1.18 The blanks on contact card are columns in our People table; 1.19 Finish building the table; 1.20 Diagram your data; 1.21 Insert your card data into the database; 1.22 Connect your form to your database objects with a data source; 1.23 Add database-driven controls to your form; 1.24 Good programs are intuitive to use; 1.25 Test drive; 1.26 How to turn YOUR application into EVERYONE'S application; 1.27 Give your users the application; 1.28 You built a complete data-driven application; 1.29 You built a complete data-driven application; 1.30 CSharpcross; 1.31 CSharpcross Solution; Chapter 2: It's All Just Code: Under the Hood; 2.1 When you're doing this...; 2.2 ...the IDE does this; 2.3 Where programs come from; 2.4 The IDE helps you code; 2.5 When you change things in the IDE, you're also changing your code; 2.6 Anatomy of a program; 2.7 Your program knows where to start; 2.8 You can change your program's entry point; 2.9 Two classes can be in the same namespace; 2.10 Your programs use variables to work with data; 2.11 C# uses familiar math symbols; 2.12 Loops perform an action over and over again; 2.13 Time to start coding; 2.14 if/else statements make decisions; 2.15 Set up conditions and see if they're true; 2.16 Csharpcross; 2.17 Csharpcross Solution; Chapter 3: Objects: Get Oriented!: Making Code Make Sense; 3.1 How Mike thinks about his problems; 3.2 How Mike's car navigation system thinks about his problems; 3.3 Mike's Navigator class has methods to set and modify routes; 3.4 Use what you've learned to build a simple application; 3.5 Mike gets an idea; 3.6 Mike can use objects to solve his problem; 3.7 You use a class to build an object; 3.8 When you create a new object from a class, it's called an instance of that class.; 3.9 A better solution... brought to you by objects!; 3.10 An instance uses fields to keep track of things; 3.11 Let's create some instances!; 3.12 Thanks for the memory; 3.13 What's on your program's mind; 3.14 You can use class and method names to make your code intuitive; 3.15 Give your classes a natural structure; 3.16 Class diagrams help you organize your classes so they make sense; 3.17 Build a class to work with some guys; 3.18 Create a project for your guys; 3.19 Build a form to interact with the guys; 3.20 There's an even easier way to initialize objects; 3.21 A few ideas for designing intuitive classes; 3.22 Objectcross; 3.23 Objectcross Solution; Chapter 4: Types and References: It's 10:00. Do you know where your data is?; 4.1 The variable's type determines what kind of data it can store; 4.2 A variable is like a data to-go cup; 4.3 10 pounds of data in a 5 pound bag; 4.4 Even when a number is the right size, you can't just assign it to any variable; 4.5 When you cast a value that's too big, C# will adjust it automatically; 4.6 C# does some casting automatically; 4.7 When you call a method, the variables must match the types of the parameters; 4.8 Combining = with an operator; 4.9 Objects use variables, too; 4.10 Refer to your objects with reference variables; 4.11 References are like labels for your object; 4.12 If there aren't any more references, your object gets garbage collected; 4.13 Typecross; 4.14 Multiple references and their side effects; 4.15 Two references means TWO ways to change an object's data; 4.16 A special case: arrays; 4.17 Arrays can contain a bunch of reference variables, too; 4.18 Welcome to Sloppy Joe's Budget House o' Discount Sandwiches!; 4.19 Objects use references to talk to each other; 4.20 Where no object has gone before; 4.21 Typecross Solution; C# Lab A Day at the Races; Chapter 5: Encapsulation: Keep your privates... private; 5.1 Kathleen is an event planner; 5.2 What does the estimator do?; 5.3 Kathleen's Test Drive; 5.4 Each option should be calculated individually; 5.5 It's easy to accidentally misuse your objects; 5.6 Encapsulation means keeping some of the data in a class private; 5.7 Use encapsulation to control access to your class's methods and fields; 5.8 But is the realName field REALLY protected?; 5.9 Private fields and methods can only be accessed from inside the class; 5.10 A few ideas for encapsulating classes; 5.11 Encapsulation keeps your data pristine; 5.12 Properties make encapsulation easier; 5.13 Build an application to test the Farmer class; 5.14 Use automatic properties to finish the class; 5.15 What if we want to change the feed multiplier?; 5.16 Use a constructor to initialize private fields; Chapter 6: Inheritance: Your object's family tree; 6.1 Kathleen does birthday parties, too; 6.2 We need a BirthdayParty class; 6.3 One more thing... can you add a $100 fee for parties over 12?; 6.4 When your classes use inheritance, you only need to write your code once; 6.5 Build up your class model by starting general and getting more specific; 6.6 How would you design a zoo simulator?; 6.7 Use inheritance to avoid duplicate code in subclasses; 6.8 Different animals make different noises; 6.9 Think about how to group the animals; 6.10 Create the class hierarchy; 6.11 Every subclass extends its base class; 6.12 Use a colon to inherit from a base class; 6.13 We know that inheritance adds the base class fields, properties, and methods to the subclass...; 6.14 A subclass can override methods to change or replace methods it inherited; 6.15 Any place where you can use a base class, you can use one of its subclasses instead; 6.16 A subclass can access its base class using the base keyword; 6.17 When a base class has a constructor, your subclass needs one too; 6.18 Now you're ready to finish the job for Kathleen!; 6.19 Build a beehive management system; 6.20 First you'll build the basic system; 6.21 Use inheritance to extend the bee management system; Chapter 7: Interfaces and Abstract Classes: Making classes keep their promises; 7.1 Let's get back to bee-sics; 7.2 We can use inheritance to create classes for different types of bees; 7.3 An interface tells a class that it must implement certain methods and properties; 7.4 Use the interface keyword to define an interface; 7.5 Now you can create an instance of NectarStinger that does both jobs; 7.6 Classes that implement interfaces have to include ALL of the interface's methods; 7.7 Get a little practice using interfaces; 7.8 You can't instantiate an interface, but you can reference an interface; 7.9 Interface references work just like object references; 7.10 You can find out if a class implements a certain interface with "is"; 7.11 Interfaces can inherit from other interfaces; 7.12 The RoboBee 4000 can do a worker bee's job without using valuable honey; 7.13 is tells you what an object implements, as tells the compiler how to treat your object; 7.14 A CoffeeMaker is also an Appliance; 7.15 Upcasting works with both objects and interfaces; 7.16 Downcasting lets you turn your appliance back into a coffee maker; 7.17 Upcasting and downcasting work with interfaces, too; 7.18 There's more than just public and private; 7.19 Access modifiers change scope; 7.20 Some classes should never be instantiated; 7.21 An abstract class is like a cross between a class and an interface; 7.22 Like we said, some classes should never be instantiated; 7.23 An abstract method doesn't have a body; 7.24 Polymorphism means that one object can take many different forms; Chapter 8: Enums and Collections: Storing lots of data; 8.1 Strings don't always work for storing categories of data; 8.2 Enums let you enumerate a set of valid values; 8.3 Enums let you represent numbers with names; 8.4 We could use an array to create a deck of cards...; 8.5 Arrays are hard to work with; 8.6 Lists make it easy to store collections of... anything; 8.7 Lists are more flexible than arrays; 8.8 Lists shrink and grow dynamically; 8.9 List objects can store any type; 8.10 Collection initializers work just like object initializers; 8.11 Let's create a list of Ducks; 8.12 Lists are easy, but SORTING can be tricky; 8.13 Two ways to sort your ducks; 8.14 Use IComparer to tell your List how to sort; 8.15 Create an instance of your comparer object; 8.16 IComparer can do complex comparisons; 8.17 Use a dictionary to store keys and values; 8.18 The Dictionary Functionality Rundown; 8.19 Your key and value can be different types, too; 8.20 You can build your own overloaded methods; 8.21 And yet MORE collection types...; 8.22 A queue is FIFO -- First In, First Out; 8.23 A stack is LIFO -- Last In, First Out; C# Lab: The Quest; Chapter 9: Reading and Writing Files: Save the byte array, save the world; 9.1 C# uses streams to read and write data; 9.2 Different streams read and write different things; 9.3 A FileStream writes bytes to a file; 9.4 How to write text to a file in 3 simple steps; 9.5 The Swindler launches another diabolical plan; 9.6 Reading and writing takes two objects; 9.7 Data can go through more than one stream; 9.8 Use built-in objects to pop up standard dialog boxes; 9.9 Dialog boxes are just another .NET control; 9.10 Dialog boxes are objects, too; 9.11 Use the built-in File and Directory classes to work with files and directories; 9.12 Use File Dialogs to open and save files (all with just a few lines of code); 9.13 IDisposable makes sure your objects are disposed properly; 9.14 Avoid file system errors with using statements; 9.15 Trouble at work; 9.16 Writing files usually involves making a lot of decisions; 9.17 Use a switch statement to choose the right option; 9.18 Use a switch statement to let your deck of cards read from a file or write itself out to one; 9.19 Add an overloaded Deck() constructor that reads a deck of cards in from a file; 9.20 What happens to an object when it's serialized?; 9.21 But what exactly IS an object's state? What needs to be saved?; 9.22 When an object is serialized, all of the objects it refers to get serialized too...; 9.23 Serialization lets you read or write a whole object all at once; 9.24 If you want your class to be serializable, mark it with the [Serializable] attribute; 9.25 Let's serialize and deserialize a deck of cards; 9.26 .NET converts text to Unicode automatically; 9.27 C# can use byte arrays to move data around; 9.28 Use a BinaryWriter to write binary data; 9.29 You can read and write serialized files manually, too; 9.30 Find where the files differ, and use that information to alter them; 9.31 Working with binary files can be tricky; 9.32 Use file streams to build a hex dumper; 9.33 StreamReader and StreamWriter will do just fine; Chapter 10: Exception Handling: Putting out fires gets old; 10.1 Brian needs his excuses to be mobile; 10.2 When your program throws an exception, .NET generates an Exception object.; 10.3 Brian's code did something unexpected; 10.4 All exception objects inherit from Exception; 10.5 The debugger helps you track down and prevent exceptions in your code; 10.6 Use the IDE's debugger to ferret out exactly what went wrong in the excuse manager; 10.7 Uh-oh--the code's still got problems...; 10.8 Handle exceptions with try and catch; 10.9 What happens when a method you want to call is risky?; 10.10 Use the debugger to follow the try/catch flow; 10.11 If you have code that ALWAYS should run, use a finally block; 10.12 Use the Exception object to get information about the problem; 10.13 Use more than one catch block to handle multiple types of exceptions; 10.14 One class throws an exception, another class catches the exception; 10.15 Bees need an OutOfHoney exception; 10.16 An easy way to avoid a lot of problems: using gives you try and finally for free; 10.17 Exception avoidance: implement IDisposable to do your own clean up; 10.18 The worst catch block EVER: comments; 10.19 Temporary solutions are okay (temporarily); 10.20 A few simple ideas for exception handling; 10.21 Brian finally gets his vacation...; Chapter 11: Events and Delegates: What your code does when you're not looking; 11.1 Ever wish your objects could think for themselves?; 11.2 But how does an object KNOW to respond?; 11.3 When an EVENT occurs... objects listen; 11.4 One object raises its event, others listen for it...; 11.5 Then, the other objects handle the event; 11.6 Connecting the dots; 11.7 The IDE creates event handlers for you automatically; 11.8 The forms you've been building all use events; 11.9 Connecting event senders with event receivers; 11.10 A delegate STANDS IN for an actual method; 11.11 Delegates in action; 11.12 Any object can subscribe to a public event...; 11.13 Use a callback instead of an event to hook up exactly one object to a delegate; 11.14 Callbacks use delegates, but NOT events; Chapter 12: Review and Preview: Knowledge, power, and building cool stuff; 12.1 You've come a long way, baby; 12.2 We've also become beekeepers; 12.3 The beehive simulator architecture; 12.4 Building the beehive simulator; 12.5 Life and death of a flower; 12.6 Now we need a Bee class; 12.7 P. A. H. B. (Programmers Against Homeless Bees); 12.8 The hive runs on honey; 12.9 Filling out the Hive class; 12.10 The hive's Go() method; 12.11 We're ready for the World; 12.12 We're building a turn-based system; 12.13 Here's the code for World; 12.14 Giving the bees behavior; 12.15 The main form tells the world to Go(); 12.16 We can use World to get statistics; 12.17 Timers fire events over and over again; 12.18 The timer's using a delegate behind the scenes; 12.19 Add a Timer to the simulator; 12.20 Test drive; 12.21 Let's work with groups of bees; 12.22 A collection collects... DATA; 12.23 LINQ makes working with data in collections and databases easy; 12.24 Test drive (Part 2); 12.25 One final challenge: Open and Save; Chapter 13: Controls and Graphics: Make it pretty; 13.1 You've been using controls all along to interact with your programs; 13.2 Form controls are just objects; 13.3 Use controls to animate the beehive simulator; 13.4 Add a renderer to your architecture; 13.5 The renderer draws everything in the World on the two forms; 13.6 Controls are well-suited for visual display elements; 13.7 Build your first animated control; 13.8 BeeControl is LIKE a PictureBox... so let's start by INHERITING from PictureBox; 13.9 Create a button to add the BeeControl to your form; 13.10 Your controls need to dispose their controls, too!; 13.11 A UserControl is an easy way to build a control; 13.12 The renderer uses your BeeControl to draw animated bees on your forms; 13.13 Add the hive and field forms to the project; 13.14 Build the Renderer; 13.15 Now connect the main form to your two new forms, HiveForm and FieldForm; 13.16 Test drive... ahem... buzz; 13.17 Looks great, but something's not quite right...; 13.18 Let's take a closer look at those performance issues; 13.19 You resized your Bitmaps using a Graphics object; 13.20 Your image resources are stored in Bitmap objects; 13.21 Use System.Drawing to TAKE CONTROL of graphics yourself; 13.22 A 30-second tour of GDI+ graphics; 13.23 Use graphics to draw a picture on a form; 13.24 Graphics can fix our transparency problem...; 13.25 Use the Paint event to make your graphics stick; 13.26 A closer look at how forms and controls repaint themselves; 13.27 Double buffering makes animation look a lot smoother; 13.28 Double buffering is built into forms and controls; 13.29 Use a Graphics object and an event handler for printing; 13.30 PrintDocument works with the print dialog and print preview window objects; 13.31 There's so much more to be done...; Chapter 14: Captain Amazing the Death of the Object; 14.1 Your last chance to DO something... your object's finalizer; 14.2 When EXACTLY does a finalizer run?; 14.3 Dispose() works with using, finalizers work with garbage collection; 14.4 Finalizers can't depend on stability; 14.5 Make an object serialize itself in its Dispose(); 14.6 A struct looks like an object...; 14.7 ..but isn't on the heap; 14.8 Values get copied, references get assigned; 14.9 Structs are value types; objects are reference types; 14.10 The stack vs. the heap: more on memory; 14.11 Captain Amazing... not so much; 14.12 Extension methods add new behavior to EXISTING classes; 14.13 Extending a fundamental type: string; Chapter 15: Linq: Get control of your data; 15.1 An easy project...; 15.2 ... but the data's all over the place; 15.3 LINQ can pull data from multiple sources; 15.4 .NET collections are already set up for LINQ; 15.5 LINQ makes queries easy; 15.6 LINQ is simple, but your queries don't have to be; 15.7 LINQ is versatile; 15.8 LINQ can combine your results into groups; 15.9 Combine Jimmy's values into groups; 15.10 Use join to combine two collections into one query; 15.11 Jimmy saved a bunch of dough; 15.12 Connect LINQ to a SQL database; 15.13 Use a join query to connect Starbuzz and Objectville; C# Lab 3: Invaders; Leftovers: The top 5 things we wanted to include in this book; #1 LINQ to XML; #2 Refactoring; #3 Some of our favorite Toolbox components; #4 Console Applications; #5 Windows Presentation Foundation; Did you know that C# and the .NET Framework can...;