Book description
Create apps, games, and more using this engaging, highly visual introduction to C#, .NET, and Visual Studio. In the first chapter you'll dive right in, building a fully functional game using C# and .NET MAUI that can run on Windows, Mac, and even Android and iOS devices. You'll learn how to use classes and object-oriented programming, create 3D games in Unity, and query data with LINQ. And you'll do it all by solving puzzles, doing hands-on exercises, and building real-world applications. Interested in a development career? You'll learn important development techniques and ideas—many who learned to code with this book are now professional developers, team leads, coding streamers, and more. There's no experience required except the desire to learn. And this is the best place to start.
What's so special about this book?
If you've read a Head First book, you know what to expect: a visually rich format designed for the way your brain works. If you haven't, you're in for a treat. With this book, you'll learn C# through a multisensory experience that engages your mind—rather than a text-heavy approach that puts you to sleep.
Publisher resources
Table of contents
-
1. Start Building apps with C#: Build Something Great…Fast!
- Why you should learn C#
- Write code and explore C# with Visual Studio
- Install Visual Studio Community Edition
- Run Visual Studio
- Create your first project in Visual Studio – Windows edition
- Create your first project in Visual Studio – Mac edition
- Let’s build a game!
- Break up large projects into smaller parts
- Here’s how you’ll build your game
- Create a .NET MAUI project in Visual Studio – Windows edition
- Create a .NET MAUI project in Visual Studio – Mac edition
- Run your new .NET MAUI app
- MAUI apps work on all of your devices
- Here’s the page that you’ll build
- Start editing your XAML code
- Add the XAML for a Button and a Label
- Use a FlexLayout to make a grid of animal buttons
- Write C# code to add the animals to the buttons
- Start editing the PlayAgainButton event handler method
- Add more statements to your event handler
- Add animals to your buttons
- Run your app!
- Visual Studio makes it easy to use Git
- Add C# code to handle mouse clicks
- Enter the code for the event handler
- Run your app and find all the pairs
- Finish the game by adding a timer
- Add a timer to your game’s code
- Finish the code for your game
- Even better ifs…
-
2. Variables, Statements, and Methods: Dive into C# code
- Take a closer look at the files in your console app
- Statements are the building blocks for your apps
- Statements live inside of methods
- Your methods use variables to work with data
- Generate a new method to work with variables
- Add code that uses operators to your method
- Use the debugger to watch your variables change
- Use operators to work with variables
- “if” statements make decisions
- Loops perform an action over and over
- Use code snippets to help write loops
- Some useful things to keep in mind about C# code
- Controls drive the mechanics of your user interfaces
- Other controls you’ll use in this book
- Build a .NET MAUI app to experiment with controls
- Create a new app to experiment with controls
- Explore your new MAUI app and figure out how it works
- Add an Entry control to your app
- Add properties to your Entry control
- Make your Entry control update a Label
- Combine horizontal and vertical stack layouts
- Add a Picker control to display a list of choices
-
3. Namespaces and Classes: Organizing your code
- Classes help you organize your code
- If code is useful, classes can help you reuse it
- Some methods take parameters and return a value
- Visual Studio helps you explore parameters and return values
- Let’s build an app that picks random cards
- You’ll use an array to store the cards
- Create an app with a Main method
- Create your PickRandomCards app without top-level statements
- Add a class called CardPicker to your app
- Use Quick Actions to remove unnecessary ‘using’ lines
- Convert between namespace styles
- Use the new keyword to create an array of strings
- Here’s the code for your finished CardPicker class
- Ana’s working on her next game
- Ana’s game is evolving…
- …so how can Ana make things easier for herself?
- Build a paper prototype for a classic game
- Build a MAUI version of your random card app
- Make your app accessible!
- Make your MAUI app pick random cards
- Reuse your the CardPicker class
- Add a using directive to use code in another namespace
- Ana’s prototypes look great…
- Ana can use objects to solve her 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 for Ana…brought to you by objects
- An instance uses fields to keep track of things
- Thanks for the memory
- What’s on your app’s mind
- Sometimes code can be difficult to read
- Most code doesn’t come with a manual
- Use intuitive class and method names
- Build a class to work with some guys
- There’s an easier way to initialize objects with C#
- Use the C# Interactive window or csi to run C# code
-
4. Data, Types, Objects, and References: Managing your App’s Data
- Owen could use our help!
- Character sheets store different types of data on paper
- A variable’s type determines what kind of data it can store
- C# has several types for storing integers
- Types for storing really HUGE and really tiny numbers
- Let’s talk about strings
- A literal is a value written directly into your code
- A variable is like a data to-go cup
- Other types come in different sizes, too
- 10 pounds of data in a 5-pound bag
- Casting lets you copy values that C# can’t automatically convert to another type
- C# does some conversions automatically
- When you call a method, the arguments need to be compatible with the types of the parameters
- Owen is constantly improving his game...
- ...but the trial and error can be time-consuming
- Let’s help Owen experiment with ability scores
- Fix the compiler error by adding a cast
- Use reference variables to access your objects
- References are like sticky notes for your objects
- If there aren’t any more references, your object gets garbage-collected
- Multiple references and their side effects
- Two references mean TWO variables that can change the same object’s data
- Objects use references to talk to each other
- Arrays hold multiple values
- Arrays can contain reference variables
- null means a reference points to nothing
- Use the string? type when a string might be null
- Welcome to Sloppy Joe’s Budget House o’ Discount Sandwiches!
- Sloppy Joe’s menu app uses a Grid layout
- Grid controls
- Define the rows and columns for a Grid
- Create the Sloppy Joe’s menu app and set up the grid
- The C# code for the main page
- Can we make the app more accessible?
- Use the SetValue method to change a control’s semantic properties
-
5. Encapsulation: How Objects Keep their Secrets
- Let’s help Owen roll for damage
- Create a console app to calculate damage
- Design a MAUI version of the damage calculator app
- Tabletop talk (or maybe... dice discussion?)
- Let’s try to fix that bug
- Use Debug.WriteLine to print diagnostic information
- It’s easy to accidentally misuse your objects
- Encapsulation means keeping some 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 instances of the same class
- Why encapsulation? Think of an object as an opaque box...
- A few ideas for encapsulating classes
- Let’s use encapsulation to improve the SwordDamage class
- Encapsulation keeps your data safe
- Write a console app to test the PaintballGun class
- Properties make encapsulation easier
- Modify your top-level statements to use the Balls property
- Auto-implemented properties simplify your code
- Use a private setter to create a read-only property
- What if we want to change the magazine size?
- Use a constructor with parameters to initialize properties
- Specify arguments when you use the “new” keyword
- Pool Puzzle
- Pool Puzzle Solution
- Initialize fields and properties inline or in the constructor
- Make the screen reader announce each roll
- A few useful facts about methods and properties
- Objectcross
-
6. Inheritance: Your Object’s Family Tree
- Calculate damage for MORE weapons
- Use a switch statement to match several candidates
- One more thing...can we calculate damage for a dagger? and a mace? and a staff? and...
- 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?
- Different animals have different behaviors
- Every subclass extends its base class
- Any place where you can use a base class, you can use one of its subclasses instead
- Use a colon to extend 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 members it inherited
- Some members are only implemented in a subclass
- Use the debugger to understand how overriding works
- Build an app to explore virtual and override
- A subclass can hide methods in the base class
- 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 to call it
- A subclass and base class can have different constructors
- It’s time to finish the job for Owen
- A class do should just one thing
- Build a beehive management system
- How the Beehive Management System app works
- The page uses a grid to lay out the controls for the UI
- The beehive management system class model
- All bees in the system extend the Bee class
- All the constants are in their own static class
- The worker bees extend the Bee class
- The Queen class: how she manages the worker bees
- Here’s the code-behind for MainPage.xaml.cs
- Feedback drives your Beehive Management game
- The Beehive Management System is turn-based... now let’s convert it to real-time
- Some classes should never be instantiated
- An abstract class is an intentionally incomplete class
- Like we said, some classes should never be instantiated
- An abstract method doesn’t have a body
- Abstract properties work just like abstract methods
- The Deadly Diamond of Death
-
7. Interfaces, Casting, and “is”: Making Classes keep their Promises
- The beehive is under attack!
- HiveDefender needs a DefendHive method because enemies can attack at any time
- We could use casting to call the DefendHive method…
- An interface defines methods and properties that a class must implement…
- Interfaces let unrelated classes do the same job
- Get a little practice using interfaces
- What’s the declaration ?
- You can’t instantiate an interface, but you can reference an interface
- Interface references are ordinary object references
- The RoboBee 4000 can do a worker bee’s job without using valuable honey
- The IWorker’s Job property is a hack
- Use “is” to check the type of an object
- Use “is” to access methods in a subclass
- What if we want different animals to swim or hunt in packs?
- Use interfaces to work with classes that do the same job
- Safely navigate your class hierarchy with “is”
- C# has another tool for safe type conversion: the “as” keyword
- Use upcasting and downcasting to move up and down a class hierarchy
- A quick example of upcasting
- Upcasting turns your CoffeeMaker into an Appliance
- Downcasting turns your Appliance back into a CoffeeMaker
- Upcasting and downcasting work with interfaces, too
- Interfaces can inherit from other interfaces
- Interfaces can have static members
- Default implementations give bodies to interface methods
- Add a ScareAdults method with a default implementation
- Data binding updates MAUI controls automatically
- Add data binding to the default MAUI app
- Make Moods implement the INotifyPropertyChanged interface
- Use the PropertyChanged event to make data binding work
- Polymorphism means that one object can take many different forms
- About the Authors
Product information
- Title: Head First C#, 5th Edition
- Author(s):
- Release date: September 2024
- Publisher(s): O'Reilly Media, Inc.
- ISBN: 9781098141783
You might also like
book
Head First C#, 4th Edition
What will you learn from this book? Dive into C# and create apps, user interfaces, games, …
book
C# in Depth, Fourth Edition
C# in Depth, Fourth Edition is your key to unlocking the powerful new features added to …
book
Clean Code with C# - Second Edition
Enhance your programming skills through code reviews, TDD and BDD implementation, and API design to overcome …
book
Programming C# 10
C# is undeniably one of the most versatile programming languages available to engineers today. With this …