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.
Author(s): Jack Purdum
Publisher: Wrox Press
Year: 2012
Language: English
Pages: 624
Beginning Object-Oriented Programming with C#
Contents
Introduction
Part I: Getting Started
Chapter 1: Introducing C#
A Short History of Object-Oriented Programming (OOP)
Installing C#
Downloading Visual Studio Professional
A Test Program Using C#
Creating a Project
The C# Integrated Development Environment
The Major IDE Windows
Using the Source Code Window
Adding a Toolbox Object to a Windows Form
Changing the Text of a Label Object
Running the Program
Summary
Chapter 2: Understanding Objects
Understanding Objects
Everyday Use of Objects
Class Properties
Class Methods
How Many Properties, How Many Methods?
Classes Are Simplifications of Objects
What Do I Do After I’ve Defined a Class?
Instantiating an Object of a Class
I Have an Object…Now What?
Why Hide the Data Inside an Object?
Getting Started with Objects
Developing a Program Plan
Where to Start? The Five Program Steps
The Five Program Steps
Creating a Simple Application Using Objects
Using the Program Steps to Create a Program Plan
1: Initialization
2: Input
3: Process
4: Display
5: Termination
Using C# to Implement Your Program Plan
Critique of the btnDisplayOutput Click Event Code
Summary
Part II: Understanding C# Syntax
Chapter 3: Understanding Data Types
Computer Data
Integer Data Types
Range of Integer Data Types
Understanding Binary Numbers
Signed Integer Values
Which Integer Should You Use?
Variable Naming Rules and Conventions
Floating-Point Data Types
Which Floating-Point Data Type Should You Use?
Monetary Values: The Decimal Data Type
Using IntelliSense to Locate Program Errors
Syntax Rules and Error Messages
The Boolean Data Type
Summary
Chapter 4: Understanding C# Statements
Basic Building Blocks of a Programming Language
Operands and Operators
Expressions
Statements
Operator Precedence
Overriding the Default Precedence Order
Defining Variables
Defining a Variable from the Compiler’s Point of View
Step 1: Preliminary Syntax Checking
Step 2: Symbol Table Checking
Step 3: Defining a Variable
Using a Variable in a Program
The Bucket Analogy
Types of Program Errors
Syntax Errors
Semantic Errors
Logic Errors
The Visual Studio Debugger
The Debugging Process
Making Repetitious Debugging Easier
Using the Visual Studio Debugger
Setting a Breakpoint
Using a Breakpoint to Examine Variables
Single-Stepping the Program
Defensive Coding
Write Your Code So That Someone Else Can Easily Understand It
Use Program Comments
Use Meaningful Variable Names
Avoid Magic Numbers
Use a Consistent Coding Style
Take a Break
Use a Second Set of Eyes
Summary
Chapter 5: Understanding Reference Data Types
String Variables
Defining a String Reference Variable
The Meaning of null
Why Reference Types Are Different from Value Types
Reference Variable Rules
Reference Type Variables Versus Value Type Variables
Why Do Reference Variables Work the Way They Do?
Pass by Value Versus Pass by Reference
A Little Efficiency Gain
Using String Variables
String Concatenation
Shorthand Assignment Operators
String Manipulation
String Length
Letting IntelliSense Show You Properties and Methods
Using an IntelliSense Option
An Important Distinction Between Properties and Methods
Thinking About Object Properties and Methods
Strings and Escape Sequences
Verbatim String Literals
DateTime Reference Objects
DateTime and ToString( ) Conversions
Class Constructors
Invoking the Application
The UpdateTimeInfo( ) Method
Overloaded Methods
Method Signatures
The Refresh Button
Summary
Chapter 6: Making Decisions in Code
Relational Operators
Using Relational Operators—The if Statement
The if-else Statement
Shorthand for Simple if-else: The Ternary Operator
Style Considerations for if and if-else Statements
Nested if Statements
RDC
Logical Operators
Using the Logical Operators
Associativity
The switch Statement
Summary
Chapter 7: Statement Repetition Using Loops
Program Loops
Good Loops, Bad Loops
The Three Conditions of a Well-Behaved Loop
The for Loop
Increment and Decrement Operators
Sequencing in a for Loop
When to Use a for Loop
Nested for Loops
Use the Debugger as a Learning Tool
while Loops
Why Have More Than One Type of Loop?
do-while Program Loops
The continue Statement
Summary
Chapter 8: Understanding Arrays and Collections
What Is an Array?
Some Array Details
Array Element Versus Array Index
N – 1 Rule
Casts
The ListView Object
Arrays Are Objects
Multidimensional Arrays
Initializing Arrays
Variations for Initializing an Array
Which Array Definition/Initialization Syntax Is Best?
Code Like a User
Initializing Multidimensional Arrays
Initializer Lists for String Objects
Ragged Arrays
Defining Ragged Arrays at Runtime
Collections
ArrayList Objects
Summary
Part III: Writing Your Own Classes
Chapter 9: Designing Classes
Class Design
Scope
Block Scope
Local Scope
Class Scope
Namespace Scope
Visualizing Scope
Why Does C# Support Scope?
Think Before You Write
Designing a Program
The Five Program Steps
Initialization Step
Input Step
Process Step
Display Step
Termination Step
Look at the Forest, Not Just the Trees
UML Light
Access Specifiers
Access Specifiers and Scope
The static Keyword
UML Methods
Design Intent and Access Specifiers for Methods
Class Property and Method Names
Think Like a User
The clsDates Design
namespace Modifier
Class Organization
static Data Versus Instance Members
Property and Helper Methods
General Methods
User Interfaces Versus User Interfaces
Summary
Chapter 10: Designing and Writing Custom Classes
Constructors
Default Constructors
Nondefault Constructors
Constructor Overloading
Constructor Sloppiness
Fixing the Constructor Problem
Always Call the Default Constructor
Property Methods
Property Methods and Getters and Setters
Property Method Rules
How the get Property Methods Work
How Does Visual Studio Know Whether to Use the get or set Statement Block?
What to Do if an Error Occurs in a Property Method
Method Coupling and Cohesion
Cohesion
Coupling
Class Design for Deck-of-Cards Program
UML Class Diagram for Shuffle Deck Program
Class Constants and Properties
Class Methods
The clsCardDeck Code
Class Properties, Constructor, and Property Methods
Class General Methods
Designing a Card Game Using clsCardDeck
Design Considerations
What Kind of Architecture?
Sideways Refinement
Summary
Chapter 11: Exception Handling and Debugging
Overview
Bugs
Syntax Errors
Semantic Errors
Logic Errors
Input Errors
Data Validation
Limit User Input
Check Boxes
Combination Boxes
Date and Time Input
Exception Handling
try-catch Statement Blocks
Anticipating a Specific Exception
Fuzzy Exception Messages
The finally Statement Block
Program Debugging
The Nature of the Beast
Detection
Isolation
The Visual Studio Debugger
The Locals Window
The Immediate Window
Single-Stepping Through the Program
Backing Up from a Breakpoint
The Debug Toolbar
Finding the Bug
Scaffold Code
Toggling Scaffold Code
Defensive Coding
Summary
Chapter 12: Generics
What Are Generics?
Recursion
Data Problems
Introduction to Generics
Generics Versus ArrayLists
Boxing and Unboxing
Generic Quicksort
Using Generics with Constraints and Interfaces
Interfaces
Why Use an Interface?
Using an Interface
How Do You Know an Interface Is Implemented for a Data Type?
Summary
Part IV: Storing Data
Chapter 13: Using Disk Data Files
Directories
The DriveInfo Class
Directory Class
DirectoryInfo Class
File Class
FileInfo Class
Types of Files
Textual Versus Binary Data Files
Sequential Versus Random Access Files
Sequential Files
Advantages and Disadvantages of Sequential Files
Random Access Files
Fixed Record Sizes
Advantages and Disadvantages of Random Access Files
Serialization and Deserialization
To Serialize or Not to Serialize
MDI, Menus, and File Dialogs
Summary
Chapter 14: Using Databases
What Is a Database?
The Structure of a Database
Database Tables, Fields, and Records
Designing Fields for a Database Table
Data Normalization
Creating Your Own Databases
Using SQL Server Management Studio
Using SQL
The SELECT Statement
The WHERE Predicate
The ORDER BY Clause
Aggregates
A DBMS System
Displaying Database Data Without Data Binding
Performing the Query
Edit a Record (UPDATE)
Summary
Chapter 15: Using LINQ
Using LINQ
Query Keywords
Summary
Part V: Advanced Topics
Chapter 16: Inheritance and Polymorphism
What Is Inheritance?
An Inheritance Example
The Base and Derived Classes
The protected Access Specifier
Advantages of Inherited Relationships
Base Classes Are Not Derived Classes
Abstract Classes
Polymorphism
Extension Methods
Summary
Chapter 17: Printing and Threading
Using a Printer
Threading
A Threading Program
What Have You Gained with Threading?
Refactoring
Rename
Extract Method
Summary
Chapter 18: Web Programming
Static Web Pages
Dynamic Web Pages
Summary
Appendix: Answers to Exercises
Index
Advertisement