Wiley
Wiley.com
Print this page Share

Beginning Object-Oriented Programming with C#

ISBN: 978-1-118-33692-2
624 pages
November 2012
Beginning Object-Oriented Programming with C# (1118336925) cover image
The ideal beginner's guide to C# and object-oriented programming

Wrox beginners' guides have the perfect formula for getting programming newcomers up and running. This one introduces beginners to object-oriented programming using C# to demonstrate all of the core constructs of this programming framework. Using real-world situations, you'll discover how to create, test, and deliver your programs and how to work with classes, arrays, collections, and all the elements of object-oriented programming.

  • Covers exactly what beginners, even those with no prior programming experience, need to know to understand object-oriented programming and start writing programs in C#
  • Explains the advantages and disadvantages of C#, and tips for understanding C# syntax
  • Explores properties, encapsulation, and classes; value data types; operands and operators; errors and debugging; variables; and reference types
  • Shows how to use statement repetition and program loops, understand arrays and collections, and write your own classes
  • Also covers inheritance and polymorphism

Beginning Object-Oriented Programming with C# uses the tried-and-true Wrox formula for making this popular programming method easy to learn.

See More
INTRODUCTION xxv

PART I: GETTING STARTED

CHAPTER 1: INTRODUCING C# 3

A Short History of Object-Oriented Programming (OOP) 4

Installing C# 5

Downloading Visual Studio Professional 5

A Test Program Using C# 8

Creating a Project 9

The C# Integrated Development Environment 10

The Major IDE Windows 10

Using the Source Code Window 12

Adding a Toolbox Object to a Windows Form 13

Changing the Text of a Label Object 14

Running the Program 16

Summary 16

CHAPTER 2: UNDERSTANDING OBJECTS 19

Understanding Objects 20

Everyday Use of Objects 20

Class Properties 20

Class Methods 22

How Many Properties, How Many Methods? 23

Classes Are Simplifi cations of Objects 23

What Do I Do After I’ve Defi ned a Class? 23

Instantiating an Object of a Class 26

I Have an Object…Now What? 29

Why Hide the Data Inside an Object? 32

Getting Started with Objects 32

Developing a Program Plan 38

Where to Start? The Five Program Steps 38

The Five Program Steps 38

Creating a Simple Application Using Objects 40

Using the Program Steps to Create a Program Plan 40

1: Initialization 40

2: Input 41

3: Process 42

4: Display 43

5: Termination 43

Using C# to Implement Your Program Plan 43

Critique of the btnDisplayOutput Click Event Code 53

Summary 54

PART II: UNDERSTANDING C# SYNTAX

CHAPTER 3: UNDERSTANDING DATA TYPES 59

Computer Data 59

Integer Data Types 60

Range of Integer Data Types 61

Understanding Binary Numbers 61

Signed Integer Values 62

Which Integer Should You Use? 62

Variable Naming Rules and Conventions 63

Floating-Point Data Types 71

Which Floating-Point Data Type Should You Use? 74

Monetary Values: The Decimal Data Type 74

Using IntelliSense to Locate Program Errors 76

Syntax Rules and Error Messages 76

The Boolean Data Type 77

Summary 79

CHAPTER 4: UNDERSTANDING C# STATEMENTS 81

Basic Building Blocks of a Programming Language 82

Operands and Operators 82

Expressions 83

Statements 83

Operator Precedence 84

Overriding the Default Precedence Order 85

Defi ning Variables 87

Defi ning a Variable from the Compiler’s Point of View 87

Step 1: Preliminary Syntax Checking 87

Step 2: Symbol Table Checking 87

Step 3: Defi ning a Variable 89

Using a Variable in a Program 90

Bucket Analogy 91

Types of Program Errors 93

Syntax Errors 93

Semantic Errors 93

Logic Errors 94

The Visual Studio Debugger 94

The Debugging Process 94

Making Repetitious Debugging Easier 95

Using the Visual Studio Debugger 97

Setting a Breakpoint 97

Using a Breakpoint to Examine Variables 98

Single-Stepping the Program 100

Defensive Coding 101

Write Your Code So That Someone Else Can Easily Understand It 101

Use Program Comments 101

Use Meaningful Variable Names 103

Avoid Magic Numbers 104

Use a Consistent Coding Style 105

Take a Break 105

Use a Second Set of Eyes 105

Summary 106

CHAPTER 5: UNDERSTANDING REFERENCE DATA TYPES 109

String Variables 110

Defi ning a String Reference Variable 110

The Meaning of null 111

Why Reference Types Are Diff erent from Value Types 113

Reference Variable Rules 113

Reference Type Variables Versus Value Type Variables 115

Why Do Reference Variables Work the Way They Do? 115

Pass by Value Versus Pass by Reference 116

A Little Effi ciency Gain 116

Using String Variables 117

String Concatenation 117

Shorthand Assignment Operators 117

String Manipulation 118

String Length 118

Letting IntelliSense Show You Properties and Methods 119

Using an IntelliSense Option 120

An Important Distinction Between Properties and Methods 120

Thinking About Object Properties and Methods 120

Strings and Escape Sequences 131

Verbatim String Literals 132

DateTime Reference Objects 133

DateTime and ToString( ) Conversions 133

Class Constructors 139

Invoking the Application 139

The UpdateTimeInfo( ) Method 140

Overloaded Methods 141

Method Signatures 142

The Refresh Button 143

Summary 144

CHAPTER 6: MAKING DECISIONS IN CODE 147

Relational Operators 148

Using Relational Operators—The if Statement 148

The if-else Statement 152

Shorthand for Simple if-else: The Ternary Operator 154

Style Considerations for if and if-else Statements 155

Nested if Statements 157

RDC 158

Logical Operators 160

Using the Logical Operators 162

Associativity 163

The switch Statement 164

Summary 167

CHAPTER 7: STATEMENT REPETITION USING LOOPS 169

Program Loops 170

Good Loops, Bad Loops 170

The Three Conditions of a Well-Behaved Loop 170

The for Loop 171

Increment and Decrement Operators 172

Sequencing in a for Loop 173

When to Use a for Loop 179

Nested for Loops 179

Use the Debugger as a Learning Tool 182

while Loops 182

Why Have More Than One Type of Loop? 184

do-while Program Loops 185

The continue Statement 189

Summary 190

CHAPTER 8: UNDERSTANDING ARRAYS AND COLLECTIONS 193

What Is an Array? 194

Some Array Details 195

Array Element Versus Array Index 195

N – 1 Rule 196

Casts 201

The ListView Object 201

Arrays Are Objects 206

Multidimensional Arrays 207

Initializing Arrays 211

Variations for Initializing an Array 212

Which Array Defi nition/Initialization Syntax Is Best? 212

Code Like a User 212

Initializing Multidimensional Arrays 213

Initializer Lists for String Objects 213

Ragged Arrays 215

Defi ning Ragged Arrays at Runtime 215

Collections 216

ArrayList Objects 218

Summary 221

PART III: WRITING YOUR OWN CLASSES

CHAPTER 9: DESIGNING CLASSES 227

Class Design 228

Scope 230

Block Scope 231

Local Scope 232

Class Scope 232

Namespace Scope 233

Visualizing Scope 233

Why Does C# Support Scope? 235

Think Before You Write 235

Designing a Program 236

The Five Program Steps 236

Initialization Step 236

Input Step 238

Process Step 238

Display Step 238

Termination Step 238

Look at the Forest, Not Just the Trees 238

UML Light 239

Access Specifi ers 239

Access Specifi ers and Scope 240

The static Keyword 241

UML Methods 242

Design Intent and Access Specifi ers for Methods 242

Class Property and Method Names 244

Think Like a User 245

The clsDates Design 245

namespace Modifi er 247

Class Organization 248

static Data Versus Instance Members 248

Property and Helper Methods 249

General Methods 249

User Interfaces Versus User Interfaces 252

Summary 252

CHAPTER 10: DESIGNING AND WRITING CUSTOM CLASSES 255

Constructors 256

Default Constructors 256

Nondefault Constructors 256

Constructor Overloading 257

Constructor Sloppiness 257

Fixing the Constructor Problem 258

Always Call the Default Constructor 259

Property Methods 259

Property Methods and Getters and Setters 260

Property Method Rules 261

How the get Property Methods Work 262

How Does Visual Studio Know Whether to

Use the get or set Statement Block? 263

What to Do if an Error Occurs in a Property Method 264

Method Coupling and Cohesion 266

Cohesion 266

Coupling 266

Class Design for Deck-of-Cards Program 267

UML Class Diagram for Shuffl e Deck Program 268

Class Constants and Properties 268

Class Methods 269

The clsCardDeck Code 273

Class Properties, Constructor, and Property Methods 275

Class General Methods 276

Designing a Card Game Using clsCardDeck 278

Design Considerations 279

What Kind of Architecture? 279

Sideways Refi nement 280

Summary 291

CHAPTER 11: EXCEPTION HANDLING AND DEBUGGING 295

Overview 296

Bugs 296

Syntax Errors 296

Semantic Errors 296

Logic Errors 297

Input Errors 297

Data Validation 297

Limit User Input 298

Check Boxes 301

Combination Boxes 302

Date and Time Input 304

Exception Handling 306

try-catch Statement Blocks 308

Anticipating a Specifi c Exception 310

Fuzzy Exception Messages 312

The fi nally Statement Block 313

Program Debugging 314

The Nature of the Beast 314

Detection 314

Isolation 316

The Visual Studio Debugger 316

The Locals Window 317

The Immediate Window 318

Single-Stepping Through the Program 318

Backing Up from a Breakpoint 319

The Debug Toolbar 319

Finding the Bug 320

Scaff old Code 321

Toggling Scaff old Code 322

Defensive Coding 322

Summary 323

CHAPTER 12: GENERICS 327

What Are Generics? 327

Recursion 333

Data Problems 333

Introduction to Generics 334

Generics Versus ArrayLists 334

Boxing and Unboxing 335

Generic Quicksort 343

Using Generics with Constraints and Interfaces 347

Interfaces 347

Why Use an Interface? 348

Using an Interface 349

How Do You Know an Interface Is Implemented for a Data Type? 350

Summary 354

PART IV: STORING DATA

CHAPTER 13: USING DISK DATA FILES 359

Directories 360

The DriveInfo Class 360

Directory Class 360

DirectoryInfo Class 361

File Class 367

FileInfo Class 368

Types of Files 369

Textual Versus Binary Data Files 370

Sequential Versus Random Access Files 380

Sequential Files 380

Advantages and Disadvantages of Sequential Files 380

Random Access Files 381

Fixed Record Sizes 381

Advantages and Disadvantages of Random Access Files 383

Serialization and Deserialization 411

To Serialize or Not to Serialize 417

MDI, Menus, and File Dialogs 418

Summary 422

CHAPTER 14: USING DATABASES 425

What Is a Database? 425

The Structure of a Database 426

Database Tables, Fields, and Records 426

Designing Fields for a Database Table 427

Data Normalization 428

Creating Your Own Databases 432

Using SQL Server Management Studio 432

Using SQL 435

The SELECT Statement 435

The WHERE Predicate 436

The ORDER BY Clause 436

Aggregates 437

A DBMS System 438

Displaying Database Data Without Data Binding 451

Performing the Query 452

Edit a Record (UPDATE) 454

Summary 456

CHAPTER 15: USING LINQ 459

Using LINQ 459

Query Keywords 460

Summary 470

PART V: ADVANCED TOPICS

CHAPTER 16: INHERITANCE AND POLYMORPHISM 475

What Is Inheritance? 476

An Inheritance Example 476

The Base and Derived Classes 478

The protected Access Specifi er 479

Advantages of Inherited Relationships 480

Base Classes Are Not Derived Classes 491

Abstract Classes 491

Polymorphism 492

Extension Methods 495

Summary 498

CHAPTER 17: PRINTING AND THREADING 501

Using a Printer 501

Threading 509

A Threading Program 510

What Have You Gained with Threading? 515

Refactoring 516

Rename 516

Extract Method 516

Summary 520

CHAPTER 18: WEB PROGRAMMING 523

Static Web Pages 523

Dynamic Web Pages 530

Summary 535

APPENDIX: ANSWERS TO EXERCISES 539

INDEX 577

See More

Jack Purdum, Ph.D., is an established author of more than a dozen programming books. Using his teaching experience as a professor of computer language instruction at Purdue University, he has identified common stumbling blocks for beginning programmers and knows how to teach concepts so readers can understand and code immediately.

Wrox Beginning guides are crafted to make learning programming languages and technologies easier than you think, providing a structured, tutorial format that will guide you through all the techniques involved.

See More
Download TitleSizeDownload
ReadMe 449 bytes Click to Download
Code Download :: Chapter 2 8.62 KB Click to Download
Code Download :: Chapter 3 75.47 KB Click to Download
Code Download :: Chapter 4 111.56 KB Click to Download
Code Download :: Chapter 5 189.06 KB Click to Download
Code Download :: Chapter 6 79.09 KB Click to Download
Code Download :: Chapter 7 159.27 KB Click to Download
Code Download :: Chapter 8 385.30 KB Click to Download
Code Download :: Chapter 9 102.70 KB Click to Download
Code Download :: Chapter 10 191.86 KB Click to Download
Code Download :: Chapter 11 153.64 KB Click to Download
Code Download :: Chapter 12 103.84 KB Click to Download
Code Download :: Chapter 13 536.13 KB Click to Download
Code Download :: Chapter 14 4.42 MB Click to Download
Code Download :: Chapter 15 121.07 KB Click to Download
Code Download :: Chapter 16 161.66 KB Click to Download
Code Download :: Chapter 17 194.91 KB Click to Download
Code Download :: Chapter 18 3.60 KB Click to Download
Code Download :: All Chapters 6.94 MB Click to Download
See More

Do you think you've discovered an error in this book? Please check the list of errata below to see if we've already addressed the error. If not, please submit the error via our Errata Form. We will attempt to verify your error; if you're right, we will post a correction below.

ChapterPageDetailsDatePrint Run
32 Error in Text
Correction to Steps:
  1. From the File menu of the main menu options, select New Project
  2. From New Projects Dialog, select: Installed --> Templates --> Visual C# --> Windows You should see a window similar to Figure 2.8. (If you are using Visual Studio 2012 Express, there will be fewer options in the center panel of the dialog box than shown in Figure 2.8.)
  3. You should see several C# templates in the center window. Select Empty Project.
  4. Towards the bottom of the list, change the name to Chapter02Program01.
  5. Change the Location to whatever directory name you wish to use for this program.
  6. The Solution Name defaults to the application name (e.g., Chapter02Program01), but you can change it if you wish. I usually uncheck the Create directory for solution option. If you have one directory where you want all your test code to reside, then leave it checked so a directory is created for each project.
  7. Click OK. The Solution Explorer window is opened for you, as is the Properties window for the project.
  8. From the main menu, click Project --> Add Windows Form. By default, Visual Studio selects the Installed --> Visual C# Items menu option, so your screen should look similar to Figure 2.9.
  9. Now select Windows Form from the center panel as shown in Figure 2.9.
  10. Change the Name field at the bottom of the dialog box to frmMain.cs and click Add. Visual Studio creates the new form and adds it to the project. The Source window displays some source code that is automatically generated by Visual Studio. Visual Studio now displays either an empty form or some source code it created. What you see depends upon some internal variables that Visual Studio maintains from previous sessions.
  11. 10a. If you see a form, double-click on the form to switch to the source code.
    10b. If you see program code, you are where you need to be.
  12. Place your cursor in the Source window and press Ctrl-A to highlight all of the code in the Source window. Now press the Delete key on your keyboard.
  13. Now type in the code shown in Listing 2-1 into the Source window. The code in Listing 2-1 is used in almost every program you will write, so you might want to make a copy of it and save it in a place where you can recall it for later reuse.
  14. As in Step 10, what you do now depends upon some internal variables used by Visual Studio. If you look in the Solution Explorer window and see that some references have been added to the Reference folder (e.g., System, System.Windows.Forms, etc.), proceed to Step 14. If there are no reference, follow Step 13a.
  15. 13a. Go to the main menu and select Project --> Add Reference. Once the Reference Manager dialog appears, make sure the Assemblies --> Framework menu sequence is select. (It is the default.) Scroll the list presented in the center window until you see System.
    13b. Click on System, and check the box that appears to the left of System. This means that the System library will be added to your project. Every program you write will use this library.
    13c. Now scroll down further until you see System.Windows.Forms. Click on that library and check the box so it, too, is added to your project. Now click OK.
  16. In the Solution Explorer window, click on the small triangle just to the left of the frmMain.cs form you just added to the project. When you do this, you will see a file named frmMain.Designer.cs has been automatically added to your project. Click on the file and press the Delete key to remove the file from the project. (Visual Studio will ask you to confirm the deletion.)
  17. Go to the main menu and select the Project --> Chapter02Program01 Properties menu selection. The projects Properties always is the last item in the Project menu. You can also get to the applications Properties by pressing Alt-F7. The Application submenu will show choices similar to those seen in Figure 2-10.
  18. Change the Output type option by clicking the drop down list option (click the small down-arrow at the end of the Output type field) to Windows Application.
  19. Also change Startup object from Not set to frmMain.
  20. If you can click on the green Start arrow now and the empty form appears, you will NOT have to perform the next step. However, some early Beta versions of Visual Studio 2012 had a bug that prevented the program from running correctly. The following instructions will likely fix the bug.
  21. 18a. If you look in the Properites window (as indicated by the Chapter02Program01 tab at the top of the Source window), the fourth option below the Application option is named Debug. Click on Debug to show the debugging options.
    18b. Uncheck the Enable the Visual Studio hosting process.
    18c. Check the Enable native code debugging
    18d. Now click the Start button on the main menu. The program should show an empty form.
01/05/2014
439 Error in Text
The reference in paragraph 3 currently reads:

Figure 14-10

Should read:

Figure 14-11
12/17/2012
See More
Instructors Resources
Wiley Instructor Companion Site
See More
See Less

Related Titles

Back to Top