Table of Contents (the real thing)
-
Your brain on C#. Here you are trying to learn something, while here your brain is doing you a favor by making sure the learning doesn’t stick. Your brain’s thinking, “Better leave room for more important things, like which wild animals to avoid and whether naked snowboarding is a bad idea.” So how do you trick your brain into thinking that your life depends on knowing C#?
-
Want to build great apps...right now?
With C#, you’ve got a modern programming language and a valuable tool at your fingertips. And with Visual Studio, you’ve got an amazing development environment with highly intuitive features that make coding as easy as possible. Not only is Visual Studio a great tool for writing code, it’s also a really effective learning tool for exploring C#. Sound appealing? Let’s get coding!
-
You’re not just an IDE user. You’re a developer.
You can get a lot of work done using the IDE, but there’s only so far it can take you. Visual Studio is one of the most advanced software development tools ever made, but a powerful IDE is only the beginning. It’s time to dive in to C# code: how it’s structured, how it works, and how you can take control of it...because there’s no limit to what you can get your apps to do.
Unity Lab #1 Explore C# with Unity
Welcome to your first Head First C# Unity Lab. Writing code is a skill, and like any other skill, getting better at it takes practice and experimentation. Unity will be a really valuable tool for that. In this lab, you can begin practicing what you’ve learned about C# in Chapter 1 and Chapter 2.
-
Great developers keep their code and data organized.
What’s the first thing you do when you’re creating an app? You think about what it’s supposed to do, whether you’re solving a problem, creating a game, or just having fun. But it’s not always obvious how individual statements fit into your app’s bigger picture...and that’s where classes come in. They let you organize your code around the features you’re creating and the problems the app needs to solve. Classes can help you organize your data too, by using them to create objects that represent any “thing” your app needs to know about—and the classes that you design serve as “blueprints” for the objects used in your app.
-
Data and objects are the building blocks of your apps.
What would your apps be without data? Think about it for a minute. Without data, your programs are...well, it’s actually hard to imagine writing code without data. You need information from your users, and you use that to look up or produce new information to give back to them. In fact, almost everything you do in programming involves working with data in one way or another. In this chapter, you’ll learn the ins and outs of C#’s data types and references, see how to work with data in your program, and even learn a few more things about objects (guess what...objects are data, too!).
“A variable’s type determines what kind of data it can store”
“Casting lets you copy values that C# can’t automatically convert to another type”
“Two references mean TWO variables that can change the same object’s data”
“Welcome to Sloppy Joe’s Budget House o’ Discount Sandwiches!”
“Use the SetValue method to change a control’s semantic properties”
Unity Lab #2 Write C# Code for Unity
Unity isn’t just a powerful, cross-platform engine and editor for building 2D and 3D games and simulations. It’s also a great way to get practice writing C# code. In this lab, you’ll get more practice writing C# code for a project in Unity.
-
Ever wished for a little more privacy?
Sometimes your objects feel the same way. Just like you don’t want anybody you don’t trust reading your journal or paging through your bank statements, good objects don’t let other objects go poking around their fields. In this chapter, you’re going to learn about the power of encapsulation, a way of programming that helps you make code that’s flexible, easy to use, and difficult to misuse. You’ll make your objects’ data private, and add properties to protect how that data is accessed—and you’ll keep your objects’ important data from leaking out to other objects so they don’t accidentally misuse it.
-
Sometimes you DO want to be just like your parents.
Ever run across a class that almost does exactly what you want your class to do? Found yourself thinking that if you could just change a few things, that class would be perfect? With inheritance, you can extend an existing class so your new class gets all of its behavior—with the flexibility to make changes to that behavior so you can tailor it however you want. Inheritance is one of the most powerful concepts and techniques in the C# language: with it, you can avoid duplicate code, model the real world more closely, and end up with apps that are easier to maintain and less prone to bugs.
“When your classes use inheritance, you only need to write your code once”
“Any place where you can use a base class, you can use one of its subclasses instead”
“A subclass can override methods to change or replace members it inherited”
“The Beehive Management System is turn-based... now let’s convert it to real-time”
Unity Lab #3 GameObject Instances
C# is an object-oriented language, and since these Head First C# Unity Labs are all about getting practice writing C# code, it makes sense that these labs will focus on creating objects.
-
Need an object to do a specific job? Use an interface.
Sometimes you need to group your objects together based on the things they can do rather than the classes they inherit from—and that’s where interfaces come in. You can use an interface to define a specific job. Any instance of a class that implements the interface is guaranteed to do that job, no matter what other classes it’s related to. To make it all work, any class that implements an interface must promise to fulfill all of its obligations...or the compiler will break its kneecaps, see?
“An interface defines methods and properties that a class must implement…”
“You can’t instantiate an interface, but you can reference an interface”
“The RoboBee 4000 can do a worker bee’s job without using valuable honey”
“What if we want different animals to swim or hunt in packs?”
“C# has another tool for safe type conversion: the as keyword”
“Use upcasting and downcasting to move up and down a class hierarchy”
“Polymorphism means that one object can take many different forms”
-
Data isn’t always as neat and tidy as you’d like it to be.
In the real world, you don’t receive your data in tidy little bits and pieces. No, your data’s going to come at you in loads, piles, and bunches. You’ll need some pretty powerful tools to organize all of it—and lucky for us, C# has just the tools we need. Enums are types that let you define valid values to categorize your data. Collections are special objects that store many values, letting you store, sort, and manage all the data that your apps need to pore through. That way, you can spend your time thinking about writing apps to work with your data, and let the collections worry about keeping track of it for you.
-
In the last Unity Lab you started to build a game, using a prefab to create GameObject instances that appear at random points in 3D space and fly in circles. This Unity Lab picks up where the last one left off, allowing you to apply what you’ve learned about interfaces in C# and more.
-
It’s a data-driven world...we all need to know how to live in it.
Gone are the days when you could program for days, even weeks, without dealing with loads of data. Today, everything is about data, and that’s where LINQ comes in. LINQ is a feature of C# and .NET that not only lets you query data in your .NET collections in an intuitive way, but lets you group data and merge data from different data sources. You’ll use anonymous objects to manage your data in new and interesting ways. You’ll add unit tests to make sure your code is working the way you want. Once you’ve got the hang of wrangling your data into manageable chunks, you can use lambda expressions to refactor your C# code to make it easier to work with and more expressive.
-
Sometimes it pays to be persistent.
So far, all of your programs have been pretty short-lived. They fire up, run for a while, and shut down. But that’s not always enough, especially when you’re dealing with important information. You need to be able to save your work. In this chapter, we’ll look at how to write data to a file, and then how to read that information back in from a file. You’ll learn about streams, and how to store your objects in files with serialization, and get down to the actual bits and bytes of hexadecimal, Unicode, and binary data.
-
When you set up a scene in Unity, you’re creating a virtual 3D world for the characters in your game to move around in. But in most games, things aren’t directly controlled by the player. So how do these objects find their way around a scene? In this lab, we’ll look at how C# can help.
-
When you have to deal with error after error, it’s called “putting out fires.”
Imagine: it’s a few years from now. You spent all that time working on your C# skills and continuing to learn and improve, and now you’re one of the most senior developers at a big tech company. Now you’re getting panicked phone calls in the middle of the night from your support team because your app crashes, or doesn’t behave like it’s supposed to. You want to spend your time writing code, not putting out fires! Because nothing pulls you out of the programming groove like having to fix a strange bug you don’t recognize. Luckily, C# has exception handling, which lets you write code to deal with problems that come up. Better yet, you can even plan for those problems, and keep things running when they happen.
-
In the last Unity Lab, you created a scene with a floor (a plane) and a player (a sphere nested under a cylinder), and you used a NavMesh, a NavMesh Agent, and raycasting to get your player to follow your mouse clicks around the scene. In this lab, you’ll add to the scene with the help of C#.
Get Head First C#, 5th Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.