0% found this document useful (0 votes)
17 views57 pages

Beginning C 2008 From Novice To Professional Second Edition Christian Gross

Uploaded by

karvejinxiu
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)
17 views57 pages

Beginning C 2008 From Novice To Professional Second Edition Christian Gross

Uploaded by

karvejinxiu
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/ 57

Visit ebookfinal.

com to download the full version and


explore more ebooks or textbooks

Beginning C 2008 From Novice to Professional


Second Edition Christian Gross

_____ Click the link below to download _____


https://ebookfinal.com/download/beginning-c-2008-from-
novice-to-professional-second-edition-christian-gross/

Explore and download more ebooks or textbook at ebookfinal.com


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

Beginning VB 2008 From Novice to Professional 1st Edition


Christian Gross (Auth.)

https://ebookfinal.com/download/beginning-vb-2008-from-novice-to-
professional-1st-edition-christian-gross-auth/

Beginning C From Novice to Professional Beginning from


Novice to Professional 4th ed. Edition Ivor Horton

https://ebookfinal.com/download/beginning-c-from-novice-to-
professional-beginning-from-novice-to-professional-4th-ed-edition-
ivor-horton/

Beginning Visual Web Programming in C From Novice to


Professional 1st Edition Daniel Cazzulino

https://ebookfinal.com/download/beginning-visual-web-programming-in-c-
from-novice-to-professional-1st-edition-daniel-cazzulino/

Beginning Fedora From Novice to Professional 2nd Edition


Keir Thomas

https://ebookfinal.com/download/beginning-fedora-from-novice-to-
professional-2nd-edition-keir-thomas/
Beginning Hibernate From Novice to Professional 1st
Edition Dave Minter

https://ebookfinal.com/download/beginning-hibernate-from-novice-to-
professional-1st-edition-dave-minter/

Beginning Python From Novice to Professional 4th Edition


Magnus Lie Hetland

https://ebookfinal.com/download/beginning-python-from-novice-to-
professional-4th-edition-magnus-lie-hetland/

Beginning PHP and Oracle From Novice to Professional 1st


Edition Bob Bryla

https://ebookfinal.com/download/beginning-php-and-oracle-from-novice-
to-professional-1st-edition-bob-bryla/

Beginning PHP and MySQL From Novice to Professional 4th


Edition W. Jason Gilmore

https://ebookfinal.com/download/beginning-php-and-mysql-from-novice-
to-professional-4th-edition-w-jason-gilmore/

Beginning Ubuntu LTS Server Administration From Novice to


Professional Second Edition Expert s Voice in Linux Sander
Van Vugt
https://ebookfinal.com/download/beginning-ubuntu-lts-server-
administration-from-novice-to-professional-second-edition-expert-s-
voice-in-linux-sander-van-vugt/
Beginning C 2008 From Novice to Professional Second
Edition Christian Gross Digital Instant Download
Author(s): Christian Gross
ISBN(s): 9781430210344, 1430210346
Edition: 2
File Details: PDF, 7.36 MB
Year: 2008
Language: english
Beginning C# 2008
From Novice to Professional
SECOND EDITION

■■■

Christian Gross
Beginning C# 2008: From Novice to Professional, Second Edition
Copyright © 2008 by Christian Gross
All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means,
electronic or mechanical, including photocopying, recording, or by any information storage or retrieval
system, without the prior written permission of the copyright owner and the publisher.
ISBN-13 (pbk): 978-1-4302-1033-7
ISBN-13 (electronic): 978-1-4302-1034-4
Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1
Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrence
of a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark
owner, with no intention of infringement of the trademark.
Lead Editor: Ewan Buckingham
Technical Reviewer: Christian Kenyeres
Editorial Board: Editorial Board: Clay Andres, Steve Anglin, Ewan Buckingham, Tony Campbell, Gary
Cornell, Jonathan Gennick, Matthew Moodie, Joseph Ottinger, Jeffrey Pepper, Frank Pohlmann, Ben
Renow-Clarke, Dominic Shakeshaft, Matt Wade, Tom Welsh
Project Manager: Sofia Marchant
Developmental Editor: Ron Pronk
Copy Editor: Damon Larson
Associate Production Director: Kari Brooks-Copony
Production Editor: Laura Esterman
Compositors: Susan Glinert and Octal Publishing, Inc.
Proofreader: Lisa Hamilton
Indexer: Broccoli Information Management
Artist: April Milne
Cover Designer: Kurt Krames
Manufacturing Director: Tom Debolski
Distributed to the book trade worldwide by Springer-Verlag New York, Inc., 233 Spring Street, 6th Floor,
New York, NY 10013. Phone 1-800-SPRINGER, fax 201-348-4505, e-mail [email protected], or
visit http://www.springeronline.com.
For information on translations, please contact Apress directly at 2855 Telegraph Avenue, Suite 600,
Berkeley, CA 94705. Phone 510-549-5930, fax 510-549-5939, e-mail [email protected], or visit
http://www.apress.com.
Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use.
eBook versions and licenses are also available for most titles. For more information, reference our Special
Bulk Sales–eBook Licensing web page at http://www.apress.com/info/bulksales.
The information in this book is distributed on an “as is” basis, without warranty. Although every precaution
has been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability to
any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly
by the information contained in this work.
The source code for this book is available to readers at http://www.apress.com.
Some food for thought when writing software:

“A common mistake that people make when trying to design something


completely foolproof is to underestimate the ingenuity of complete fools.”

“The major difference between a thing that might go wrong and a thing that
cannot possibly go wrong is that when a thing that cannot possibly go wrong goes
wrong it usually turns out to be impossible to get at or repair.”

—Douglas Adams, Mostly Harmless


Contents at a Glance

About the Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii


About the Technical Reviewer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi

■CHAPTER 1 Ready, Steady, Go! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1


■CHAPTER 2 .NET Number and Value Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
■CHAPTER 3 String Manipulations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
■CHAPTER 4 Data Structures, Decisions, and Loops . . . . . . . . . . . . . . . . . . . 75
■CHAPTER 5 C# Exception Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
■CHAPTER 6 Object-Oriented Programming Basics . . . . . . . . . . . . . . . . . . . 131
■CHAPTER 7 Components and Object Hierarchies . . . . . . . . . . . . . . . . . . . . . 159
■CHAPTER 8 Component-Oriented Architecture . . . . . . . . . . . . . . . . . . . . . . . 193
■CHAPTER 9 Lists, Delegates, and Lambda Expressions . . . . . . . . . . . . . 225
■CHAPTER 10 All About Persistence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251
■CHAPTER 11 Exploring .NET Generics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289
■CHAPTER 12 Application Configuration and Dynamic Loading . . . . . . . . 323
■CHAPTER 13 All About Multithreading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 353
■CHAPTER 14 Using Relational Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 383
■CHAPTER 15 Larning About LINQ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 409
■CHAPTER 16 Writing Functional Code in C# . . . . . . . . . . . . . . . . . . . . . . . . . . . 433
■CHAPTER 17 C# Odds and Ends . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 449

■INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 465

v
Contents

About the Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii


About the Technical Reviewer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi

■CHAPTER 1 Ready, Steady, Go! .........................................1

Downloading and Installing the Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1


Downloading Visual C# Express . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
Choosing the Application Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Creating Projects and Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Creating the Windows Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Viewing the Source Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Renaming the Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Saving the Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Running the Windows Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Making the Windows Application Say Hello . . . . . . . . . . . . . . . . . . . . . 9
Adding Comments to the Application . . . . . . . . . . . . . . . . . . . . . . . . . 11
Navigating the User Controls of the Solution . . . . . . . . . . . . . . . . . . . . . . . 13
Creating the Console Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Adding a Console Application Project to the Solution . . . . . . . . . . . 16
Making the Console Application Say Hello . . . . . . . . . . . . . . . . . . . . . 16
Setting the Startup Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
Running the Console Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
Creating the Class Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
Adding a Class Library Project to the Solution . . . . . . . . . . . . . . . . . 17
Moving Functionality . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
Defining References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
Calling Class Library Functionality . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
Using Variables and Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
Understanding How the .NET Framework Works . . . . . . . . . . . . . . . . . . . . 23
The Important Stuff to Remember . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
Some Things for You to Do . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26

vii
viii ■C O N T E N T S

■CHAPTER 2 .NET Number and Value Types . . . . . . . . . . . . . . . . . . . . . . . . . . . 27

Focusing and Organizing Your Development . . . . . . . . . . . . . . . . . . . . . . . 27


Organizing the Calculator. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
Focusing the Calculator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
Implementing the Class Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
Writing the Add() Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
Writing Code to Test the Add() Method . . . . . . . . . . . . . . . . . . . . . . . 36
Understanding Problems with Numeric Values and Types . . . . . . . 41
Understanding Numeric and Value Data Types . . . . . . . . . . . . . . . . . . . . . 43
Understanding Value and Reference Types . . . . . . . . . . . . . . . . . . . . 43
Understanding the CLR Numeric Types . . . . . . . . . . . . . . . . . . . . . . . 44
Finishing the Calculator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
The Important Stuff to Remember . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
Some Things for You to Do . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49

■CHAPTER 3 String Manipulations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51

Organizing the Translation Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51


Building the Translator Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
Creating the Translator Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
Translating Hello . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
Creating the Test Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
Answering the Question of Responsibility . . . . . . . . . . . . . . . . . . . . . 55
Investigating the String Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
Solving the Extra Whitespace Problem . . . . . . . . . . . . . . . . . . . . . . . . 61
Quoting Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
Character Mapping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
Dealing with Languages and Cultures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
Setting Culture and Language in Windows . . . . . . . . . . . . . . . . . . . . 67
Parsing and Processing Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
Working with Cultures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
The Important Stuff to Remember . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
Some Things for You to Do . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74

■CHAPTER 4 Data Structures, Decisions, and Loops . . . . . . . . . . . . . . . . . . . 75


Understanding the Depth-First Search Algorithm . . . . . . . . . . . . . . . . . . . 75
Implementing User-Defined Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
Declaring Structs and Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
Value Type Constraints. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
■C O N T E N T S ix

Organizing the Search Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85


Writing the Depth-First Search Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
Defining and Implementing the Data Structure . . . . . . . . . . . . . . . . . 87
Defining the Algorithm Test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
Implementing the Depth-First Search Algorithm . . . . . . . . . . . . . . . 101
Running the Depth-First Search Algorithm . . . . . . . . . . . . . . . . . . . 109
The Important Stuff to Remember . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
Some Things for You to Do . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111

■CHAPTER 5 C# Exception Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113

Understanding Errors, Exceptions, and Exception Handling . . . . . . . . . . 113


Running the Debugger . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114
Handling Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
Catching Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
Implementing Exception Handlers . . . . . . . . . . . . . . . . . . . . . . . . . . 118
Safeguarding Against Stack Unwinding . . . . . . . . . . . . . . . . . . . . . . 121
Filtering Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122
Writing Exception-Safe Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
Writing Defensive Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
Using Default State . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
Processing Errors That Are Warnings . . . . . . . . . . . . . . . . . . . . . . . . 128
The Important Stuff to Remember . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
Some Things for You to Do . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130

■CHAPTER 6 Object-Oriented Programming Basics . . . . . . . . . . . . . . . . . . . 131


Understanding Currency Spreads . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132
Organizing the Currency Exchange Application . . . . . . . . . . . . . . . . . . . . 133
Writing Tests for the Currency Exchange Application . . . . . . . . . . . . . . . 133
Getting Started with Structural Code . . . . . . . . . . . . . . . . . . . . . . . . 133
Understanding Base Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134
Understanding Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135
Using C# Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
Understanding Inheritance and Scope Modifiers . . . . . . . . . . . . . . 140
Handling Verification. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143
Finishing the Base Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146
Writing the Active Trader and Hotel Trader Currency Converters. . . . . . 147
Implementing ActiveCurrencyTrader . . . . . . . . . . . . . . . . . . . . . . . . 147
Implementing HotelCurrencyTrader . . . . . . . . . . . . . . . . . . . . . . . . . 150
x ■C O N T E N T S

Learning More About Preprocessor Directives, Properties, and


Abstract Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152
More Preprocessor Directive Details . . . . . . . . . . . . . . . . . . . . . . . . 152
More Property Scope Details . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154
More abstract Keyword Details . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154
The Important Stuff to Remember . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156
Some Things for You to Do . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157

■CHAPTER 7 Components and Object Hierarchies . . . . . . . . . . . . . . . . . . . . . 159


Understanding Some Basic Tax Concepts . . . . . . . . . . . . . . . . . . . . . . . . 159
Organizing the Tax Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160
Programming Using Ideas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161
Representing Ideas Using C# Interfaces . . . . . . . . . . . . . . . . . . . . . 162
Understanding How Inheritance and Components Work . . . . . . . . 164
Implementing a Tax Calculation Engine . . . . . . . . . . . . . . . . . . . . . . . . . . 170
Defining the Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170
Implementing a Base Class Tax Calculation Engine . . . . . . . . . . . . 171
Using Default Implementations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176
Implementing a Base Tax Account . . . . . . . . . . . . . . . . . . . . . . . . . . 177
Using the Base Functionality of the Tax Calculation Engine . . . . . . . . . . 179
Implementing a Tax Calculation Engine and Tax Account . . . . . . . 179
Using the Tax Calculation Engine . . . . . . . . . . . . . . . . . . . . . . . . . . . 184
More About Inheritance and Type Casting . . . . . . . . . . . . . . . . . . . . . . . . 184
More Inheritance Details . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184
More Type-Casting Details . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190
The Important Stuff to Remember . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191
Some Things for You to Do . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192

■CHAPTER 8 Component-Oriented Architecture . . . . . . . . . . . . . . . . . . . . . . . 193


Understanding Kernels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193
Organizing the Lighting Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194
Building the Kernel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195
Defining the Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196
Implementing the Kernel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200
Defining the Kernel As an Interface Instead of a Class . . . . . . . . . . 216
Building a Complete Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218
Defining Some Rooms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218
Instantiating PublicRoom and PrivateRoom . . . . . . . . . . . . . . . . . . . 219
■C O N T E N T S xi

Learning More About Private Classes and Object Initialization . . . . . . . . 221


Private Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221
Object Initialization with Nested Data Types . . . . . . . . . . . . . . . . . . 221
The Important Stuff to Remember . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223
Some Things for You to Do . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223

■CHAPTER 9 Lists, Delegates, and Lambda Expressions . . . . . . . . . . . . . 225

Managing Collections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225


Managing a Collection Before C# 2.0 . . . . . . . . . . . . . . . . . . . . . . . . 226
Managing a Collection After C# 2.0 . . . . . . . . . . . . . . . . . . . . . . . . . 230
The Case of the Code That Feels Wrong . . . . . . . . . . . . . . . . . . . . . . . . . . 231
Using Delegates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235
Using Anonymous Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239
Multicasting with Delegates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 240
Using Lambda Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241
Understanding Lambda Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243
Creating the Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244
Implementing the Algorithm Using Lambda Expressions . . . . . . . . 245
More About Collection Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246
Using a Plain-Vanilla List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246
Using a Key/Value Pair List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247
Using a Stack . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248
Using a Queue . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248
The Important Stuff to Remember . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249
Some Things for You to Do . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249

■CHAPTER 10 All About Persistence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251

Organizing the Lottery-Prediction System . . . . . . . . . . . . . . . . . . . . . . . . 251


Piping Data Using a Console . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 252
Reading Data from the Console . . . . . . . . . . . . . . . . . . . . . . . . . . . . 252
Building a Shell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254
Implementing the TextProcessor Application . . . . . . . . . . . . . . . . . 263
Piping Binary Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 272
Defining the Interfaces and Implementing the Shell . . . . . . . . . . . . 273
Defining the Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 276
Converting a Text Stream to a Binary Stream . . . . . . . . . . . . . . . . . 278
Converting a Binary Stream to a Text Stream . . . . . . . . . . . . . . . . . 279
xii ■C O N T E N T S

Tweaking Serialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280


Performing Custom Serialization. . . . . . . . . . . . . . . . . . . . . . . . . . . . 281
Declaring a Data Member As Nonserializable . . . . . . . . . . . . . . . . . 282
Separating Data Objects from Action Objects . . . . . . . . . . . . . . . . . 282
Completing Custom Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283
Implementing GetHashCode() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283
Implementing Equals() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 286
The Important Stuff to Remember . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287
Some Things for You to Do . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 288

■CHAPTER 11 Exploring .NET Generics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289


Why Use .NET Generics? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289
The Theory of a Server-Side Spreadsheet . . . . . . . . . . . . . . . . . . . . . . . . 292
Architecting a Server-Side Spreadsheet . . . . . . . . . . . . . . . . . . . . . . . . . 295
Designing the Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295
Defining the Server Spreadsheet Interfaces . . . . . . . . . . . . . . . . . . 296
Implementing the Server Spreadsheet . . . . . . . . . . . . . . . . . . . . . . . . . . . 305
Using Lambda Expressions in the Spreadsheet . . . . . . . . . . . . . . . 310
Assigning State Without Knowing the Type . . . . . . . . . . . . . . . . . . . 314
Overriding the ToString() Functionality . . . . . . . . . . . . . . . . . . . . . . . 316
Iterating Data Using Enumerators . . . . . . . . . . . . . . . . . . . . . . . . . . . 317
Using the Spreadsheet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 318
Calculating an Average . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 318
Understanding Why the Calculations Work . . . . . . . . . . . . . . . . . . . 320
The Important Stuff to Remember . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 321
Some Things for You to Do . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 322

■CHAPTER 12 Application Configuration and Dynamic Loading . . . . . . . . 323


Convention over Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 323
Decoupling Using a Configuration Architecture . . . . . . . . . . . . . . . . 325
Decoupling Using a Convention Architecture. . . . . . . . . . . . . . . . . . 325
Setting Up the Dynamic Loading Projects . . . . . . . . . . . . . . . . . . . . . . . . . 326
Signing an Assembly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 327
Setting the Output Path . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 328
Defining and Processing a Configuration File . . . . . . . . . . . . . . . . . . . . . . 329
Creating an XML-Based Configuration File . . . . . . . . . . . . . . . . . . . 330
Adding the Dynamic Loading Configuration Items . . . . . . . . . . . . . 332
Reading a Configuration File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 332
■C O N T E N T S xiii

Dynamically Loading an Assembly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 333


Dynamically Instantiating a Type . . . . . . . . . . . . . . . . . . . . . . . . . . . 333
Enhancing the Configuration File . . . . . . . . . . . . . . . . . . . . . . . . . . . 337
Loading a Strongly Named Assembly . . . . . . . . . . . . . . . . . . . . . . . . . . . . 342
Relocating a Strongly Named Assembly to the GAC . . . . . . . . . . . . 343
Using Version Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 345
Implementing a Convention-Based Architecture . . . . . . . . . . . . . . . . . . . 348
Dynamically Loading Base Class or Interface Types . . . . . . . . . . . . . . . . 350
The Important Stuff to Remember . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 350
Some Things for You to Do . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 351

■CHAPTER 13 All About Multithreading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 353


Understanding Multitasking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 353
Preemptive Multitasking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 354
Understanding Time Slicing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 354
Using Threads . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 357
Creating a New Thread . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 357
Waiting for the Thread to End . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 358
Creating a Thread with State . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 359
Synchronizing Between Threads. . . . . . . . . . . . . . . . . . . . . . . . . . . . 361
How to Prevent Your Code from Deadlocking . . . . . . . . . . . . . . . . . 366
Implementing a Reader/Writer Threaded Architecture . . . . . . . . . . . . . . 370
Implementing a Producer/Consumer Architecture . . . . . . . . . . . . . . . . . . 374
Using a Hidden Producer/Consumer Implementation . . . . . . . . . . . 374
Implementing a Generic Producer/Consumer Architecture . . . . . . 376
Using an Asynchronous Approach . . . . . . . . . . . . . . . . . . . . . . . . . . 378
The Important Stuff to Remember . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 380
Some Things for You to Do . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 381

■CHAPTER 14 Using Relational Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 383

Understanding Relational Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 383


Relational Database Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 384
Database Relations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 385
Accessing Relational Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 388
Designing a Database Using Visual C# Express . . . . . . . . . . . . . . . . . . . . 389
Configuring the Data Source . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 390
Adding the Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 393
xiv ■C O N T E N T S

Accessing the Database Using ADO.NET . . . . . . . . . . . . . . . . . . . . . . . . . 396


Connecting to a Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 397
Adding Table Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 397
Selecting Data from a Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 399
Deleting Data from the Database . . . . . . . . . . . . . . . . . . . . . . . . . . . 400
Closing a Database Connection . . . . . . . . . . . . . . . . . . . . . . . . . . . . 401
Recapping ADO.NET Usage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 401
Using the Dataset Designer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 401
Building Relations Between Tables. . . . . . . . . . . . . . . . . . . . . . . . . . 402
Using the Generated Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 406
The Important Stuff to Remember . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 407
Some Things for You to Do . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 408

■CHAPTER 15 Learning About LINQ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 409

Finding the Frequency of Winning Numbers . . . . . . . . . . . . . . . . . . . . . . . 409


Extending the Lottery-Prediction System . . . . . . . . . . . . . . . . . . . . . 410
Implementing a Frequency Solution . . . . . . . . . . . . . . . . . . . . . . . . . 413
Learning More LINQ Tricks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 419
Selecting and Altering Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 423
Selecting with Anonymous Types . . . . . . . . . . . . . . . . . . . . . . . . . . . 424
Processing Multiple Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 425
Sorting the Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 426
Splitting Results into Groups . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 427
Performing Set Operations. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 428
Using LINQ in Other Contexts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 430
The Important Stuff to Remember . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 432
Some Things for You to Do . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 432

■CHAPTER 16 Writing Functional Code in C# . . . . . . . . . . . . . . . . . . . . . . . . . . . 433

Why Functional Programming? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 433


The Essence of Functional Programming . . . . . . . . . . . . . . . . . . . . . . . . . 435
Higher-Order Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 436
Pure Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 439
Function Evaluation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 444
Recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 446
The Important Stuff to Remember . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 447
Some Things for You to Do . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 448
■C O N T E N T S xv

■CHAPTER 17 C# Odds and Ends . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 449


Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 449
Using Arithmetic Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 449
Overloading the Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 454
The goto Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 456
Understanding .NET Generics Constraints . . . . . . . . . . . . . . . . . . . . . . . . 457
Using the type Constraint . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 457
Using the new Constraint . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 459
Using the class Constraint . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 460
Nullable Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 460
Partial Classes and Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 462
The Important Stuff to Remember . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 464
Some Things for You to Do . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 464

■INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 465
About the Author

Many people say that by looking at a person’s dog, you can tell what the
person is like. Well, the picture is of my dog Louys, an English bulldog.
And yes, my English bulldog and I have many common characteristics.
But what about the biography of the author, CHRISTIAN GROSS? It’s
pretty simple: I’m a guy who has spent oodles of time strapped to a chair
debugging and taking apart code. In fact, I really enjoy this business we
call software development. I have loved it ever since I learned how to peek
and poke my first bytes. I have written various books, including Ajax and REST Recipes:
A Problem-Solution Approach, Foundations of Object-Oriented Programming Using .NET
2.0 Patterns, and A Programmer’s Introduction to Windows DNA, all available from Apress.
These days, I enjoy coding and experimenting with .NET, as it is a fascinating environment.
.NET makes me feel like a kid opening a present on Christmas morning. You had an idea what
the gift was, but you were not completely sure. And with .NET, there is no relative giving you
socks or a sweater. It’s excitement all the way!

xvii
About the Technical Reviewer

■CHRISTIAN KENYERES, principal architect for Collaborative Consulting, is


a visionary technology professional with more than 15 years of extensive
information technology experience. He has served numerous high-profile
clients as an enterprise architect and boasts a broad range of technical
and business knowledge.
Prior to joining Collaborative, Christian performed consulting for
various companies such as Compaq, EMC, Fidelity Investments, Liberty
Mutual Insurance, and John Hancock. He holds B.S. and M.S. degrees in Computer Science
from the University of Massachusetts and Boston University, respectively.

xix
Introduction

T he first computer programming book I read was entitled Programming Windows 3.0 by
Charles Petzold. This was around the time when Microsoft Windows 3.0 (circa 1992) once and
for all showed the industry that Microsoft was a company with a future. Writing code for Windows
back then was complicated by many things: lack of documentation, 16-bit architecture, and the
necessity of buying a compiler separate from the software development kit (SDK). Charles’s
book tied everything together and solved the problem of how to write a program for Windows.
Now the problems are quite the opposite: we have too much documentation, we have
64-bit architectures, and everything including the kitchen sink is thrown into a development
environment. Now we need to figure out what we actually need. We have too many options—too
many ways to solve the same problem. What I am trying to do with this book is the same thing
that Charles did for me when I first started out, and that was to help me figure out what I needed
to write code.
This book is about explaining the C# programming language in the context of solving prob-
lems. C# has become a sophisticated programming language that can achieve many goals, but
you are left wondering what techniques to use when. This book is here to answer your questions.
This book is not a reference to all of the features of the C# programming language. I don’t
explain the esoteric C# features. I stick to the C# programming features that you will use day
in and day out. That does not mean that you will be missing certain C# programming language
constructs, because I have covered all of the major features.
To get the full benefit of this book, I suggest that you do the exercises at the end of the chapters.
The answers are available on the Apress web site (http://www.apress.com), and you can cheat
and not do the exercises, but I advise against that.
If you are a beginning programmer who has no clue about C#, and you read this book
and do the exercises, I am almost entirely sure that you will be a solid and knowledgeable C#
programmer by the end of the book. If that sounds like a big promise, well, yes it is. The chapter
text is intended to get you acquainted with the C# programming language and how to apply its
features. The exercises are intended to make sure you actually understand the C# programming
language and its features.
The chapter exercises are challenging. They cannot be solved within a few minutes. In fact,
when I did all of the exercises, it took me five working-hour days to do all of them!
If you have any questions, such as, “So what was he trying to get at with that exercise?” I am
available on Skype with the user ID christianhgross. Please don’t just ring me. First chat using
text, and if necessary, we can have a voice conversation. Also, you can send e-mail to me at
[email protected].
Thanks and good luck.

xxi
CHAPTER 1
■■■

Ready, Steady, Go!

T his book is about the C# programming language and helping you become a proficient C#
programmer—even if you’ve never programmed before or you’ve only worked with procedural
languages like Visual Basic. (C# is called an object-oriented language, which differs in approach
from procedural languages like Visual Basic, Pascal, COBOL, and a lot of others that have been
just about left for dead.) Object-oriented languages are not only the wave of the future, they’re
the wave of today. You can’t program for the Web if you don’t know how to use an object-
oriented language like Java, C++, or C#. And if you want to use the .NET platform to program
web sites and web data exchanges (an extremely popular approach), then C# is the language
you want to learn.
In this chapter, you’ll get started by acquiring the tools you need to develop C# applica-
tions and taking tools for a test spin. Along the way, you’ll create a few C# applications.

Downloading and Installing the Tools


If you’re just getting started with C# 3.0, you’re probably eager to write some code that actually
does something. The great part of .NET is that you can start writing code immediately after you
have installed either the .NET software development kit (.NET SDK) or a Visual Studio inte-
grated development environment (IDE). Downloading and installing the right environment is
critical to taking your first step toward a productive and valuable coding experience.

■Note This book covers the C# 3.0 programming language as it’s used to write applications for the .NET
Framework. With C# 3.0, you’ll use the .NET 3.0 and 3.5 Frameworks. .NET 3.0 provides you with all of the
programming and coding essentials, and .NET 3.5 gives you a lot of extras and many additional programming
options.

For the examples in this book, we’ll use Visual C# 2008 Express Edition. Why? Well, it’s
freely available and has everything you need to get started with C# 3.0. The other Express
Edition IDEs available from Microsoft are tailored to different languages (Visual Basic and C++)
or, in the case of Visual Web Developer Express, support specific functionality that is too
restrictive for our purposes.

1
2 CHAPTER 1 ■ READY, STEADY, GO!

Microsoft also offers full versions of the Visual Studio IDE, such as the Standard, Profes-
sional, and Team editions. Each of these editions has different feature sets and different price
tags. See the Microsoft Visual Studio Web site (http://msdn2.microsoft.com/en-us/vstudio/
default.aspx) for more information. If you already have Visual Studio 2008 Professional installed,
you can use that for the examples in this book. The Visual Studio 2008 edition can do every-
thing that Visual C# Express can do, and, in fact, has more options.

■Note I personally use Visual Studio Standard or Professional in combination with other tools such as
X-develop and JustCode! from Omnicore (http://www.omnicore.com), TestDriven.NET (http://
www.testdriven.net/), and NUnit (http://www.nunit.org). The Visual Studio products are very good,
but others are available. Being a good developer means knowing which tools are available and determining
which tools will work best for you.

Installing and downloading Visual C# Express from the Microsoft web site involves the
transfer of large files. If you don’t have a broadband connection, you might prefer to install the
IDE from a CD, which you can order from Microsoft’s online site.

Downloading Visual C# Express


Here’s the procedure for downloading Visual C# Express from the Microsoft web site. By the
time you read this book, the procedure might have changed, but it should be similar enough
that you’ll be able to find and download the IDE package.

1. Go to http://msdn.microsoft.com/vstudio/express/.

2. Select the Visual Studio 2008 Express Editions link.

3. Select Windows Development (because, for the scope of this book, that is what you’ll
be doing).

4. Click the Visual Studio Express Download link.

5. You’ll see a list of Visual Studio Express editions, as shown in Figure 1-1. Click Visual C#
2008 Express Edition.

6. A dialog box appears, asking where you want to store the downloaded file. The file that
you are downloading is a small bootstrap file, which you’ll use to begin the actual instal-
lation of the Visual C# Express IDE. Choose to save the file on the desktop.

These steps can be carried out very quickly—probably within a few minutes. If you follow
this process, please don’t mistake the procedure for downloading the complete Visual C# Express
application, because that’s not what happened. The installation procedure itself (which you’ll
perform next) will download the vast majority of the IDE. At this point, you’re just downloading
the initial setup file.
CHAPTER 1 ■ READY, STEADY, GO! 3

Figure 1-1. Selecting Visual C# 2008 Express Edition

After you’ve downloaded the setup file, you can start the Visual C# Express installation.
During this process, all the pieces of the IDE—about 300MB—are downloaded and installed.
Follow these steps:

1. On your desktop, double-click the vcssetup.exe file. Wait while the setup program
loads all the required components.

2. Click Next on the initial setup screen.

3. A series of dialog boxes will appear. Select the defaults, and click Next to continue
through the setup program. In the final dialog box, click Install.

4. After all the elements have been downloaded and installed, you may need to restart
your computer.

After Visual C# Express has been installed, you can start it by selecting it from the Start menu.
Discovering Diverse Content Through
Random Scribd Documents
consists in knowing the explanation of words, and that greater
perfection is attained by employing more words and longer speech.
It is, however, right that we should examine the Scriptural texts by
the intellect, after having acquired a knowledge of demonstrative
science, and of the true hidden meaning of prophecies. But if one
has obtained some knowledge in this matter he must not preach on
it, as I stated in my Commentary on the Mishnah (Ḥagigah, ii. 7),
and our Sages said distinctly: From the beginning of the book to this
place—after the account of the sixth day of the Creation—it is “the
glory of God to conceal a thing” (Prov. xxv. 2).

We have thus clearly stated our opinion. It is, however, part of the
Divine plan that every one who has obtained some perfection
transmit it to some other persons, as will be shown in the chapter on
Prophecy. It is, therefore, impossible for a scholar to possess
knowledge of these problems, whether it be through his own
researches or through his master’s teaching, without communicating
part of that knowledge to others; it cannot be done in clear words; it
must be done sparingly by way of hints. We find in the words of
some of our Sages numerous hints and notes of this kind, but mixed
up with the words of others and with other subjects. In treating of
these mysteries, as a rule, I quote as much as contains the principal
idea, and leave the rest for those who are worthy of it.

Secondly, the prophets employ homonymous terms and use words


which are not meant to be understood in their ordinary signification,
but are only used because of some other meaning which they admit,
e.g., “a rod of an almond-tree (shaked),” because of the words
which follow, “for I will hasten (shaked)” (Jer. i. 11, 12), as will be
shown in the chapter on Prophecy. According to the same principle
Ezekiel in the account of the Divine Chariot employs, as we have
stated the term ḥashmal (Ezek. i. 4); also regel egel (v. 7), neḥoshet
kalal (v. 7), and similar terms; Zechariah (vi. 1) likewise [212]adopts
this method, and says: “And the mountains were mountains of
neḥoshet (brass),” and the like.

After these two remarks I will proceed to the chapter which I have
promised.

[Contents]
CHAPTER XXX
There is a difference between first and beginning (or principle). The
latter exists in the thing of which it is the beginning, or co-exists
with it; it need not precede it; e.g., the heart is the beginning of the
living being; the element is the beginning of that of which it is the
basis. The term “first” is likewise applied to things of this kind; but is
also employed in cases where precedence in time alone is to be
expressed, and the thing which precedes is not the beginning (or the
cause) of the thing that follows. E.g., we say A. was the first
inhabitant of this house, after him came B; this does not imply that
A is the cause of B inhabiting the house. In Hebrew, teḥillah is used
in the sense of “first”; e.g., when God first (teḥillat) spake to Hosea
(Hos. i. 1), and the “beginning” is expressed by reshith, derived from
rosh, “head,” the principal part of the living being as regards
position. The Universe has not been created out of an element that
preceded it in time, since time itself formed part of the Creation. For
this reason Scripture employs the term “bereshit” (in a principle), in
which the beth is a preposition denoting “in.” The true explanation of
the first verse of Genesis is as follows: “In [creating] a principle God
created the beings above and the things below.” This explanation is
in accordance with the theory of the Creation. We find that some of
our Sages are reported to have held the opinion that time existed
before the Creation. But this report is very doubtful, because the
theory that time cannot be imagined with a beginning, has been
taught by Aristotle, as I showed you, and is objectionable. Those
who have made this assertion have been led to it by a saying of one
of our Sages in reference to the terms “one day,” “a second day.”
Taking these terms literally, the author of that saying asked, What
determined “the first day,” since there was no rotating sphere, and
no sun? and continues as follows: Scripture uses the term “one day”;
R. Jehudah, son of R. Simon, said: “Hence we learn that the
divisions of time have existed previously.” R. Abahu said, “Hence we
learn that God built worlds and again destroyed them.” This latter
exposition is still worse than the former. Consider the difficulty which
these two Rabbis found in the statement that time existed before
the creation of the sun. We shall undoubtedly soon remove this
difficulty, unless these two Rabbis intended to infer from the
Scriptural text that the divisions of time must have existed before
the Creation, and thus adopted the theory of the Eternity of the
Universe. But every religious man rejects this. The above saying is,
in my opinion, certainly of the same character as that of R. Eliezer,
“Whence were the heavens created,” etc., (chap. xxvi.). In short, in
these questions, do not take notice of the utterances of any person.
I told you that the foundation of our faith is the belief that God
created the Universe from nothing; that time did not exist previously,
but was created; for it depends on the motion of the sphere, and the
sphere has been created. [213]

You must know that the particle et in the phrase et ha-shamayim ve-
et ha-areẓ (“the heavens and the earth”) signifies “together with”;
our Sages have explained the word in the same sense in many
instances. Accordingly they assume that God created with the
heavens everything that the heavens contain, and with the earth
everything the earth includes. They further say that the
simultaneous Creation of the heavens and the earth is implied in the
words, “I call unto them, they stand up together” (Ps. xlviii.).
Consequently, all things were created together, but were separated
from each other successively. Our Sages illustrated this by the
following simile: We sow various seeds at the same time; some
spring forth after one day, some after two, and some after three
days, although all have been sown at the same time. According to
this interpretation, which is undoubtedly correct, the difficulty is
removed, which led R. Jehudah, son of R. Simon, to utter the above
saying, and consisted in the doubt as to the thing by which the first
day, the second, and the third were determined. In Bereshit Rabba,
our Sages, speaking of the light created on the first day according to
the Scriptural account, say as follows: these lights [of the luminaries
mentioned in the Creation of the fourth day] are the same that were
created on the first day, but were only fixed in their places on the
fourth day. The meaning [of the first verse] has thus been clearly
stated.

We must further consider that the term ereẓ is a homonym, and is


used in a general and a particular sense. It has a more general
signification when used of everything within the sphere of the moon,
i.e., of all the four elements; and is used in particular of one of
them, of the lowest, viz., earth. This is evident from the passage:
“And the earth was without form and void, and darkness was on the
surface of the deep. And the wind of God moved upon the face of
the waters.” The term “earth” [mentioned here, and in the first
verse] includes all the four elements, whilst further on it is said, “And
God called the dry land Earth” (Gen. i. 10).

It is also important to notice that the words, “And God called a


certain thing a certain name,” are invariably intended to distinguish
one thing from others which are called by the same common noun. I
explain, therefore, the first verse in Genesis thus: In creating the
principle God created the things above and those below. Ereẓ in this
verse denotes “the things below,” or “the four elements,” and in the
verse, “And God called the dry land Earth” (ereẓ), it signifies the
element earth. This subject is now made clear.

The four elements indicated, according to our explanation, in the


term ereẓ “earth,” in the first verse, are mentioned first after the
heavens; for there are named ereẓ (earth), ruaḥ (air), mayim
(water), and ḥoshek (fire). By ḥoshek the element fire is meant,
nothing else; comp. “And thou heardest his words out of the midst
of the fire” (Deut. iv. 36); and, “When ye heard the voice out of the
midst of the ḥoshek” (darkness) (ibid. v. 2); again, “All ḥoshek
(darkness) shall be hid in his secret places: a fire not blown shall
consume him” (Job xx. 26). The element fire is called ḥoshek
because it is not luminous, it is only transparent; for if it were
luminous we should see at night the whole atmosphere in flames.
The order of the four elements, according to the natural position is
here described; namely, first earth, above it water, air close to water,
and fire above air; for by placing [214]air over water, ḥoshek (fire),
which is “upon the face of the deep,” is undoubtedly above air. It
was here necessary to use the term ruaḥ elohim, because air is
described here as in motion (meraḥefet), and the motion of the air
is, as a rule, ascribed to God; comp. “And there went forth a wind
from the Lord” (Num. xi. 31); “Thou didst blow with thy wind”
(Exod. xv. 10); “And the Lord turned a mighty strong west wind”
(ibid. x. 19), and the like. As the first ḥoshek, which denotes the
element fire, is different from the ḥoshek mentioned further on in
the sense of “darkness,” the latter is explained and distinguished
from the former, according to our explanation, in the words, “And
darkness he called Night.” This is now clear.

The phrase, “And he divided between the waters,” etc., does not
describe a division in space, as if the one part were merely above
the other, whilst the nature of both remained the same, but a
distinction as regards their nature or form. One portion of that which
was first called water was made one thing by certain properties it
received, and another portion received a different form, and this
latter portion is that which is commonly called water and of this it is
said, “And the gathering of the waters he called Seas.” Scripture
even indicates that the first mayim (“water”) in the phrase, “On the
face of the waters,” does not refer to the waters which form the
seas; and that part of the element “water,” having received a
particular form, and being above the air, is distinguished from the
other part which has received the form of ordinary water. For the
words, “And he divided between the waters which are beneath the
firmament and the waters which are above the firmament,” are
similar in meaning to the phrase, “And God divided between the light
and the darkness,” and refer to a distinction by a separate form. The
firmament itself was formed of water; and in the words of our Sages
(Bereshit Rabba; cap. iv.), “The middle drop congealed and formed
the heavens.”

Here likewise Scripture says, in accordance with what I said above,


“And God called the firmament Heaven” (Gen. i. 8), in order to
explain the homonymity of the term shamayim (heaven), and to
show that shamayim in the first verse is not the firmament which is
also called shamayim (heaven). The difference is more clearly
expressed in the words, “In the open firmament of heaven” (ibid. i.
20); here it is shown that “firmament” (rakiʻa), and “heaven”
(shamayim), are two different things. In consequence of this
homonymity of the term shamayim the term rakiʻa (firmament) is
also used of the true heaven, just as the real firmament is
sometimes called shamayim (heaven); comp. “And God set them in
the rakiʻa (firmament) of the heaven” (ibid. i. 17).

This verse shows clearly that the stars, the sun, and the moon are
not, as people believe, on the surface of the spheres, but they are
fixed in the spheres, and this has been proved satisfactorily, there
being no vacuum in the Universe; for it is said, “in the firmament of
the heaven,” and not “upon the firmament of the heaven.”

It is therefore clear that there has been one common element called
water, which has been afterwards distinguished by three different
forms; one part forms the seas, another the firmament, and a third
part is over the firmament, and all this is separate from the earth.
The Scriptural text follows here a [215]peculiar method in order to
indicate some extraordinary mysteries. It has also been declared by
our Sages that the portion above the firmament is only water by
name, not in reality, for they say (Babyl. Talmud, Ḥagigah 14b) “Four
entered the paradise,” etc. R. Akiba said to them, “When you come
to the stores of pure marble, do not say, Water, water, for it is
written, ‘He that telleth lies shall not tarry in my sight’ ” (Ps. ci. 7).
Consider, if you belong to the class of thinking men, how clearly and
distinctly this passage explains the subject for those who reflect on
it! Understand that which has been proved by Aristotle in his book
On Meteorology, and note whatever men of science have said on
meteorological matters.

It is necessary to inquire into the reason why the declaration “that it


was good” is not found in the account of the second day of the
Creation. The various Midrashic sayings of our Sages on this point
are well known; the best of them is the explanation that the creation
of the water was not completed on that day. According to my
opinion the reason is likewise clear, and is as follows: When the
creation of any part of the Universe is described that is permanent,
regular, and in a settled order, the phrase “that it is good” is used.
But the account of the firmament, with that which is above it and is
called water, is, as you see, of a very mysterious character. For if
taken literally the firmament would appear at first thought to be
merely an imaginary thing, as there is no other substance but the
elements between us and the lowest of the heavenly spheres, and
there is no water above the air; and if the firmament, with that
which is over it, be supposed to be above the heavens, it would a
fortiori seem to be unreal and uncomprehensible. But if the account
be understood in a figurative sense and according to its true
meaning, it is still more mysterious, since it was considered
necessary to make this one of the most hidden secrets, in order to
prevent the multitude from knowing it. This being the case, how
could it be said [of the creation of the second day] “that it was
good”? This phrase would tell us that it is perfectly clear what share
the thing to which it refers takes in the permanent existence of the
Universe. But what good can people find in a thing whose real
nature is hidden, and whose apparent nature is not real? Why,
therefore, should it be said in reference to it, “that it was good”? I
must, however, give the following additional explanation. Although
the result of the second day’s creation forms an important element
among the existing things, the firmament was not its primary object
in the organization of the Universe, and therefore it could not be
said “that it was good”; it was only the means for the uncovering of
the earth. Note this. Our Sages have already explained that the
herbs and trees, which God caused to spring forth from the ground,
were caused by God to grow, after He had sent down rain upon
them; and the passage beginning, “And there went up a mist from
the earth” (ii. 6), refers to that which took place before the creative
act, related in the words, “Let the earth bring forth grass,” etc. (i.
ii.). Therefore Onkelos translates it: “And there had gone up a mist
from the earth.” It is also evident from the text itself, where it is
distinctly said, “And every plant in the field before it was in the
earth,” etc. (ii. 5). This question is now explained.

It is well known to every philosopher that the principal causes of


production and destruction, after the influence of the spheres, are
light and darkness, [216]in so far as these are accompanied by heat
and cold. For by the motion of the spheres the elements intermix,
and by light and darkness their constitution changes. The first
change consists in the formation of two kinds of mist; these are the
first causes of meteorological phenomena, such as rain; they also
caused the formation of minerals, of plants, of animals, and at last
of man. It is likewise known that darkness is the natural property of
all things on earth; in them light is accidental, coming from an
external cause, and therefore everything remains in a state of rest in
the absence of light. The Scriptural account of the Creation follows
in every respect exactly the same order, without any deviation.

Note also the saying of our Sages: “When the Universe was created,
all things were created with size, intellect, and beauty fully
developed, i.e., everything was created perfect in magnitude and
form, and endowed with the most suitable properties; the word
ẓibyonam (their beauty) used here has the same meaning as ẓebi,
‘glory’ ” (Ezek. xx. 6). Note this likewise, for it includes a principle
fully established.

The following point now claims our attention. The account of the six
days of creation contains, in reference to the creation of man, the
statement: “Male and female created he them” (i. 27), and
concludes with the words: “Thus the heavens and the earth were
finished, and all the host of them” (ii. 1), and yet the portion which
follows describes the creation of Eve from Adam, the tree of life, and
the tree of knowledge, the history of the serpent and the events
connected therewith, and all this as having taken place after Adam
had been placed in the Garden of Eden. All our Sages agree that this
took place on the sixth day, and that nothing new was created after
the close of the six days. None of the things mentioned above is
therefore impossible, because the laws of Nature were then not yet
permanently fixed. There are, however, some utterances of our
Sages on this subject [which apparently imply a different view]. I will
gather them from their different sources and place them before you,
and I will refer also to certain things by mere hints, just as has been
done by the Sages. You must know that their words, which I am
about to quote, are most perfect, most accurate, and clear to those
for whom they were said. I will therefore not add long explanations,
lest I make their statements plain, and I might thus become “a
revealer of secrets,” but I will give them in a certain order,
accompanied with a few remarks, which will suffice for readers like
you.

One of these utterances is this: “Adam and Eve were at first created
as one being, having their backs united; they were then separated,
and one half was removed and brought before Adam as Eve.” The
term mi-ẓalʻotav (lit. “of his ribs”) signifies “of his sides.” The
meaning of the word is proved by referring to ẓelʻa, “the side” of the
tabernacle (Exod. xxvi. 20), which Onkelos renders setar (“side”),
and so also mi-ẓalʻotav is rendered by him “mi-sitrohi” (of his sides).
Note also how clearly it has been stated that Adam and Eve were
two in some respects, and yet they remained one, according to the
words, “Bone of my bones, and flesh of my flesh” (Gen. ii. 23). The
unity of the two is proved by the fact that both have the same
name, for she is called ishshah (woman), because she was taken out
of ish (man), also by the words, “And shall cleave unto his wife, and
they shall be one flesh” (ii. 24). How great is the ignorance of those
who do not [217]see that all this necessarily includes some [other]
idea [besides the literal meaning of the words]. This is now clear.

Another noteworthy Midrashic remark of our Sages is the following:


“The serpent had a rider, the rider was as big as a camel, and it was
the rider that enticed Eve; this rider was Samaël.” Samaël is the
name generally applied by our Sages to Satan. Thus they say in
several places that Satan desired to entice Abraham to sin, and to
abstain from binding Isaac, and he desired also to persuade Isaac
not to obey his father. At the same time they also say, in reference
to the same subject, viz., the Akedah (“the binding of Isaac”), that
Samaël came to Abraham and said to him, “What! hast thou, being
an old man, lost thy senses?” etc. This shows that Samaël and Satan
are identical. There is a meaning in this name [Samaël], as there is
also in the name naḥash (“serpent”). In describing how the serpent
came to entice Eve, our Sages say: “Samaël was riding on it, and
God was laughing at both the camel and its rider.” It is especially of
importance to notice that the serpent did not approach or address
Adam, but all his attempts were directed against Eve, and it was
through her that the serpent caused injury and death to Adam. The
greatest hatred exists between the serpent and Eve, and between
his seed and her seed; her seed being undoubtedly also the seed of
man. More remarkable still is the way in which the serpent is joined
to Eve, or rather his seed to her seed; the head of the one touches
the heel of the other. Eve defeats the serpent by crushing its head,
whilst the serpent defeats her by wounding her heel. This is likewise
clear.

The following is also a remarkable passage, most absurd in its literal


sense; but as an allegory it contains wonderful wisdom, and fully
agrees with real facts, as will be found by those who understand all
the chapters of this treatise. When the serpent came to Eve he
infected her with poison; the Israelites, who stood at Mount Sinai,
removed that poison; idolaters, who did not stand at Mount Sinai,
have not got rid of it. Note this likewise. Again they said: “The tree
of life extends over an area of five hundred years’ journey, and it is
from beneath it that all the waters of the creation sprang forth”; and
they added the explanation that this measure referred to the
thickness of its body, and not to the extent of its branches, for they
continue thus: “Not the extent of the branches thereof, but the stem
thereof [korato, lit., ‘its beam,’ signifying here ‘its stem’] has a
thickness of five hundred years’ journey.” This is now sufficiently
clear. Again: “God has never shown the tree of knowledge [of good
and evil] to man, nor will He ever show it.” This is correct, for it
must be so according to the nature of the Universe. Another
noteworthy saying is this: “And the Lord God took the man, i.e.,
raised him, and placed him in the Garden of Eden,” i.e., He gave him
rest. The words “He took him,” “He gave him,” have no reference to
position in space, but they indicate his position in rank among
transient beings, and the prominent character of his existence.
Remarkable and noteworthy is the great wisdom contained in the
names of Adam, Cain, and Abel, and in the fact that it was Cain who
slew Abel in the field, that both of them perished, although the
murderer had some respite, and that the existence of mankind is
due to Seth alone. Comp. “For God has appointed me another seed”
(iv. 25). This has proved true.

It is also necessary to understand and consider the words, “And


Adam [218]gave names” (ii. 20); here it is indicated that languages
are conventional, and that they are not natural, as has been
assumed by some. We must also consider the four different terms
employed in expressing the relations of the heavens to God, bore
(Creator), ʻoseh (Maker), koneh (Possessor), and el (God). Comp.
“God created the heaven and the earth” (i. 1); “In the day that God
made the earth and the heavens” (ii. 4); “Possessor of heaven and
earth” (xiv. 19); “God of the Universe” (xxi. 31); “The God of heaven
and the God of the earth” (xxiv. 3). As to the verbs, konen, “he
established,” tafaḥ, “he spanned,” and natah, “he stretched out,”
occurring in the following passages, “Which thou hast established”
(Ps. viii. 4), “My right hand hath spanned the heavens” (Isa. xviii.
13), “Who stretchest out the heavens” (Ps. civ. 2), they are included
in the term ʻasah (“he made”); the verb yaẓar, “he formed,” does not
occur in reference to the heavens. According to my opinion the verb
yaẓar denotes to make a form, a shape, or any other accident (for
form and shape are likewise accidents). It is therefore said, yoẓer or,
“Who formeth the light” (Isa. xiv. 7), light being an accident; yoẓer
harim, “That formeth the mountains” (Amos iv. 13), i.e., that gave
them their shape. In the same sense the verb is used in the
passage, “And the Lord God formed (va-yiẓer) all the beasts,” etc.
(Gen. ii. 7). But in reference to the Universe, viz., the heavens and
the earth, which comprises the totality of the Creation, Scripture
employs the verb bara, which we explain as denoting he produced
something from nothing; also ʻasah (“he made”), on account of the
general forms or natural properties of the things which were given to
them; kanah, “he possessed,” because God rules over them like a
master over his servants. For this reason He is also called, “The Lord
of the whole earth” (Jos. iii. 11–13); ha-adon, “the Lord” (Exod. xx.,
iii. 17). But although none can be a master unless there exists
something that is in his possession, this attribute cannot be
considered to imply the belief in the eternal existence of a materia
prima, since the verbs bara, “he created,” and ʻasah, “he made,” are
also employed in reference to the heavens. The Creator is called the
God of the heavens and the God of the Universe, on account of the
relations between Him and the heavens; He governs, and they are
governed; the word elohim does not signify “master” in the sense of
“owner”; it expresses the relation between His position in the totality
of existing beings, and the position of the heavens or the Universe;
He is God, not they, i.e., not the heavens. Note this.

This, together with those explanations which we have given, and


which we intend to give, in reference to this subject, may suffice,
considering the object of this treatise and the capacity of the reader.

[Contents]
CHAPTER XXXI
It is perhaps clear why the laws concerning Sabbath are so severe,
that their transgression is visited with death by stoning, and that the
greatest of the prophets put a person to death for breaking the
Sabbath. The commandment of the Sabbath is the third from the
commandment concerning the existence and the unity of God. For
the commandment not to worship any other being is merely an
explanation of the first. You know already from what I have said,
that no opinions retain their vitality except those which are
[219]confirmed, published, and by certain actions constantly revived
among the people. Therefore we are told in the Law to honour this
day; in order to confirm thereby the principle of Creation which will
spread in the world, when all peoples keep Sabbath on the same
day. For when the question is asked, why this is done, the answer is
given: “For in six days the Lord hath made,” etc. (Exod. xx. 11). Two
different reasons are given for this commandment, because of two
different objects. In the Decalogue in Exodus, the following reason is
given for distinguishing the Sabbath: “For in six days,” etc. But in
Deuteronomy (chap. v. 15) the reason is given: “And thou shalt
remember that thou hast been a slave in the land of Egypt, etc.,
therefore the Lord thy God commanded thee,” etc. This difference
can easily be explained. In the former, the cause of the honour and
distinction of the day is given; comp. “Therefore the Lord hath
blessed the day of the Sabbath and sanctified it” (Exod. xx. 10), and
the cause for this is, “For in six days,” etc. But the fact that God has
given us the law of the Sabbath and commanded us to keep it, is the
consequence of our having been slaves; for then our work did not
depend on our will, nor could we choose the time for it; and we
could not rest. Thus God commanded us to abstain from work on
the Sabbath, and to rest, for two purposes; namely, (1) That we
might confirm the true theory, that of the Creation, which at once
and clearly leads to the theory of the existence of God. (2) That we
might remember how kind God has been in freeing us from the
burden of the Egyptians.—The Sabbath is therefore a double
blessing: it gives us correct notions, and also promotes the well-
being of our bodies.

[Contents]
CHAPTER XXXII
There are as many different opinions concerning Prophecy as
concerning the Eternity or Non-Eternity of the Universe. For we have
shown that those who assume the existence of God as proved may
be divided into three classes, according to the view they take of the
question, whether the Universe is eternal or not. Similarly there are
three different opinions on Prophecy. I will not notice the view of the
Atheist; he does not believe in the Existence of God, much less in
Prophecy; but I will content myself with discussing the various
opinions [on Prophecy] held by those who believe in God.

1. Among those who believe in Prophecy, and even among our


coreligionists, there are some ignorant people who think as follows:
God selects any person He pleases, inspires him with the spirit of
Prophecy, and entrusts him with a mission. It makes no difference
whether that person be wise or stupid, old or young; provided he
be, to some extent, morally good. For these people have not yet
gone so far as to maintain that God might also inspire a wicked
person with His spirit. They admit that this is impossible, unless God
has previously caused him to improve his ways.

2. The philosophers hold that prophecy is a certain faculty of man in


a state of perfection, which can only be obtained by study. Although
the faculty is common to the whole race, yet it is not fully developed
in each individual, either on account of the individual’s defective
constitution, or on account of some other external cause. This is the
case with every faculty common to a class. It is only brought to a
state of perfection in some individuals, [220]and not in all; but it is
impossible that it should not be perfect in some individual of the
class; and if the perfection is of such a nature that it can only be
produced by an agent, such an agent must exist. Accordingly, it is
impossible that an ignorant person should be a prophet; or that a
person being no prophet in the evening, should, unexpectedly on the
following morning, find himself a prophet, as if prophecy were a
thing that could be found unintentionally. But if a person, perfect in
his intellectual and moral faculties, and also perfect, as far as
possible, in his imaginative faculty, prepares himself in the manner
which will be described, he must become a prophet; for prophecy is
a natural faculty of man. It is impossible that a man who has the
capacity for prophecy should prepare himself for it without attaining
it, just as it is impossible that a person with a healthy constitution
should be fed well, and yet not properly assimilate his food; and the
like.

3. The third view is that which is taught in Scripture, and which


forms one of the principles of our religion. It coincides with the
opinion of the philosophers in all points except one. For we believe
that, even if one has the capacity for prophecy, and has duly
prepared himself, it may yet happen that he does not actually
prophesy. It is in that case the will of God [that withholds from him
the use of the faculty]. According to my opinion, this fact is as
exceptional as any other miracle, and acts in the same way. For the
laws of Nature demand that every one should be a prophet, who has
a proper physical constitution, and has been duly prepared as
regards education and training. If such a person is not a prophet, he
is in the same position as a person who, like Jeroboam (1 Kings xiii.
4), is deprived of the use of his hand, or of his eyes, as was the case
with the army of Syria, in the history of Elisha (2 Kings vi. 18). As for
the principle which I laid down, that preparation and perfection of
moral and rational faculties are the sine quâ non, our Sages say
exactly the same: “The spirit of prophecy only rests upon persons
who are wise, strong, and rich.” We have explained these words in
our Commentary on the Mishnah, and in our large work. We stated
there that the Sons of the Prophets were constantly engaged in
preparation. That those who have prepared themselves may still be
prevented from being prophets, may be inferred from the history of
Baruch, the son of Nerijah; for he followed Jeremiah, who prepared
and instructed him; and yet he hoped in vain for prophecy; comp., “I
am weary with my sighing, and rest have I not found.” He was then
told through Jeremiah, “Thus saith the Lord, Thus shalt thou say to
him, Thou seekest for thee great things, do not seek” (Jer. xlv. 5). It
may perhaps be assumed that prophecy is here described as a thing
“too great” for Baruch. So also the fact that “her prophets did not
find visions from the Lord” (Lam. ii. 4), may be considered as the
result of the exile of her prophets, as will be explained (chap.
xxxvi.). There are, however, numerous passages in Scripture as well
as in the writings of our Sages, which support the principle that it
depends chiefly on the will of God who is to prophesy, and at what
time; and that He only selects the best and the wisest. We hold that
fools and ignorant people are unfit for this distinction. It is as
impossible for any one of these to prophesy as it is for an ass or a
frog; for prophecy is impossible without study and training; when
these have created the possibility, then it depends on the will of God
whether the possibility is to be turned into reality. We must not be
misled by the [221]words of Jeremiah (i. 5), “Before I formed thee in
the womb I knew thee, and before thou camest forth from the
womb I have sanctified thee”; for this is the case with all prophets;
there must be a physical preparation from the beginning of their
existence, as will be explained. As to the words, “For I am young”
(ibid. ver. 6), it is well known that the pious Joseph, when he was
thirty years old, is called by the Hebrew “young” (naʻar); also
Joshua, when he was nearly sixty years old. For the statement, “and
his minister Joshua, the son of Nun, was young,” occurs in the
account of the Golden Calf (Exod. xxxiii. 11). Moses was then eighty-
one years old, he lived one hundred and twenty years; Joshua, who
survived him fourteen years, lived one hundred and ten years and
must consequently have been at least fifty-seven years old at the
time when the Golden Calf was made, and yet he is called naʻar,
“young.” Nor must we be misled by prophecies like the following: “I
will pour out my spirit over all flesh, and your sons and your
daughters shall prophesy”; since it is distinctly stated what is meant
by “prophesy” in this place, viz., “Your old men will dream dreams,
your young men shall see visions.” For we call also prophets all those
who reveal something unknown by surmises, or conjectures, or
correct inferences. Thus “prophets of Baal” and “of Asherah” are
mentioned in Scripture. And God says, “If there arise among you a
prophet or a dreamer of dreams,” etc. (Deut. xiii. 1). As to the
revelation on Mount Sinai, all saw the great fire, and heard the
fearful thunderings, that caused such an extraordinary terror; but
only those of them who were duly qualified were prophetically
inspired, each one according to his capacities. Therefore it is said,
“Come up unto the Lord, thou and Aaron, Nadab and Abihu.” Moses
rose to the highest degree of prophecy, according to the words, “And
Moses alone shall come near the Lord.” Aaron was below him, Nadab
and Abihu below Aaron, and the seventy elders below Nadab and
Abihu, and the rest below the latter, each one according to his
degree of perfection. Similarly our Sages wrote: Moses had his own
place and Aaron his own. Since we have touched upon the revelation
on Mount Sinai, we will point out in a separate chapter what may be
inferred as regards the nature of that event, both from the Scriptural
text, in accordance with reasonable interpretation, and from the
words of our Sages.

[Contents]
CHAPTER XXXIII
It is clear to me that what Moses experienced at the revelation on
Mount Sinai was different from that which was experienced by all the
other Israelites, for Moses alone was addressed by God, and for this
reason the second person singular is used in the Ten
Commandments; Moses then went down to the foot of the mount
and told his fellow-men what he had heard. Comp., “I stood
between the Lord and you at that time to tell you the word of the
Lord” (Deut. v. 5). Again, “Moses spake, and God answered him with
a loud voice” (Exod. xix. 19). In the Mechilta our Sages say distinctly
that he brought to them every word as he had heard it.
Furthermore, the words, “In order that the people hear when I
speak with thee” (Exod. xix. 9), show that God spoke to Moses, and
the people only heard the mighty sound, not distinct words. It is to
the perception of this mighty sound that Scripture [222]refers in the
passage, “When ye hear the sound” (Deut. v. 20); again it is stated,
“You heard a sound of words” (ibid. iv. 12), and it is not said “You
heard words”; and even where the hearing of the words is
mentioned, only the perception of the sound is meant. It was only
Moses that heard the words, and he reported them to the people.
This is apparent from Scripture, and from the utterances of our
Sages in general. There is, however, an opinion of our Sages
frequently expressed in the Midrashim, and found also in the
Talmud, to this effect: The Israelites heard the first and the second
commandments from God, i.e., they learnt the truth of the principles
contained in these two commandments in the same manner as
Moses, and not through Moses. For these two principles, the
existence of God and His Unity, can be arrived at by means of
reasoning, and whatever can be established by proof is known by
the prophet in the same way as by any other person; he has no
advantage in this respect. These two principles were not known
through prophecy alone. Comp., “Thou hast been shown to know
that,” etc. (Deut. iv. 34). But the rest of the commandments are of
an ethical and authoritative character, and do not contain [truths]
perceived by the intellect. Notwithstanding all that has been said by
our Sages on this subject, we infer from Scripture as well as from
the words of our Sages, that the Israelites heard on that occasion a
certain sound which Moses understood to proclaim the first two
commandments, and through Moses all other Israelites learnt them
when he in intelligible sounds repeated them to the people. Our
Sages mention this view, and support it by the verse, “God hath
spoken once; twice have I heard this” (Ps. lxii. 11). They state
distinctly, in the beginning of Midrash Ḥazita, that the Israelites did
not hear any other command directly from God; comp. “A loud voice,
and it was not heard again” (Deut. v. 19). It was after this first
sound was heard that the people were seized with the fear and
terror described in Scripture, and that they said, “Behold the Lord
our God has shown us, etc., and now why shall we die, etc. Come
thou near,” etc. Then Moses, the most distinguished of all mankind,
came the second time, received successively the other
commandments, and came down to the foot of the mountain to
proclaim them to the people, whilst the mighty phenomena
continued; they saw the fire, they heard the sounds, which were
those of thunder and lightning during a storm, and the loud sound of
the shofar; and all that is said of the many sounds heard at that
time, e.g., in the verse, “and all the people perceived the sounds,”
etc., refers to the sound of the shofar, thunder, and similar sounds.
But the voice of the Lord, that is, the voice created for that purpose,
which was understood to include the diverse commandments, was
only heard once, as is declared in the Law, and has been clearly
stated by our Sages in the places which I have indicated to you.
When the people heard this voice their soul left them; and in this
voice they perceived the first two commandments. It must, however,
be noticed that the people did not understand the voice in the same
degree as Moses did. I will point out to you this important fact, and
show you that it was a matter of tradition with the nation, and well
known by our Sages. For, as a rule, Onkelos renders the word va-
yedabber by u-mallel (“and God spake”); this is also the case with
this word in the beginning of the twentieth chapter of Exodus, but
the words ve-al yedabber immanu elohim, “let not God speak to us”
[223](Exod. xx. 19), addressed by the people to Moses, is rendered
vela yitmallel immanu min kodam adonai (“Let not aught be spoken
to us by the Lord”). Onkelos makes thus the same distinction which
we made. You know that according to the Talmud Onkelos received
all these excellent interpretations directly from R. Eliezer and R.
Joshua, the wisest men in Israel. Note it, and remember it, for it is
impossible for any person to expound the revelation on Mount Sinai
more fully than our Sages have done, since it is one of the secrets of
the Law. It is very difficult to have a true conception of the events,
for there has never been before, nor will there ever be again,
anything like it. Note it.

[Contents]
CHAPTER XXXIV
The meaning of the Scriptural passage, “Behold I will send an angel
before thee,” etc. (Exod. xxiii. 20), is identical with the parallel
passage in Deuteronomy which God is represented to have
addressed to Moses at the revelation on Mount Sinai, namely, “I will
raise them up a prophet from among their brethren,” etc. (Deut.
xviii. 18). The words, “Beware of him, and obey his voice,” etc., said
in reference to the angel, prove [that this passage speaks of a
prophet]. For there is no doubt that the commandment is given to
the ordinary people, to whom angels do not appear with
commandments and exhortations, and it is therefore unnecessary to
tell them not to disobey him. The meaning of the passage quoted
above is this: God informs the Israelites that He will raise up for
them a prophet, to whom an angel will appear in order to speak to
him, to command him, and to exhort him; he therefore cautions
them not to rebel against this angel, whose word the prophet will
communicate to them. Therefore it is expressly said in Deuteronomy,
“Unto him ye shall hearken” (Deut. xviii. 15); “And it shall come to
pass that whosoever shall not hearken unto my words which he shall
speak in my name,” etc. (ibid. 19). This is the explanation of the
words, “for my name is in him” (Exod. xxiv. 21). The object of all this
is to say to the Israelites, This great sight witnessed by you, the
revelation on Mount Sinai, will not continue for ever, nor will it ever
be repeated. Fire and cloud will not continually rest over the
tabernacle, as they are resting now on it; but the towns will be
conquered for you, peace will be secured for you in the land, and
you will be informed of what you have to do, by an angel whom I
will send to your prophets; he will thus teach you what to do, and
what not to do. Here a principle is laid down which I have constantly
expounded, viz., that all prophets except Moses receive the prophecy
through an angel. Note it.

[Contents]
CHAPTER XXXV
I have already described the four points in which the prophecy of
Moses our Teacher was distinguished from that of other prophets, in
books accessible to every one, in the Commentary on the Mishnah
(Sanhedrin x. 1) and in Mishneh-torah (S. Maddʻa I. vii. 6); I have
also adduced evidence for my explanation, and shown the
correctness thereof. I need not repeat the subject here, nor is it
included in the theme of this work. For I must tell you that whatever
I say here of prophecy refers exclusively to the form of the prophecy
of all prophets before and after [224]Moses. But as to the prophecy of
Moses I will not discuss it in this work with one single word, whether
directly or indirectly, because, in my opinion, the term prophet is
applied to Moses and other men homonymously. A similar
distinction, I think, must be made between the miracles wrought by
Moses and those wrought by other prophets, for his signs are not of
the same class as the miracles of other prophets. That his prophecy
was distinguished from that of all his predecessors is proved by the
passage, “And I appeared to Abraham, etc., but by my name, the
Lord, I was not known unto them” (Exod. vi. 3). We thus learn that
his prophetic perception was different from that of the Patriarchs,
and excelled it; a fortiori it must have excelled that of other prophets
before Moses. As to the distinction of Moses’ prophecy from that of
succeeding prophets, it is stated as a fact, “And there arose not a
prophet since in Israel like unto Moses, whom the Lord knew face to
face” (Deut. xxxiv. 10). It is thus clear that his prophetic perception
was above that of later prophets in Israel, who are “a kingdom of
priests and a holy nation,” and “in whose midst is the Lord”; much
more is it above that of prophets among other nations.
The general distinction between the wonders of Moses and those of
other prophets is this: The wonders wrought by prophets, or for
them, are witnessed by a few individuals, e.g., the wonders wrought
by Elijah and Elisha; the king of Israel is therefore surprised, and
asked Gehazi to describe to him the miracles wrought by Elisha: “Tell
me, I pray thee, all the great things that Elisha hath done. And it
came to pass as he was telling, etc. And Gehazi said: ‘My lord, O
king, this is the woman, and this is her son, whom Elisha restored to
life’ ” (2 Kings viii. 4, 5). The same is the case with the signs of every
other prophet, except Moses our Teacher. Scripture, therefore
declares that no prophet will ever, like Moses, do signs publicly in the
presence of friend and enemy, of his followers and his opponents;
this is the meaning of the words: “And there arose not a prophet
since in Israel like unto Moses, etc., in all the signs and the wonders,
etc., in the sight of all Israel.” Two things are here mentioned
together; namely, that there will not arise a prophet that will
perceive as Moses perceived, or a prophet that will do as he did;
then it is pointed out that the signs were made in the presence of
Pharaoh, all his servants and all his land, the opponents of Moses,
and also in the presence of all the Israelites, his followers. Comp. “In
the sight of all Israel.” This is a distinction not possessed by any
prophet before Moses; nor, as is correctly foretold, will it ever be
possessed by another prophet. We must not be misled by the
account that the light of the sun stood still certain hours for Joshua,
when “he said in the sight of Israel,” etc. (Josh. x. 12); for it is not
said there “in the sight of all Israel,” as is said in reference to Moses.
So also the miracle of Elijah, at Mount Carmel, was witnessed only
by a few people. When I said above that the sun stood still certain
hours, I explain the words “ka-jom tamim” to mean “the longest
possible day,” because tamim means “perfect,” and indicates that
that day appeared to the people at Gibeon as their longest day in
the summer. Your mind must comprehend the distinction of the
prophecy and the wonders of Moses, and understand that his
greatness in prophetic perception was the same as his power of
producing miracles. If you further assume that we are unable fully to
comprehend the nature of this greatness, you will understand
[225]that when I speak, in the chapters which follow this, on
prophecy and the different classes of prophets, I only refer to the
prophets which have not attained the high degree that Moses
attained. This is what I desired to explain in this chapter.

[Contents]
CHAPTER XXXVI
Prophecy is, in truth and reality, an emanation sent forth by the
Divine Being through the medium of the Active Intellect, in the first
instance to man’s rational faculty, and then to his imaginative
faculty; it is the highest degree and greatest perfection man can
attain; it consists in the most perfect development of the imaginative
faculty. Prophecy is a faculty that cannot in any way be found in a
person, or acquired by man, through a culture of his mental and
moral faculties; for even if these latter were as good and perfect as
possible, they would be of no avail, unless they were combined with
the highest natural excellence of the imaginative faculty. You know
that the full development of any faculty of the body, such as the
imagination, depends on the condition of the organ, by means of
which the faculty acts. This must be the best possible as regards its
temperament and its size, and also as regards the purity of its
substance. Any defect in this respect cannot in any way be supplied
or remedied by training. For when any organ is defective in its
temperament, proper training can in the best case restore a healthy
condition to some extent, but cannot make such an organ perfect.
But if the organ is defective as regards size, position, or as regards
the substance and the matter of which the organ is formed, there is
no remedy. You know all this, and I need not explain it to you at
length.

Part of the functions of the imaginative faculty is, as you well know,
to retain impressions by the senses, to combine them, and chiefly to
form images. The principal and highest function is performed when
the senses are at rest and pause in their action, for then it receives,
to some extent, divine inspiration in the measure as it is predisposed
for this influence. This is the nature of those dreams which prove
true, and also of prophecy, the difference being one of quantity, not
of quality. Thus our Sages say, that dream is the sixtieth part of
prophecy; and no such comparison could be made between two
things of different kinds, for we cannot say the perfection of man is
so many times the perfection of a horse. In Bereshit Rabba (sect.
xvii.) the following saying of our Sages occurs, “Dream is the nobelet
(the unripe fruit) of prophecy.” This is an excellent comparison, for
the unripe fruit (nobelet) is really the fruit to some extent, only it
has fallen from the tree before it was fully developed and ripe. In a
similar manner the action of the imaginative faculty during sleep is
the same as at the time when it receives a prophecy, only in the first
case it is not fully developed, and has not yet reached its highest
degree. But why need I quote the words of our Sages, when I can
refer to the following passage of Scripture: “If there be among you a
prophet, I, the Lord, will make myself known unto him in a vision, in
a dream will I speak to him” (Num. xii. 6). Here the Lord tells us
what the real essence of prophecy is, that it is a perfection acquired
in a dream or in a vision (the original mareh is a noun derived from
the verb raah); the imaginative faculty acquires such an efficiency in
its action that it sees [226]the thing as if it came from without, and
perceives it as if through the medium of bodily senses. These two
modes of prophecy, vision and dream, include all its different
degrees. It is a well-known fact that the thing which engages greatly
and earnestly man’s attention whilst he is awake and in the full
possession of his senses forms during his sleep the object of the
action of his imaginative faculty. Imagination is then only influenced
by the intellect in so far as it is predisposed for such influence. It
would be quite useless to illustrate this by a simile, or to explain it
fully, as it is clear, and every one knows it. It is like the action of the
senses, the existence of which no person with common sense would
ever deny. After these introductory remarks you will understand that
a person must satisfy the following conditions before he can become
a prophet: The substance of the brain must from the very beginning
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