Andrew Stellman, despite being raised a New Yorker, has lived in 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 project management book for O'Reilly.
Meer over de auteursHead First C#
A Learner's Guide to Real-World Programming with C# and .Net
Paperback Engels 2024 9781098141783Samenvatting
What will you learn from this book?
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.
Specificaties
Lezersrecensies
Over Jennifer Greene
Inhoudsopgave
Why you should learn C#
Write code and explore C# with Visual Studio
Visual Studio is your gateway to C#
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
Stop your 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 a C# statement to the event handler method
Add more statements to your event handler
Add animals to your buttons
Run your app!
Here are just a few things Git can do for you
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
Uh-oh – there’s a bug in your code
When you find a bug, you need to sleuth it out
Finish the game by adding a timer
Add a timer to your game’s code
Finish the code for your game
Add a field to hold the time elapsed
Finish your TimerTick method
Even better ifs…
2. Variables, Statements, and Methods: Dive into C# code
Take a closer look at the files in your console app
A statement performs one single action
Statements are the building blocks for your apps
Statements live inside of methods
Methods do something
Methods help you organize your code
Your methods use variables to work with data
Declare your variables
Variables vary
You need to assign values to variables before you use them
A few useful types
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
if/else statements also do something if a condition isn’t true
Loops perform an action over and over
while loops keep looping statements while a condition is true
do/while loops run the statements then check the condition
for loops run a statement after each loop
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
Meet some of the controls you’ll use in this book
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
You’ll nest one Layout inside another
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…
…but what if she wants more than one enemy?
Ana can use objects to solve her problem
You use a class to build an object
An object gets its methods from its class
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
Theory and practice
An instance uses fields to keep track of things
Methods are what an object does. Fields are what the object knows.
Thanks for the memory
What’s on your app’s mind
Sometimes code can be difficult to read
Extremely compact code can be especially problematic
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!
Storytelling, fantasy, and mechanics
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
Use suffixes to give your literals types
A variable is like a data to-go cup
Use the Convert class to explore bits and bytes
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
So what happened?
When you cast a value that’s too big, C# adjusts it to fit its new container
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
Add a cast to get the AbilityScoreCalculator class to compile...
...but there’s still a bug!
Now we can finally fix Owen’s bug—and get the REAL “Sharpen” answer
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
Use each element in an array like it’s a normal variable
Arrays can contain reference variables
null means a reference points to nothing
Console.ReadLine returns a null when there are no lines available
Use the string? type when a string might be null
int.TryParse takes a string? parameter
Welcome to Sloppy Joe’s Budget House o’ Discount Sandwiches!
Sloppy Joe’s menu app uses a Grid layout
Grid controls
Use Grid properties to put a control in a cell
Define the rows and columns for a Grid
Create the Sloppy Joe’s menu app and set up the grid
Here’s the XAML for the app.
The C# code for the main page
Can we make the app more accessible?
Set the main header so the screen reader narrates it
Try setting the item1 label’s SemanticProperties.Description instead
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
How your damage calculator app will work
Tabletop talk (or maybe... dice discussion?)
Let’s try to fix that bug
Oops! It’s still not working
Use Debug.WriteLine to print diagnostic information
It’s easy to accidentally misuse your objects
Encapsulation means keeping some data in a class private
When in doubt, make it 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...
Encapsulation makes your classes...
A few ideas for encapsulating classes
Let’s use encapsulation to improve the SwordDamage class
Is every member of the SwordDamage class public?
Are fields or methods being misused?
Is there calculation required after setting a field?
So what fields and methods really need to be public?
Encapsulation keeps your data safe
Let’s use encapsulation in a class
Write a console app to test the PaintballGun class
Our class is well-encapsulated, but...
Properties make encapsulation easier
Replace the GetBalls and SetBalls methods with a property
Modify your top-level statements to use the Balls property
Debug your PaintballGun class to understand how the property works
Auto-implemented properties simplify your code
Use the prop snippet to create an auto-implemented property
Use a private setter to create a read-only property
Make the BallsLoaded setter private
What if we want to change the magazine size?
But there’s a problem...how do we initialize MagazineSize?
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
C# always calls the most specific method
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...
...but some birds don’t fly!
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
Thank you for returning my precious jewels!
Hiding methods versus overriding methods
Use the new keyword when you’re hiding methods
Use different references to call hidden methods
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
Use the debugger to really understand how these classes work
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 Bee Jobs box uses a Border with a VerticalStackLayout
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
Workers and honey are in a feedback loop
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
Let’s plan a trip to another planet
Like we said, some classes should never be instantiated
Solution: use an abstract class
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…
…but what if we add more Bee subclasses that can defend?
An interface defines methods and properties that a class must implement…
…but there’s no limit to the number of interfaces a class can implement
Interfaces let unrelated classes do the same job
Get a little practice using interfaces
What’s the picture ?
What’s the declaration ?
You can’t instantiate an interface, but you can reference an interface
If you try to instantiate an interface, your code won’t build
Use the interface to reference an object you already have
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
Use the “is” keyword to check if the Animal is a swimmer or pack hunter
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
Keep your eyes open for polymorphism!
The four core principles of object-oriented programming
Rubrieken
- advisering
- algemeen management
- coaching en trainen
- communicatie en media
- economie
- financieel management
- inkoop en logistiek
- internet en social media
- it-management / ict
- juridisch
- leiderschap
- marketing
- mens en maatschappij
- non-profit
- ondernemen
- organisatiekunde
- personal finance
- personeelsmanagement
- persoonlijke effectiviteit
- projectmanagement
- psychologie
- reclame en verkoop
- strategisch management
- verandermanagement
- werk en loopbaan