Preface

In the summer of 2000, Microsoft released C# 1.0, which together with the .NET Framework, represented a major change in the way Windows applications and web applications were to be built.

In November 2005, Microsoft released C# 2.0, also known as C# 2005, and an upgraded platform, class library, and set of tools, including Visual Studio 2005. Throughout this book, we’ll refer to C# 2.0 as C# 2005. Although not quite as revolutionary a step as the launch of .NET itself, 2.0 represents a significant maturation of the .NET platform and of the C# language, along with greatly enhanced tools to significantly increase programmer productivity.

About This Book

Learning C# 2005, Second Edition is a primer on the C# 2005 language specifically, and object-oriented software development in general. This book focuses on the fundamentals of the C# programming language, both syntactical and semantic. After mastering these concepts, you should be ready to move on to a more advanced programming guide that will help you create large-scale web and Windows applications.

Who This Book Is For

Learning C# 2005 was written for programmers with little or no object-oriented programming experience, as well as for novice programmers. Those coming from another language may have a slight advantage, but I’ve tried to provide an on-ramp for beginners as well, by defining all terms, demonstrating the relationships among the various constructs, and reviewing key concepts along the way.

How the Book Is Organized

Chapter 1, C# and .NET Programming

Introduces you to the C# language and the .NET platform.

Chapter 2, Visual Studio 2005

Provides a guided tour of Visual Studio 2005—the tool you will use to build all the applications in the book, and perhaps all the applications you’ll build from now on.

Chapter 3, C# Language Fundamentals

Introduces the basic syntax and structure of the C# language, including the intrinsic types, variables, statements, and expressions.

Chapter 4, Operators

Describes some of the symbols that cause C# to take an action, such as assigning a value to a variable and arithmetically operating on values (adding, subtracting, and so forth).

Chapter 5, Branching

Shows how to create programs that branch based on conditions that may change while the program is running.

Chapter 6, Object-Oriented Programming

Explains the principles behind object-oriented programming, including encapsulation, specialization, and polymorphism.

Chapter 7, Classes and Objects

Introduces the key concepts of programmer-defined types (classes) and instances of those types (objects). Classes and objects are the building blocks of object-oriented programming.

Chapter 8, Inside Methods

Delves into the specific programming instructions you’ll write to define the behavior of objects.

Chapter 9, Basic Debugging

Introduces the debugger integrated into the Visual Studio 2005 Integrated Development Environment.

Chapter 10, Arrays

Introduces the array, an indexed collection of objects that are all the same type.

Chapter 11, Inheritance and Polymorphism

Explores two of the key concepts behind object-oriented programming, inheritance and polymorphism, and demonstrates how you might implement them in your code.

Chapter 12, Operator Overloading

Explains how to add standard operators to the types you define.

Chapter 13, Interfaces

Explains how you can define a set of behaviors (an interface) that any number of classes might implement.

Chapter 14, Generics and Collections

Explains generics and shows how generics are used to create type-safe and efficient collections. Chapter 14 then introduces the standard .NET Framework generic collections: List, Stack, Queue, and Dictionary.

Chapter 15, Strings

Discusses the manipulation of strings of characters, the C# string class, and regular expression syntax.

Chapter 16, Throwing and Catching Exceptions

Explains how to handle errors and abnormal conditions that may arise in relation to your programs through the use of exceptions.

Chapter 17, Delegates and Events

Discusses how to write code to respond to programming occurrences like mouse clicks, keystrokes, and other events, through the use of delegates and the event keyword.

Chapter 18, Creating Windows Applications

Shows you how to bring all these skills to bear to create a Windows application.

Chapter 19, Programming ASP.NET Applications

Applies the same skills to building a web application.

Appendix, Answers to Quizzes and Exercises

Answers to all the chapter quizzes and exercises, with complete code examples.

Conventions Used in This Book

The following font conventions are used in this book:

Italic

Used for pathnames, filenames, program names, Internet addresses (such as domain names and URLs), and new terms where they are defined

Constant Width

Used for command lines and options that should be typed verbatim, C# keywords, and code examples

Constant Width Italic

Used for replaceable items, such as variables or optional elements, within syntax lines or code

Constant Width Bold

Used for emphasis within program code

Pay special attention to notes set apart from the text with the following icons:

This book is here to help you get your job done. In general, you may use the code in this book in your programs and documentation. You do not need to contact us for permission unless you’re reproducing a significant portion of the code. For example, writing a program that uses several chunks of code from this book does not require permission. Selling or distributing a CD-ROM of examples from O’Reilly books does require permission. Answering a question by citing this book and quoting example code does not require permission. Incorporating a significant amount of example code from this book into your product’s documentation does require permission.

We appreciate, but do not require, attribution. An attribution usually includes the title, author, publisher, and ISBN. For example: "Learning C# 2005, Second Edition, by Jesse Liberty and Brian MacDonald. Copyright 2006 O’Reilly Media, Inc., 0-596-10209-7.”

If you feel your use of code examples falls outside fair use or the permission given above, feel free to contact us at .

Support

I provide ongoing support for my books through my web site (click on “books”):

http://www.LibertyAssociates.com

On this web site, you’ll also find the source code for all the examples in Learning C# 2005, Second Edition, as well as access to a free online support discussion forum. You’ll also find an errata, FAQ, and other useful resources.

We’d Like to Hear from You

We have tested and verified the information in this book to the best of our ability, but you may find that features have changed (or even that we have made mistakes!). Please let us know about any errors you find, as well as your suggestions for future editions, by writing to:

O’Reilly Media, Inc.
1005 Gravenstein Highway North
Sebastopol, CA 95472
(800) 998-9938 (in the U.S. or Canada)
(707) 829-0515 (international/local)
(707) 829-0104 (fax)

We have a web page for this book where we list examples and any plans for future editions. You can access this information at:

http://www.oreilly.com/catalog/learncsharp2

You can also send messages electronically. To be put on the mailing list or request a catalog, send email to:

To comment on the book, send email to:

For more information about this book and others, as well as additional technical articles and discussion on the C# and the .NET Framework, see the O’Reilly web site:

http://www.oreilly.com

and the O’Reilly .NET DevCenter:

http://www.oreillynet.com/dotnet

ONDotnet.com provides independent coverage of fundamental, interoperable, and emerging Microsoft .NET programming and web services technologies.

Safari® Enabled

When you see a Safari® Enabled icon on the cover of your favorite technology book, that means the book is available online through Safari Books Online.

Safari offers a solution that’s better than e-books. It’s a virtual library that lets you easily search thousands of top tech books, cut and paste code samples, download chapters, and find quick answers when you need the most accurate, current information. Try it for free at http://safari.oreilly.com.

Acknowledgments

Jesse Liberty

John Osborn signed me to O’Reilly, for which I will forever be in his debt. William Hamilton and Nicholas Paldino helped make this book better than what I’d written. Rob Romano created a number of the illustrations and improved the others. Tim O’Reilly provided support and resources.

Special thanks are due to my co-author, Brian MacDonald, who made this book far superior to what I’d originally written.

Brian MacDonald

First and foremost, thanks to Jesse for giving me the opportunity to participate in this book. Many thanks also to John Osborn, for getting me involved with O’Reilly in the first place. Finally, thanks to my wife, Carole, who provided both moral support and technical assistance.