0% found this document useful (0 votes)
4 views35 pages

3422

The document provides a link to download the PDF of 'C Primer Plus 1st Edition' by Klaus Michelsen, along with other recommended educational resources. It includes details about the book such as its ISBN, file size, and publication year. Additionally, it outlines the contents and structure of the book, covering various programming concepts and C# programming techniques.

Uploaded by

zqerrle522
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views35 pages

3422

The document provides a link to download the PDF of 'C Primer Plus 1st Edition' by Klaus Michelsen, along with other recommended educational resources. It includes details about the book such as its ISBN, file size, and publication year. Additionally, it outlines the contents and structure of the book, covering various programming concepts and C# programming techniques.

Uploaded by

zqerrle522
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 35

C Primer Plus 1st Edition Klaus Michelsen download

pdf

https://ebookfinal.com/download/c-primer-plus-1st-edition-klaus-
michelsen/

Visit ebookfinal.com today to download the complete set of


ebook or textbook!
Here are some recommended products that we believe you will be
interested in. You can click the link to download.

C Primer Plus Fifth Edition Chrisv

https://ebookfinal.com/download/c-primer-plus-fifth-edition-chrisv/

C Primer Plus 5th ed Edition Stephen Prata

https://ebookfinal.com/download/c-primer-plus-5th-ed-edition-stephen-
prata/

Using the TI 83 Plus TI 84 Plus Christopher R. Mitchell

https://ebookfinal.com/download/using-the-ti-83-plus-ti-84-plus-
christopher-r-mitchell/

Organizational Behavior Wiley Plus Stand alone Wiley Plus


Products 7th edition John Schermerhorn

https://ebookfinal.com/download/organizational-behavior-wiley-plus-
stand-alone-wiley-plus-products-7th-edition-john-schermerhorn/
Politics and Suicide The philosophy of political self
destruction 1st Edition Nicholas Michelsen

https://ebookfinal.com/download/politics-and-suicide-the-philosophy-
of-political-self-destruction-1st-edition-nicholas-michelsen/

Unmapping the 21st Century Between Networks and the State


1st Edition Nicholas Michelsen

https://ebookfinal.com/download/unmapping-the-21st-century-between-
networks-and-the-state-1st-edition-nicholas-michelsen/

Kohler s Invention 1st Edition Klaus Eichmann

https://ebookfinal.com/download/kohler-s-invention-1st-edition-klaus-
eichmann/

Aesthetics Primer Peter Lang Primer First Printing Edition


Boyd White

https://ebookfinal.com/download/aesthetics-primer-peter-lang-primer-
first-printing-edition-boyd-white/

Arts Based Research Primer Peter Lang Primer 1st Edition


James Rolling Haywood Jr.

https://ebookfinal.com/download/arts-based-research-primer-peter-lang-
primer-1st-edition-james-rolling-haywood-jr/
C Primer Plus 1st Edition Klaus Michelsen Digital Instant
Download
Author(s): Klaus Michelsen
ISBN(s): 9780672321528, 0672321521
Edition: 1
File Details: PDF, 3.16 MB
Year: 2001
Language: english
C#
Primer Plus

Klaus Michelsen

201 West 103rd St., Indianapolis, Indiana, 46290 USA


C# Primer Plus ASSOCIATE PUBLISHER
Jeff Koch
Copyright © 2002 by Sams Publishing ACQUISITIONS EDITOR
All rights reserved. No part of this book shall be reproduced, stored in a retrieval system, Neil Rowe
or transmitted by any means, electronic, mechanical, photocopying, recording, or otherwise,
DEVELOPMENT EDITOR
without written permission from the publisher. No patent liability is assumed with respect
Kevin Howard
to the use of the information contained herein. Although every precaution has been taken
in the preparation of this book, the publisher and author assume no responsibility for errors MANAGING EDITOR
or omissions. Nor is any liability assumed for damages resulting from the use of the infor- Matt Purcell
mation contained herein.
PROJECT EDITOR
International Standard Book Number: 0-672-32152-1 George E. Nedeff
Library of Congress Catalog Card Number: 2001089509
COPY EDITOR
Printed in the United States of America Pat Kinyon
First Printing: November 2001
INDEXER
04 03 02 01 4 3 2 1 Rebecca Salerno
PROOFREADER

Trademarks Plan-It Publishing


TECHNICAL EDITOR
All terms mentioned in this book that are known to be trademarks or service marks have Brad Shannon
been appropriately capitalized. Sams Publishing cannot attest to the accuracy of this infor-
mation. Use of a term in this book should not be regarded as affecting the validity of any TEAM COORDINATOR
trademark or service mark. Denni Bannister
MEDIA DEVELOPER
Dan Scherf
Warning and Disclaimer INTERIOR DESIGNER
Every effort has been made to make this book as complete and as accurate as possible, but Gary Designer
no warranty or fitness is implied. The information provided is on an “as is” basis.
COVER DESIGNER
Alan Clements
PAGE LAYOUT
Plan-It Publishing
Brad Lenser
CONTENTS AT A GLANCE
INTRODUCTION . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1
CHAPTER 1 Computers and Computer Programming: Basic Concepts . . . . . . . .7
CHAPTER 2 Your First C# Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .23
CHAPTER 3 A Guided Tour through C#: Part I . . . . . . . . . . . . . . . . . . . . . . . . .59
CHAPTER 4 A Guided Tour through C#: Part II . . . . . . . . . . . . . . . . . . . . . . . .91
CHAPTER 5 Your First Object-Oriented C# Program . . . . . . . . . . . . . . . . . . .111
CHAPTER 6 Types Part I: The Simple Types . . . . . . . . . . . . . . . . . . . . . . . . . .141
CHAPTER 7 Types Part II: Operators, Enumerators and Strings . . . . . . . . . . .201
CHAPTER 8 Flow of Control Part I: Branching Statements and
Related Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .271
CHAPTER 9 Flow of Control Part II: Iteration Statements . . . . . . . . . . . . . . . .329
CHAPTER 10 Arrays Part I: Array Essentials . . . . . . . . . . . . . . . . . . . . . . . . . . .367
CHAPTER 11 Arrays Part II: Multidimensional Arrays—
Searching and Sorting Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . .427
CHAPTER 12 Class Anatomy Part I: static Class Members
and Method Adventures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .481
CHAPTER 13 Class Anatomy Part II: Object Creation and
Garbage Collection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .535
CHAPTER 14 Class Anatomy Part III: Writing Intuitive Code . . . . . . . . . . . . . .583
CHAPTER 15 Namespaces, Compilation Units and Assemblies . . . . . . . . . . . . .635
CHAPTER 16 Inheritance Part I: Basic Concepts . . . . . . . . . . . . . . . . . . . . . . . .663
CHAPTER 17 Inheritance Part II: abstract Functions, Polymorphism,
and Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .707
CHAPTER 18 Structs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .761
CHAPTER 19 Exception Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .771
CHAPTER 20 Delegates and Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .793
CHAPTER 21 Preprocessing, XML Documentation, and Attributes . . . . . . . . . .815
CHAPTER 22 File I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .833
CHAPTER 23 Recursion Fundamentals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .851
APPENDIX A Answers to Quizzes and Exercises . . . . . . . . . . . . . . . . . . . . . . . .867
INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . .955
TABLE OF CONTENTS

I N T R O D U C T I O N . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1

C H A P T E R 1 : Computers and Computer Programming: Basic Concepts . . . . . . . . .7


C# Is an Object-Oriented Programming Language . . . . . . . . . . . . . . . . . . . . . .9
Computer Organization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .9
Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .10
The Nature of Main Memory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .11
The Number Systems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .12
Bytes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .13
Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .14
Software . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .15
Overall Process of Executing a C# Program . . . . . . . . . . . . . . . . . . . . . . . .15
The Operating System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .16
Programming Languages and Compilers . . . . . . . . . . . . . . . . . . . . . . . . . .16
Introducing .NET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .17
Compiling C# Source Code in .NET . . . . . . . . . . . . . . . . . . . . . . . . . . . . .18
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .20
Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .21
C H A P T E R 2 : Your First C# Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .23
Software Development . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .23
The Software Development Process . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .24
Algorithms and Pseudocode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .26
The Three Different Types of Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .28
Object-Oriented Programming: A First Encounter . . . . . . . . . . . . . . . . . . . . .29
Process-Oriented Programming and Its Innate Problems . . . . . . . . . . . . . .30
Object-Oriented Programming and Its Advantages . . . . . . . . . . . . . . . . . .30
Software Reuse . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .34
The Assembly, the Basic Unit of Code Reuse in .NET . . . . . . . . . . . . . . . . . . .36
The Assembly Developer’s Viewpoint . . . . . . . . . . . . . . . . . . . . . . . . . . . . .37
The Assembly User’s Viewpoint . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .39
The .NET Framework Class Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .40
C#: History and Design Goals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .41
C#’s Background . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .41
C#’s Design Goals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .44
What Kind of Programs Can I Write with C#? . . . . . . . . . . . . . . . . . . . . . . . .45
The Mechanics of Creating a C# Program . . . . . . . . . . . . . . . . . . . . . . . . . . . .46
Before Getting Started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .47
Choosing a Text Editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .48
The Seven Steps to Writing a Simple C# Program . . . . . . . . . . . . . . . . . . . . . .48
Opening and Using the Command Console (Step 1) . . . . . . . . . . . . . . . . .49
Typing and Saving the C# Source Code (Step 2) . . . . . . . . . . . . . . . . . . . .50
Turn the Source Code into a PE (.exe) File (Step 3) . . . . . . . . . . . . . . . . .52
If the Compiler Detects Syntax Errors (Step 4) . . . . . . . . . . . . . . . . . . . . . .53
Run the Program (Step 5) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .54
Verify the Output (Step 6) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .54
Time to Celebrate (Step 7) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .54
A Brief Source Code Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .54
Note on Syntax Errors and Compilers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .55
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .56
Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .57
Programming Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .58
C H A P T E R 3 : A Guided Tour through C#: Part I . . . . . . . . . . . . . . . . . . . . . . . . . .59
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .60
Abstraction and Encapsulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .60
Abstraction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .60
Encapsulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .61
A Note on Learning Object-Oriented Programming . . . . . . . . . . . . . . . . . .67
An Interactive Hello World! Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .68
Presenting Hello.cs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .68
Basic Elements of Hello.cs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .70
A Few Fundamental Observations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .82
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .86
Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .88
Programming Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .88
C H A P T E R 4 : A Guided Tour through C#: Part II . . . . . . . . . . . . . . . . . . . . . . . . .91
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .91
Essential Elements of SimpleCalculator.cs . . . . . . . . . . . . . . . . . . . . . . . . .91
Presenting SimpleCalculator.cs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .92
A Closer Look at SimpleCalculator.cs . . . . . . . . . . . . . . . . . . . . . . . . . .94
Simplifying Your Code with Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .105
Methods as Building Blocks: Encapsulating Your Helper Methods
with the private keyword. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .106
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .108
Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .109
Programming Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .110
vi C# PRIMER PLUS

C H A P T E R 5 : Your First Object-Oriented C# Program . . . . . . . . . . . . . . . . . . . .111


Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .111
Lexical Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .112
Identifiers and CaPitaLIzaTioN Styles . . . . . . . . . . . . . . . . . . . . . . . . . . .112
Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .113
Comments and Source Code Documentation . . . . . . . . . . . . . . . . . . . . . .113
Separators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .113
Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .114
Keywords . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .115
Some Thoughts on Elevator Simulations . . . . . . . . . . . . . . . . . . . . . . . . . . . .115
Concepts, Goals and Solutions in an Elevator Simulation
Program: Collecting Valuable Statistics for Evaluating an Elevator System .116
Object-Oriented Programming: A Practical Example . . . . . . . . . . . . . . . . . .117
Presenting SimpleElevatorSimulation.cs . . . . . . . . . . . . . . . . . . . . . . .118
Overall Structure of the Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .121
A Deeper Analysis of SimpleElevatorSimulation.cs . . . . . . . . . . . . . . .123
Class Relationships and UML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .133
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .137
Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .138
Programming Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .138
C H A P T E R 6 : Types Part I: The Simple Types . . . . . . . . . . . . . . . . . . . . . . . . . . .141
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .141
Types in C#: An Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .142
What Is a Type? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .142
C#: A Strongly Typed Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .143
The Advantages of Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .146
The Types of C# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .147
Simple Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .152
Overview of the Simple Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .154
Integer Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .158
The Blipos Clock . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .173
Floating-Point Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .182
The decimal Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .189
Floating-Point, decimal, and Integer Value Compatibilities . . . . . . . . . . .190
Explicit Type Conversions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .190
Constants: Symbolic Names for Literals . . . . . . . . . . . . . . . . . . . . . . . . . .192
Formatting Numeric Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .196
The bool Type: A Brief Encounter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .199
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .200
Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .201
Programming Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .202
CONTENTS vii

C H A P T E R 7 : Types Part II: Operators, Enumerators, and Strings . . . . . . . . . . . .203


Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .203
Arithmetic Operators and Numerical Expressions . . . . . . . . . . . . . . . . . . . .204
Binary Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .205
Associativity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .209
Parentheses and Precedence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .209
The Modulus Operator (%) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .211
Unary Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .216
Unary Plus and Unary Minus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .217
Increment and Decrement Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . .218
Determining the Type of an Expression . . . . . . . . . . . . . . . . . . . . . . . . . . . . .222
Conversions and the Unary Plus/Minus Operators . . . . . . . . . . . . . . . . . .223
Combining Different Types in One Expression . . . . . . . . . . . . . . . . . . . . . . .224
Accessing the Metadata of a Component: A Brief Introduction . . . . . . . . . . .228
Enumerated Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .232
Enumerator Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .236
Conversions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .236
The Methods of System.Enum . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .237
Characters and Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .237
The char Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .238
The Double Life of char . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .240
The string type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .242
string Literals and string Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . .242
Verbatim Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .243
Working with Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .244
Embedding Formatted Numbers in a string . . . . . . . . . . . . . . . . . . . . . .248
Working with strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .252
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .268
Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .269
Programming Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .270
C H A P T E R 8 : Flow of Control Part I: Branching Statements and
Related Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .271
Introduction to Flow of Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .271
Branching with the if Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .273
The Simple if Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .273
Compound Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .276
The Optional else Clause . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .276
Comparison Operators and Boolean Expressions . . . . . . . . . . . . . . . . . . . . .279
Nested if Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .282
viii C# PRIMER PLUS

Multibranch if-else Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .287


Only Use the default_statement for Genuine Defaults in
Multibranch if-else Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .290
Logical Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .292
The Logical AND Operator: && . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .294
The Logical OR Operator: || . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .297
Short Circuit Evaluation and the Bitwise Operators & and | . . . . . . . . . . .300
The Bitwise Exclusive OR Operator: ^ . . . . . . . . . . . . . . . . . . . . . . . . . . .302
The Logical NOT Operator: ! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .302
The Scope of Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .305
Scope and Lifetime of Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .308
The goto Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .309
The switch Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .311
switch Statement Rule 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .316
switch Statement Rule 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .316
switch Statement Rule 3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .319
switch Statement Rule 4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .321
Working with switch Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .322
The Conditional Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .323
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .324
Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .325
Programming Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .327
C H A P T E R 9 : Flow of Control Part II: Iteration Statements . . . . . . . . . . . . . . . . .329
Traversing, Analyzing, and Generating Sequences of Data . . . . . . . . . . . . . .330
The while Loop Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .331
The do-while Loop Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .336
The for Loop Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .339
The Jump Statements break and continue . . . . . . . . . . . . . . . . . . . . . . . . . .346
The break Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .346
The continue Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .348
Structured Programming and Structured Constructs . . . . . . . . . . . . . . . . . .349
Combination Assignment Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .352
Nested Iteration Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .354
Programming Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .361
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .363
Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .364
Programming Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .366
CONTENTS ix

C H A P T E R 1 0 : Arrays Part I: Array Essentials . . . . . . . . . . . . . . . . . . . . . . . . . . .367


Declaring and Defining an Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .368
Accessing Individual Array Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .372
Array Indexes Out of Range . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .378
Adjusting for the Zero-Based Array Index . . . . . . . . . . . . . . . . . . . . . . . . . . .382
Initializing Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .383
Traversing an Entire Array with the foreach Statement . . . . . . . . . . . . . . . .385
System.Array Is a Reference Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .386
Arrays and Equality . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .389
Arrays and Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .391
Array Elements As Method Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . .392
Array References As Method Arguments . . . . . . . . . . . . . . . . . . . . . . . . . .393
Cloning an Array Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .396
A Method to Perform Array Value Equality Comparisons . . . . . . . . . . . . . . .400
Command Line Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .402
Using Arrays As Return Values from Methods . . . . . . . . . . . . . . . . . . . . . . . .404
Arrays and Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .408
Array Elements Referencing Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .408
Arrays As Instance Variables in Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . .412
Case Study: Bank Simulation Program . . . . . . . . . . . . . . . . . . . . . . . . . . .413
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .421
Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .422
Programming Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .424
C H A P T E R 1 1 : Arrays Part II: Multidimensional Arrays—
Searching and Sorting Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .427
Multidimensional Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .428
Two-Dimensional Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .429
Jagged Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .447
Arrays of More Than Two Dimensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . .451
Accessing Multidimensional Arrays with the foreach Statement . . . . . . .454
The Built-In Methods of System.Array . . . . . . . . . . . . . . . . . . . . . . . . . . . .455
Special Array Techniques . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .458
Sorting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .458
Searching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .466
Sequential Search . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .467
Binary Search . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .469
Searching with System.Array’s IndexOf Method . . . . . . . . . . . . . . . . . . .475
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .476
Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .477
Programming Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .478
x C# PRIMER PLUS

C H A P T E R 1 2 : Class Anatomy Part I: static Class Members and


Method Adventures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .481
The Anatomy of a Class: Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .482
Data Members . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .484
Instance Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .484
static Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .486
Constant Members . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .493
readonly Members . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .494
Declaring Data Members: An Overview . . . . . . . . . . . . . . . . . . . . . . . . . .494
Function Members . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .495
Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .495
static Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .498
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .529
Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .531
Programming Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .533
C H A P T E R 1 3 : Class Anatomy Part II: Object Creation and
Garbage Collection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .535
Instance Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .536
Why Do We Need Instance Constructors? . . . . . . . . . . . . . . . . . . . . . . . .536
Working with Instance Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . .539
Overloading Instance Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . .544
The Constructor Initializer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .550
private Instance Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .553
static Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .553
The readonly Member . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .554
Garbage Collection: Automatic Dynamic Memory Management . . . . . . . . . .556
How Do Objects Get Out of Reach? . . . . . . . . . . . . . . . . . . . . . . . . . . . . .556
The Tasks of the Garbage Collector . . . . . . . . . . . . . . . . . . . . . . . . . . . . .560
Freeing Scarce Resources Other Than Memory . . . . . . . . . . . . . . . . . . . .562
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .577
Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .579
Programming Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .580
C H A P T E R 1 4 : Class Anatomy Part III: Writing Intuitive Code . . . . . . . . . . . . . .583
Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .584
Properties Versus Accessor and Mutator Methods . . . . . . . . . . . . . . . . . .585
Properties Are Speedy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .591
Implementing Delayed Initialization and Lazy Updates with Properties . .593
CONTENTS xi

Indexers: Using Objects Like Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .597


Calling an Indexer from within the Object It Inhabits . . . . . . . . . . . . . . .600
Indexer Overloading: Multiple Indexers in the Same Class . . . . . . . . . . .601
Avoid Overusing Indexers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .606
Operator Overloading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .607
User-Defined Operator Overloading . . . . . . . . . . . . . . . . . . . . . . . . . . . . .608
User-Defined Implicit and Explicit Conversions . . . . . . . . . . . . . . . . . . . . . .617
Two Cases in Need of User-Defined Conversions . . . . . . . . . . . . . . . . . . .618
Using Non-User-Defined Conversion Techniques . . . . . . . . . . . . . . . . . .619
The Syntax of User-Defined Conversions . . . . . . . . . . . . . . . . . . . . . . . . .620
Combining User-Defined and Implicit Conversions . . . . . . . . . . . . . . . . .626
Nested Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .628
The Advantages of Nested Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .629
A Simple Nested Class Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .629
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .630
Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .631
Programming Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .633
C H A P T E R 1 5 : Namespaces, Compilation Units, and Assemblies . . . . . . . . . . . .635
Defining Your Own Namespaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .635
The Global Nameless Namespace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .637
Namespaces and Compilation Units . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .637
Nested Namespaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .639
Namespace Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .642
More About the using Directive . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .643
Class and Namespace Aliases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .644
Compile Units, Namespaces, and Assemblies . . . . . . . . . . . . . . . . . . . . . . . .645
Compiling Several Compile Units into One Assembly . . . . . . . . . . . . . . .646
Reusing the Namespaces Contained in an Assembly . . . . . . . . . . . . . . . .650
Separating Namespaces into Several Assemblies . . . . . . . . . . . . . . . . . . . .652
Exploring Assemblies with the Ildasm Utility . . . . . . . . . . . . . . . . . . . . . . . .656
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .658
Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .659
Programming Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .661
C H A P T E R 1 6 : Inheritance Part I:Basic Concepts . . . . . . . . . . . . . . . . . . . . . . . .663
The Need for Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .664
Life Without Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .665
Inheritance Fundamentals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .672
Overriding Function Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .675
Access Modifiers and Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .680
The protected Access Modifier . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .680
Accessing private Base Class Members . . . . . . . . . . . . . . . . . . . . . . . . .681
xii C# PRIMER PLUS

The internal protected Access Modifier . . . . . . . . . . . . . . . . . . . . . . .682


C#’s Access Modifiers: An Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . .683
Derived Class Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .683
Indexers Are Also Inherited and Can Be Overridden . . . . . . . . . . . . . . . . . .688
Calling an Overridden Function in the Base Class . . . . . . . . . . . . . . . . . . . .691
Reusing the .NET Framework Class Library with Inheritance . . . . . . . . . . . .694
Multiple Levels of Derived Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .696
Method Overriding and Overloading Are Different Mechanisms . . . . . . . . . .699
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .701
Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .703
Programming Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .704
C H A P T E R 1 7 : Inheritance Part II: abstract Functions, Polymorphism,
and Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .707
abstract Methods, Properties, Indexers, and Classes . . . . . . . . . . . . . . . . . .708
Polymorphism . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .712
An Object of a Descendant Class Has More Than One Type . . . . . . . . . . .712
Dynamic Binding of Virtual Methods and (get, set) Accessors . . . . . . . .713
Case Study: Using Polymorphism to Create a Simple Drawing Program . .715
Losing and Regaining Type Information . . . . . . . . . . . . . . . . . . . . . . . . . . . .722
The is Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .723
Casting Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .724
The as Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .727
System.Object: The Ultimate Base Class . . . . . . . . . . . . . . . . . . . . . . . . . . .727
Method hiding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .733
Versioning with the new and override keywords . . . . . . . . . . . . . . . . . . . . . .735
Multiple inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .738
Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .739
Defining an interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .742
Implementing an Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .743
Generic Programming with Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . .747
Building Interface Hierarchies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .751
Interface Conversions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .752
Overriding Virtual Interface Implementations . . . . . . . . . . . . . . . . . . . . .752
Implementing Interface Functions Explicitly . . . . . . . . . . . . . . . . . . . . . .753
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .756
Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .757
Programming Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .759
C H A P T E R 1 8 : Structs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .761
Defining a Struct . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .762
Boxing and Unboxing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .764
Creating Structs with and without new . . . . . . . . . . . . . . . . . . . . . . . . . . . . .766
CONTENTS xiii

Value Types and Reference Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .766


Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .768
Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .768
Programming Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .769
C H A P T E R 1 9 : Exception Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .771
Exception Handling: A Brief Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . .771
Life without try-catch-finally . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .773
The try and catch blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .775
Catching the Exception Object Farther up the Function Call Chain . . . . . . .778
Multiple catch Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .779
The finally Block . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .780
Nested try Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .783
throw: Explicitly Throwing Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . .784
Writing Custom Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .787
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .790
Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .790
Programming Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .791
C H A P T E R 2 0 : Delegates and Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .793
Delegates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .794
Arrays of Delegates and Delegates As Arguments to Method Calls . . . . . .797
Multicast Delegates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .801
Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .804
Writing Event-Driven Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .805
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .812
Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .812
Programming Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .813
C H A P T E R 2 1 : Preprocessing, XML Documentation, and Attributes . . . . . . . . . .815
Preprocessor Directives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .816
Exclude and Include Code with #define, #if, and #endif . . . . . . . . . . .816
#undef: Undefining Identifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .818
#elif and #else . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .818
#error and #warning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .819
#region and #endregion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .819
#line . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .819
XML Documentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .820
A Simple Documentation Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .820
Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .822
A Simple Attribute Annotation Example . . . . . . . . . . . . . . . . . . . . . . . . .825
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .828
Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .828
Programming Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .830
xiv C# PRIMER PLUS

C H A P T E R 2 2 : File I/O Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .833


The Need for Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .833
Stream Basics and File I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .834
Text Files and Binary Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .834
File I/O Classes: An Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .835
The FileInfo Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .836
Text File Input and Output with StreamReader and StreamWriter . . . . . . .839
Binary Input and Output with the FileStream Class . . . . . . . . . . . . . . . . . .844
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .847
Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .847
Programming Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .848
C H A P T E R 2 3 : Recursion Fundamentals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .851
Pending Method Instances of Different Methods . . . . . . . . . . . . . . . . . . . . . .852
Pending Method Instances of the Same Method . . . . . . . . . . . . . . . . . . . . . .853
Putting Recursion to Work: Calculating n Factorial . . . . . . . . . . . . . . . . . . .857
Recursion and Iteration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .860
Binary Search Using Recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .861
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .864
Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .865
Programming Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .866
A P P E N D I X A : Answers to Quizzes and Exercises . . . . . . . . . . . . . . . . . . . . . . .867
Chapter 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .867
Answers to Chapter 1 Review Questions . . . . . . . . . . . . . . . . . . . . . . . . .867
Chapter 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .868
Answers to Chapter 2 Review Questions . . . . . . . . . . . . . . . . . . . . . . . . .868
Answers to Chapter 2 Programming Exercises . . . . . . . . . . . . . . . . . . . . .870
Chapter 3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .870
Answers to Chapter 3 Review Questions . . . . . . . . . . . . . . . . . . . . . . . . .870
Answers to Chapter 3 Programming Exercises . . . . . . . . . . . . . . . . . . . . .872
Chapter 4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .872
Answers to Chapter 4 Review Questions . . . . . . . . . . . . . . . . . . . . . . . . .872
Answers to Chapter 4 Programming Exercises . . . . . . . . . . . . . . . . . . . . .873
Chapter 5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .876
Answers to Chapter 5 Review Questions . . . . . . . . . . . . . . . . . . . . . . . . .876
Answers to Programming Exercises in Chapter 5 . . . . . . . . . . . . . . . . . . .877
Chapter 6 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .878
Answers to Chapter 6 Review Questions . . . . . . . . . . . . . . . . . . . . . . . . .878
Answers to Chapter 6 Programming Exercises . . . . . . . . . . . . . . . . . . . . .879
Chapter 7 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .885
Answers to Chapter 7 Review Questions . . . . . . . . . . . . . . . . . . . . . . . . .885
Answers to Chapter 7 Programming Exercises . . . . . . . . . . . . . . . . . . . . .886
CONTENTS xv

Chapter 8 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .889
Answers to Chapter 8 Review Questions . . . . . . . . . . . . . . . . . . . . . . . . .889
Answers to Chapter 8 Programming Exercises . . . . . . . . . . . . . . . . . . . . .890
Chapter 9 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .892
Answers to Chapter 9 Review Questions . . . . . . . . . . . . . . . . . . . . . . . . .892
Answers to Chapter 9 Programming Exercises . . . . . . . . . . . . . . . . . . . . .894
Chapter 10 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .895
Answers to Chapter 10 Review Questions . . . . . . . . . . . . . . . . . . . . . . . .895
Answers to Chapter 10 Programming Exercises . . . . . . . . . . . . . . . . . . . .897
Chapter 11 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .899
Answers to Chapter 11 Review Questions . . . . . . . . . . . . . . . . . . . . . . . .899
Answers to Chapter 11 Programming Exercises . . . . . . . . . . . . . . . . . . . .900
Chapter 12 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .905
Answers to Chapter 12 Review Questions . . . . . . . . . . . . . . . . . . . . . . . .905
Answers to Chapter 12 Programming Exercises . . . . . . . . . . . . . . . . . . . .907
Chapter 13 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .910
Answers to Chapter 13 Review Questions . . . . . . . . . . . . . . . . . . . . . . . .910
Answers to Chapter 13 Programming Exercises . . . . . . . . . . . . . . . . . . . .912
Chapter 14 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .913
Answers to Chapter 14 Review Questions . . . . . . . . . . . . . . . . . . . . . . . .913
Answers to Chapter 14 Programming Exercises . . . . . . . . . . . . . . . . . . . .915
Chapter 15 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .919
Answers to Chapter 15 Review Questions . . . . . . . . . . . . . . . . . . . . . . . .919
Answers to Chapter 15 Programming Exercises . . . . . . . . . . . . . . . . . . . .921
Chapter 16 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .923
Answers to Chapter 16 Review Questions . . . . . . . . . . . . . . . . . . . . . . . .923
Answers to Chapter 16 Programming Exercises . . . . . . . . . . . . . . . . . . . .924
Chapter 17 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .927
Answers to Chapter 17 Review Questions . . . . . . . . . . . . . . . . . . . . . . . .927
Answers to Chapter 17 Programming Exercises . . . . . . . . . . . . . . . . . . . .929
Chapter 18 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .933
Answers to Chapter 18 Review Questions . . . . . . . . . . . . . . . . . . . . . . . .933
Answers to Chapter 18 Programming Exercises . . . . . . . . . . . . . . . . . . . .934
Chapter 19 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .935
Answers to Chapter 19 Review Questions . . . . . . . . . . . . . . . . . . . . . . . .935
Answers to Chapter 19 Programming Exercises . . . . . . . . . . . . . . . . . . . .936
Chapter 20 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .938
Answers to Chapter 20 Review Questions . . . . . . . . . . . . . . . . . . . . . . . .938
Answers to Chapter 20 Programming Exercises . . . . . . . . . . . . . . . . . . . .939
Chapter 21 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .944
Answers to Chapter 21 Review Questions . . . . . . . . . . . . . . . . . . . . . . . .944
Answers to Chapter 21 Programming Exercises . . . . . . . . . . . . . . . . . . . .946
xvi C# PRIMER PLUS

Chapter 22 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .948
Answers to Chapter 22 Review Questions . . . . . . . . . . . . . . . . . . . . . . . .948
Answers to Chapter 22 Programming Exercises . . . . . . . . . . . . . . . . . . . .949
Chapter 23 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .952
Answers to Chapter 23 Review Questions . . . . . . . . . . . . . . . . . . . . . . . .952
Answers to Chapter 23 Programming Exercises . . . . . . . . . . . . . . . . . . . .953
I N D E X . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .955
CONTENTS xvii

ABOUT THE AUTHOR


Klaus Michelsen has a Masters degree in Economics from Aarhus University in Denmark. His
studies included artificial intelligence and courses in advanced computer science at Sydney
University in Australia. Since his first programming assignment for a shoe shop when he was
18, he has, during his 15 years of programming experience, been exposed to many different
technologies and programming languages, including Java™, Visual J++, C++, MS Access, VB,
Eiffel, and Pascal. Klaus has been working with C# and .NET since their introduction in the
year 2000.
xviii C# PRIMER PLUS

DEDICATION

To Deborah,

My friend, my love, my wife.


xix

ACKNOWLEDGMENTS
I would like to thank my wife Deborah who, despite my obstinate tendency to turn into a her-
mit during this project and my countless late nights of writing frenzies, is still putting up with
me. Without you and your boundless support, this book project would have been impossible.
Without you and our young boy Julian, any project in my life would seem meaningless. I love
you!
I would like to thank the team of editors, coordinators, and technicians at Sams Publishing for
putting their hard work and invaluable expertise into this project. I’m especially grateful to
Neil Rowe, Acquisitions Editor, for initially contacting me, encouraging me to engage in this
project, and for his solid support during the whole process. Thanks for taking a chance with a
new writer; it has changed my life forever.
I would like to thank my mother Vita Lund Michelsen, for her strong support and incessant
encouragement throughout my life and in particular during the months I was writing. Thanks
to my sister Kirsten for keeping the spirits up on the other side of the world, even during diffi-
cult times; this gave me peace of mind to focus on the book.
Lastly, thanks to my two dogs, Charlie and Honey, and the great composers Mozart and
Beethoven who all kept me company on many a late night.
Random documents with unrelated
content Scribd suggests to you:
Gutenberg” is associated) is accessed, displayed, performed,
viewed, copied or distributed:

This eBook is for the use of anyone anywhere in the United


States and most other parts of the world at no cost and with
almost no restrictions whatsoever. You may copy it, give it
away or re-use it under the terms of the Project Gutenberg
License included with this eBook or online at
www.gutenberg.org. If you are not located in the United
States, you will have to check the laws of the country where
you are located before using this eBook.

1.E.2. If an individual Project Gutenberg™ electronic work is


derived from texts not protected by U.S. copyright law (does not
contain a notice indicating that it is posted with permission of the
copyright holder), the work can be copied and distributed to
anyone in the United States without paying any fees or charges.
If you are redistributing or providing access to a work with the
phrase “Project Gutenberg” associated with or appearing on the
work, you must comply either with the requirements of
paragraphs 1.E.1 through 1.E.7 or obtain permission for the use
of the work and the Project Gutenberg™ trademark as set forth
in paragraphs 1.E.8 or 1.E.9.

1.E.3. If an individual Project Gutenberg™ electronic work is


posted with the permission of the copyright holder, your use and
distribution must comply with both paragraphs 1.E.1 through
1.E.7 and any additional terms imposed by the copyright holder.
Additional terms will be linked to the Project Gutenberg™
License for all works posted with the permission of the copyright
holder found at the beginning of this work.

1.E.4. Do not unlink or detach or remove the full Project


Gutenberg™ License terms from this work, or any files
containing a part of this work or any other work associated with
Project Gutenberg™.
1.E.5. Do not copy, display, perform, distribute or redistribute
this electronic work, or any part of this electronic work, without
prominently displaying the sentence set forth in paragraph 1.E.1
with active links or immediate access to the full terms of the
Project Gutenberg™ License.

1.E.6. You may convert to and distribute this work in any binary,
compressed, marked up, nonproprietary or proprietary form,
including any word processing or hypertext form. However, if
you provide access to or distribute copies of a Project
Gutenberg™ work in a format other than “Plain Vanilla ASCII” or
other format used in the official version posted on the official
Project Gutenberg™ website (www.gutenberg.org), you must, at
no additional cost, fee or expense to the user, provide a copy, a
means of exporting a copy, or a means of obtaining a copy upon
request, of the work in its original “Plain Vanilla ASCII” or other
form. Any alternate format must include the full Project
Gutenberg™ License as specified in paragraph 1.E.1.

1.E.7. Do not charge a fee for access to, viewing, displaying,


performing, copying or distributing any Project Gutenberg™
works unless you comply with paragraph 1.E.8 or 1.E.9.

1.E.8. You may charge a reasonable fee for copies of or


providing access to or distributing Project Gutenberg™
electronic works provided that:

• You pay a royalty fee of 20% of the gross profits you derive from
the use of Project Gutenberg™ works calculated using the
method you already use to calculate your applicable taxes. The
fee is owed to the owner of the Project Gutenberg™ trademark,
but he has agreed to donate royalties under this paragraph to
the Project Gutenberg Literary Archive Foundation. Royalty
payments must be paid within 60 days following each date on
which you prepare (or are legally required to prepare) your
periodic tax returns. Royalty payments should be clearly marked
as such and sent to the Project Gutenberg Literary Archive
Foundation at the address specified in Section 4, “Information
about donations to the Project Gutenberg Literary Archive
Foundation.”

• You provide a full refund of any money paid by a user who


notifies you in writing (or by e-mail) within 30 days of receipt that
s/he does not agree to the terms of the full Project Gutenberg™
License. You must require such a user to return or destroy all
copies of the works possessed in a physical medium and
discontinue all use of and all access to other copies of Project
Gutenberg™ works.

• You provide, in accordance with paragraph 1.F.3, a full refund of


any money paid for a work or a replacement copy, if a defect in
the electronic work is discovered and reported to you within 90
days of receipt of the work.

• You comply with all other terms of this agreement for free
distribution of Project Gutenberg™ works.

1.E.9. If you wish to charge a fee or distribute a Project


Gutenberg™ electronic work or group of works on different
terms than are set forth in this agreement, you must obtain
permission in writing from the Project Gutenberg Literary
Archive Foundation, the manager of the Project Gutenberg™
trademark. Contact the Foundation as set forth in Section 3
below.

1.F.

1.F.1. Project Gutenberg volunteers and employees expend


considerable effort to identify, do copyright research on,
transcribe and proofread works not protected by U.S. copyright
law in creating the Project Gutenberg™ collection. Despite
these efforts, Project Gutenberg™ electronic works, and the
medium on which they may be stored, may contain “Defects,”
such as, but not limited to, incomplete, inaccurate or corrupt
data, transcription errors, a copyright or other intellectual
property infringement, a defective or damaged disk or other
medium, a computer virus, or computer codes that damage or
cannot be read by your equipment.

1.F.2. LIMITED WARRANTY, DISCLAIMER OF DAMAGES -


Except for the “Right of Replacement or Refund” described in
paragraph 1.F.3, the Project Gutenberg Literary Archive
Foundation, the owner of the Project Gutenberg™ trademark,
and any other party distributing a Project Gutenberg™ electronic
work under this agreement, disclaim all liability to you for
damages, costs and expenses, including legal fees. YOU
AGREE THAT YOU HAVE NO REMEDIES FOR NEGLIGENCE,
STRICT LIABILITY, BREACH OF WARRANTY OR BREACH
OF CONTRACT EXCEPT THOSE PROVIDED IN PARAGRAPH
1.F.3. YOU AGREE THAT THE FOUNDATION, THE
TRADEMARK OWNER, AND ANY DISTRIBUTOR UNDER
THIS AGREEMENT WILL NOT BE LIABLE TO YOU FOR
ACTUAL, DIRECT, INDIRECT, CONSEQUENTIAL, PUNITIVE
OR INCIDENTAL DAMAGES EVEN IF YOU GIVE NOTICE OF
THE POSSIBILITY OF SUCH DAMAGE.

1.F.3. LIMITED RIGHT OF REPLACEMENT OR REFUND - If


you discover a defect in this electronic work within 90 days of
receiving it, you can receive a refund of the money (if any) you
paid for it by sending a written explanation to the person you
received the work from. If you received the work on a physical
medium, you must return the medium with your written
explanation. The person or entity that provided you with the
defective work may elect to provide a replacement copy in lieu
of a refund. If you received the work electronically, the person or
entity providing it to you may choose to give you a second
opportunity to receive the work electronically in lieu of a refund.
If the second copy is also defective, you may demand a refund
in writing without further opportunities to fix the problem.

1.F.4. Except for the limited right of replacement or refund set


forth in paragraph 1.F.3, this work is provided to you ‘AS-IS’,
WITH NO OTHER WARRANTIES OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO
WARRANTIES OF MERCHANTABILITY OR FITNESS FOR
ANY PURPOSE.

1.F.5. Some states do not allow disclaimers of certain implied


warranties or the exclusion or limitation of certain types of
damages. If any disclaimer or limitation set forth in this
agreement violates the law of the state applicable to this
agreement, the agreement shall be interpreted to make the
maximum disclaimer or limitation permitted by the applicable
state law. The invalidity or unenforceability of any provision of
this agreement shall not void the remaining provisions.

1.F.6. INDEMNITY - You agree to indemnify and hold the


Foundation, the trademark owner, any agent or employee of the
Foundation, anyone providing copies of Project Gutenberg™
electronic works in accordance with this agreement, and any
volunteers associated with the production, promotion and
distribution of Project Gutenberg™ electronic works, harmless
from all liability, costs and expenses, including legal fees, that
arise directly or indirectly from any of the following which you do
or cause to occur: (a) distribution of this or any Project
Gutenberg™ work, (b) alteration, modification, or additions or
deletions to any Project Gutenberg™ work, and (c) any Defect
you cause.

Section 2. Information about the Mission of


Project Gutenberg™
Project Gutenberg™ is synonymous with the free distribution of
electronic works in formats readable by the widest variety of
computers including obsolete, old, middle-aged and new
computers. It exists because of the efforts of hundreds of
volunteers and donations from people in all walks of life.

Volunteers and financial support to provide volunteers with the


assistance they need are critical to reaching Project
Gutenberg™’s goals and ensuring that the Project Gutenberg™
collection will remain freely available for generations to come. In
2001, the Project Gutenberg Literary Archive Foundation was
created to provide a secure and permanent future for Project
Gutenberg™ and future generations. To learn more about the
Project Gutenberg Literary Archive Foundation and how your
efforts and donations can help, see Sections 3 and 4 and the
Foundation information page at www.gutenberg.org.

Section 3. Information about the Project


Gutenberg Literary Archive Foundation
The Project Gutenberg Literary Archive Foundation is a non-
profit 501(c)(3) educational corporation organized under the
laws of the state of Mississippi and granted tax exempt status by
the Internal Revenue Service. The Foundation’s EIN or federal
tax identification number is 64-6221541. Contributions to the
Project Gutenberg Literary Archive Foundation are tax
deductible to the full extent permitted by U.S. federal laws and
your state’s laws.

The Foundation’s business office is located at 809 North 1500


West, Salt Lake City, UT 84116, (801) 596-1887. Email contact
links and up to date contact information can be found at the
Foundation’s website and official page at
www.gutenberg.org/contact

Section 4. Information about Donations to


the Project Gutenberg Literary Archive
Foundation
Project Gutenberg™ depends upon and cannot survive without
widespread public support and donations to carry out its mission
of increasing the number of public domain and licensed works
that can be freely distributed in machine-readable form
accessible by the widest array of equipment including outdated
equipment. Many small donations ($1 to $5,000) are particularly
important to maintaining tax exempt status with the IRS.

The Foundation is committed to complying with the laws


regulating charities and charitable donations in all 50 states of
the United States. Compliance requirements are not uniform
and it takes a considerable effort, much paperwork and many
fees to meet and keep up with these requirements. We do not
solicit donations in locations where we have not received written
confirmation of compliance. To SEND DONATIONS or
determine the status of compliance for any particular state visit
www.gutenberg.org/donate.

While we cannot and do not solicit contributions from states


where we have not met the solicitation requirements, we know
of no prohibition against accepting unsolicited donations from
donors in such states who approach us with offers to donate.

International donations are gratefully accepted, but we cannot


make any statements concerning tax treatment of donations
received from outside the United States. U.S. laws alone swamp
our small staff.

Please check the Project Gutenberg web pages for current


donation methods and addresses. Donations are accepted in a
number of other ways including checks, online payments and
credit card donations. To donate, please visit:
www.gutenberg.org/donate.

Section 5. General Information About Project


Gutenberg™ electronic works
Professor Michael S. Hart was the originator of the Project
Gutenberg™ concept of a library of electronic works that could
be freely shared with anyone. For forty years, he produced and
distributed Project Gutenberg™ eBooks with only a loose
network of volunteer support.

Project Gutenberg™ eBooks are often created from several


printed editions, all of which are confirmed as not protected by
copyright in the U.S. unless a copyright notice is included. Thus,
we do not necessarily keep eBooks in compliance with any
particular paper edition.

Most people start at our website which has the main PG search
facility: www.gutenberg.org.

This website includes information about Project Gutenberg™,


including how to make donations to the Project Gutenberg
Literary Archive Foundation, how to help produce our new
eBooks, and how to subscribe to our email newsletter to hear
about new eBooks.
Welcome to our website – the ideal destination for book lovers and
knowledge seekers. With a mission to inspire endlessly, we offer a
vast collection of books, ranging from classic literary works to
specialized publications, self-development books, and children's
literature. Each book is a new journey of discovery, expanding
knowledge and enriching the soul of the reade

Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.

Let us accompany you on the journey of exploring knowledge and


personal growth!

ebookfinal.com

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy