Chapter 3. Objects…get oriented!: Making code make sense

Images

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—and really put some thought into how you design them—you end up with code that’s intuitive to write, and easy to read and change.

If code is useful, it gets reused

Developers have been reusing code since the earliest days of programming, and it’s not hard to see why. If you’ve written a class for one program, and you have another program that needs code that does exactly the same thing, then it makes sense to reuse the same class in your new program.

Images

Some methods take parameters and return a value

You’ve seen methods that do things, like the SetUpGame method in Chapter 1 that sets up your game. Methods can do more than that: they can use parameters to get input, do something with that input, and then generate output with a return value that can be used by the statement that ...

Get Head First C#, 4th 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.