Chủ Nhật, 23 tháng 2, 2014

head first c# 2e (o''reilly)

table of contents
ix
Table of Contents (Summary)
Table of Contents (the real thing)
Your brain on C#. You’re sitting around trying to learn something, but
your brain keeps telling you all that learning isn’t important. Your brain’s saying,
“Better leave room for more important things, like which wild animals to avoid and
whether nude archery is a bad idea.” So how do you trick your brain into thinking
that your life really depends on learning C#?
Intro
Who is this book for? xxx
We know what you’re thinking xxxi
Metacognition xxxiii
Bend your brain into submission xxxv
What you need for this book xxxvi
Read me xxxvii
The technical review team xxxviii
Acknowledgments xxxix
Intro xxix
1 Get productive with C#: Visual Applications, in 10 minutes or less 1
2 It’s All Just Code: Under the hood 41
3 Objects: Get Oriented: Making code make sense 85
4 Types and References: It’s 10:00. Do you know where your data is? 125
C# Lab 1: A Day at the races 169
5 Encapsulation: Keep your privates… private 179
6 Inheritance: Your object’s family tree 215
7 Interfaces and abstract classes: Making classes keep their promises 269
8 Enums and collections: Storing lots of data 327
C# Lab 2: The Quest 385
9 Reading and Writing Files: Save the byte array, save the world 407
10 Exception Handling: Putting out fires gets old 463
11 Events and Delegates: What your code does when you’re not looking 507
12 Review and Preview: Knowledge, power, and building cool stuff 541
13 Controls and Graphics: Make it pretty 589
14 Captain Amazing: The Death of the Object 647
15 LINQ: Get control of your data 685
C# Lab 3: Invaders 713
i Leftovers: The top 11 things we wanted to include in this book 735
table of contents
x
Visual Applications, in 10 minutes or less
1
Want to build great programs really fast
With C#, you’ve got a powerful programming language and a valuable tool
at your fingertips. With the Visual Studio IDE, you’ll never have to spend hours
writing obscure code to get a button working again. Even better, you’ll be able
to focus on getting your work done, rather than remembering which method
parameter was for the name of a button, and which one was for its label. Sound
appealing? Turn the page, and let’s get programming.
get productive with C#
Why you should learn C# 2
C# and the Visual Studio IDE make lots of things easy 3
Help the CEO go paperless 4
Get to know your users’ needs before you start
building your program 5
What you do in Visual Studio… 8
What Visual Studio does for you… 8
Develop the user interface 12
Visual Studio, behind the scenes 14
Add to the auto-generated code 15
We need a database to store our information 18
The IDE created a database 19
SQL is its own language 19
Creating the table for the Contact List 20
Finish building the table 25
Insert your card data into the database 26
Connect your form to your database objects with a data source 28
Add database-driven controls to your form 30
How to turn YOUR application into EVERYONE’S application 35
Give your users the application 36
You’re NOT done: test your installation 37
You’ve built a complete data-driven application 38
table of contents
xi
Under the hood
You’re a programmer, not just an IDE user.
You can get a lot of work done using the IDE. But there’s only so far it
can take you. Sure, there are a lot of repetitive tasks that you do when
you build an application. And the IDE is great at doing those things for
you. But working with the IDE is only the beginning. You can get your
programs to do so much more—and writing C# code is how you do it.
Once you get the hang of coding, there’s nothing your programs can’t do.
it’s all just code
2
When you’re doing this… 42
…the IDE does this 43
Where programs come from 44
The IDE helps you code 46
When you change things in the IDE, you’re also changing
your code 48
49
Anatomy of a program 50
Your program knows where to start 52
53
Two classes can be in the same namespace 59
Your programs use variables to work with data 60
C# uses familiar math symbols 62
Use the debugger to see your variables change 63
Loops perform an action over and over 65
Time to start coding 66
if/else statements make decisions 67
Set up conditions and see if they’re true 68
table of contents
xii
3
Making Code Make Sense
Every program you write solves a problem.
When you’re building a program, it’s always a good idea to start by thinking about what
problem your program’s supposed to solve. That’s why objects are really useful. They
let you structure your code based on the problem it’s solving, so that you can spend your
time thinking about the problem you need to work on rather than getting bogged down in
the mechanics of writing code. When you use objects right, you end up with code that’s
intuitive to write, and easy to read and change.
objects: get oriented!
new Navigator()
new Navigator()
new Navigator()
How Mike thinks about his problems 86
How Mike’s car navigation system thinks about his problems 87
Mike’s Navigator class has methods to set and modify routes 88
Use what you’ve learned to build a program that uses a class 89
90
Mike can use objects to solve his problem 92
You use a class to build an object 93
When you create a new object from a class, it’s called an instance
of that class 94
A better solution…brought to you by objects! 95
An instance uses fields to keep track of things 100
Let’s create some instances! 101
What’s on your program’s mind 103
You can use class and method names to make your code intuitive 104
Give your classes a natural structure 106
Class diagrams help you organize your classes so they make sense 108
Build a class to work with some guys 112
Create a project for your guys 113
Build a form to interact with the guys 114
There’s an easier way to initialize objects 117
table of contents
xiii
4
It’s 10:00. Do you know where your data is?
Data type, database, Lieutenant Commander Data…
it’s all important stuff.
Without data, your programs are useless. 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, see how to work with data in your program, and
even figure out a few dirty secrets about objects (pssst…objects are data, too).
types and references
The variable’s type determines what kind of data it can store 126
A variable is like a data to-go cup 128
10 pounds of data in a 5 pound bag 129
Even when a number is the right size, you can’t just assign it to
any variable 130
When you cast a value that’s too big, C# will adjust it automatically 131
C# does some casting automatically 132
When you call a method, the arguments must be compatible
with the types of the parameters 133
Combining = with an operator 138
Objects use variables, too 139
Refer to your objects with reference variables 140
References are like labels for your object 141
If there aren’t any more references, your object gets
garbage-collected 142
Multiple references and their side effects 144
Two references means TWO ways to change an object’s data 149
A special case: arrays 150
Welcome to Sloppy Joe’s Budget House o’ Discount Sandwiches! 152
Objects use references to talk to each other 154
Where no object has gone before 155
Build a typing game 160
fido
Lucky
fido
Lucky
table of contents
xiv
Joe, Bob, and Al love going to the track, but they’re
tired of losing all their money. They need you to build a
simulator for them so they can figure out winners before
they lay their money down. And, if you do a good job,
they’ll cut you in on their profits.
C# Lab 1
A Day at the Races
The spec: build a racetrack simulator 170
The Finished Product 178
table of contents
xv
5
Keep your privates… private
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. You’ll make your object’s data private, and
add methods to protect how that data is accessed.
encapsulation
Kathleen is an event planner 180
What does the estimator do? 181
Kathleen’s Test Drive 186
Each option should be calculated individually 188
It’s easy to accidentally misuse your objects 190
Encapsulation means keeping some of the data in a class private 191
Use encapsulation to control access to your class’s methods
and fields 192
But is the realName field REALLY protected? 193
Private fields and methods can only be accessed from
inside the class 194
Encapsulation keeps your data pristine 202
Properties make encapsulation easier 203
Build an application to test the Farmer class 204
Use automatic properties to finish the class 205
What if we want to change the feed multiplier? 206
Use a constructor to initialize private fields 207
table of contents
xvi
6
Your object’s family tree
Sometimes you
DO
want to be just like your parents.
Ever run across an object that almost does exactly what you want your object to do?
Found yourself wishing that if you could just change a few things, that object would
be perfect? Well, that’s just one reason that inheritance is one of the most powerful
concepts and techniques in the C# language. Before you’re through with this chapter,
you’ll learn how to subclass an object to get its behavior, but keep the flexibility to
make changes to that behavior. You’ll avoid duplicate code, model the real world
more closely, and end up with code that’s easier to maintain.
inheritance
Kathleen does birthday parties, too 216
We need a BirthdayParty class 217
Build the Party Planner version 2.0 218
When your classes use inheritance, you only need to write
your code once 226
Kathleen needs to figure out the cost of her parties, no matter what
kind of parties they are. 226
Build up your class model by starting general and getting
more specific 227
How would you design a zoo simulator? 228
Use inheritance to avoid duplicate code in subclasses 229
0
Think about how to group the animals 231
Create the class hierarchy 232
Every subclass extends its base class 233
A subclass can override methods to change or replace methods
it inherited 238
Any place where you can use a base class, you can use one of
its subclasses instead 239
A subclass can hide methods in the superclass 246
Use the override and virtual keywords to inherit behavior 248
251
Now you’re ready to finish the job for Kathleen! 252
Build a beehive management system 257
First you’ll build the basic system 258
Use inheritance to extend the bee management system 263
table of contents
xvii
7
Making classes keep their promises
Actions speak louder than words.
Sometimes you need to group your objects together based on the things they can
do rather than the classes they inherit from. That’s where interfaces come in—they
let you work with any class that can do the job. But with great power comes great
responsibility, and any class that implements an interface must promise to fulfill all of
its obligations…or the compiler will break their kneecaps, see?
interfaces and abstract classes
Let’s get back to bee-sics 270
We can use inheritance to create classes for different types of bees 271
An interface tells a class that it must implement certain methods
and properties 272
Use the interface keyword to define an interface 273
Classes that implement interfaces have to include ALL of the
interface’s methods 275
You can’t instantiate an interface, but you can reference an interface 278
278
Interface references work just like object references 279
You can find out if a class implements a certain interface with “is” 280
Interfaces can inherit from other interfaces 281
Upcasting works with both objects and interfaces 285
Downcasting lets you turn your appliance back into a coffee maker 286
Upcasting and downcasting work with interfaces, too 287
There’s more than just public and private 291
Access modifiers change visibility 292
Some classes should never be instantiated 295
An abstract class is like a cross between a class and an interface 296
An abstract method doesn’t have a body 299
Polymorphism means that one object can take many different forms 307
table of contents
xviii
8
Storing lots of data
When it rains, it pours.
In the real world, you don’t get to handle your data in tiny 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 that’s where collections
come in. They let you store, sort, and manage all the data that your programs
need to pore through. That way, you can think about writing programs to work
with your data, and let the collections worry about keeping track of it for you.
enums and collections
Strings don’t always work for storing categories of data 328
Enums let you work with a set of valid values 329
Enums let you represent numbers with names 330
We could use an array to create a deck of cards… 333
Lists are more flexible than arrays 336
Generics can store any type 340
Collection initializers work just like object initializers 344
Let’s create a List of Ducks 345
Lists are easy, but SORTING can be tricky 346
IComparable <Duck> helps your list sort its ducks 347
Use IComparer to tell your List how to sort 348
Create an instance of your comparer object 349
IComparer can do complex comparisons 350
Overriding a ToString() method lets an object describe itself 353
Update your foreach loops to let your Ducks and Cards
print themselves 354
You can upcast an entire list using IEnumerable 356
You can build your own overloaded methods 357
The Dictionary Functionality Rundown 364
Build a program that uses a Dictionary 365
And yet MORE collection types… 377
A queue is FIFO—First In, First Out 378
A stack is LIFO—Last In, First Out 379
poof!

Không có nhận xét nào:

Đăng nhận xét