Contents

Introduction

Part I: The C# Language

Chapter 1: Introducing C#

What Is the .NET Framework?

What Is C#?

Visual Studio 2008

Summary

Chapter 2: Writing a C# Program

The Development Environments

Console Applications

Windows Forms Applications

Summary

Chapter 3: Variables and Expressions

Basic C# Syntax

Basic C# Console Application Structure

Variables

Expressions

Summary

Exercises

Chapter 4: Flow Control

Boolean Logic

The goto Statement

Branching

Looping

Summary

Exercises

Chapter 5: More About Variables

Type Conversion

Complex Variable Types

String Manipulation

Summary

Exercises

Chapter 6: Functions

Defining and Using Functions

Variable Scope

The Main() Function

Struct Functions

Overloading Functions

Delegates

Summary

Exercises

Chapter 7: Debugging and Error Handling

Debugging in VS and VCE

Error Handling

Summary

Exercises

Chapter 8: Introduction to Object-Oriented Programming

What Is Object-Oriented Programming?

OOP Techniques

OOP in Windows Applications

Summary

Exercises

Chapter 9: Defining Classes

Class Definitions in C#

System.Object

Constructors and Destructors

OOP Tools in VS and VCE

Class Library Projects

Interfaces Versus Abstract Classes

Struct Types

Summary

Exercises

Chapter 10: Defining Class Members

Member Definitions

Additional Class Member Topics

Interface Implementation

Partial Class Definitions

Partial Method Definitions

Example Application

Summary

Exercises

Chapter 11: Collections, Comparisons, and Conversions

Collections

Comparisons

Conversions

Summary

Exercises

Chapter 12: Generics

What Is a Generic?

Using Generics

Defining Generics

Summary

Exercises

Chapter 13: Additional OOP Techniques

The :: Operator and the Global Namespace Qualifier

Custom Exceptions

Events

Expanding and Using CardLib

Summary

Exercises

Chapter 14: C# 3.0 Language Enhancements

Initializers

Type Inference

Anonymous Types

Extension Methods

Lambda Expressions

Summary

Exercises

Part II: Windows Programming

Chapter 15: Basic Windows Programming

Controls

The Button Control

The Label and LinkLabel Controls

The TextBox Control

The RadioButton and CheckBox Controls

The RichTextBox Control

The ListBox and CheckedListBox Controls

The ListView Control

The TabControl Control

Summary

Exercises

Chapter 16: Advanced Windows Forms Features

Menus and Toolbars

Toolbars

SDI and MDI Applications

Creating Controls

Summary

Exercises

Chapter 17: Using Common Dialogs

Common Dialogs

How to Use Dialogs

File Dialogs

Printing

Print Preview

FontDialog and ColorDialog

Summary

Exercises

Chapter 18: Deploying Windows Applications

Deployment Overview

ClickOnce Deployment

Visual Studio Setup and Deployment Project Types

Microsoft Windows Installer Architecture

Creating an Installation Package for the SimpleEditor

Building the Project

Installation

Summary

Exercises

Part III: Web Programming

Chapter 19: Basic Web Programming

Overview

ASP.NET Runtime

Creating a Simple Page

Server Controls

Event Handlers

Input Validation

State Management

Authentication and Authorization

Reading and Writing to an SQL Server Database

Summary

Exercises

Chapter 20: Advanced Web Programming

Master Pages

Site Navigation

User Controls

Profiles

Web Parts

JavaScript

Summary

Exercises

Chapter 21: Web Services

Before Web Services

Where to Use Web Services

Web Services Architecture

Web Services and the .NET Framework

Creating a Simple ASP.NET Web Service

Testing the Web Service

Implementing a Windows Client

Calling the Service Asynchronously

Implementing an ASP.NET Client

Passing Data

Summary

Exercises

Chapter 22: Ajax Programming

Ajax Overview

Update Panel

Timer Control

Update Progress

Web Services

Extender Controls

Summary

Exercises

Chapter 23: Deploying Web Applications

Internet Information Services

IIS Configuration

Copying a Web Site

Publishing a Web Site

Windows Installer

Summary

Exercises

Part IV: Data Access

Chapter 24: File System Data

Streams

The Classes for Input and Output

Serialized Objects

Monitoring the File Structure

Summary

Exercises

Chapter 25: XML

XML Documents

Using XML in Your Application

Summary

Exercises

Chapter 26: Introduction to LINQ

LINQ Varieties

First LINQ Query

Using the LINQ Method Syntax and Lambda Expressions

Ordering Query Results

orderby Clause

Ordering Using Method Syntax

Querying a Large Data Set

Aggregate Operators

Querying Complex Objects

Projection: Creating New Objects in Queries

Projection: Method Syntax

Select Distinct Query

Any and All

Ordering By Multiple Levels

Multi-Level Ordering Method Syntax: ThenBy

Group Queries

Take and Skip

First and FirstOrDefault

Set Operators

Joins

Resources and Further Reading

Summary

Exercises

Chapter 27: LINQ to SQL

Object-Relational Mapping (ORM)

Installing SQL Server and the Northwind Sample Data

First LINQ to SQL Query

Navigating LINQ to SQL Relationships

Drilling Down Further with LINQ to SQL

Grouping, Ordering, and Other Advanced Queries in LINQ to SQL

Displaying Generated SQL

Data Binding with LINQ to SQL

Updating Bound Data with LINQ to SQL

Summary

Exercises

Chapter 28: ADO.NET and LINQ over DataSet

What Is ADO.NET?

Overview of ADO.NET Classes and Objects

Reading Data with the DataReader

Reading Data with the DataSet

Updating the Database

Accessing Multiple Tables in a DataSet

XML and ADO.NET

SQL Support in ADO.NET

Using LINQ over DataSet with ADO.NET

Summary

Exercises

Chapter 29: LINQ to XML

LINQ to XML Functional Constructors

Saving and Loading an XML Document

Working with XML Fragments

Generating XML from LINQ to SQL

How to Query an XML Document

Summary

Exercises

Part V: Additional Techniques

Chapter 30: Attributes

What Is an Attribute?

Reflection

Built-in Attributes

Custom Attributes

Summary

Chapter 31: XML Documentation

Adding XML Documentation

Making Use of XML Documentation

Summary

Exercises

Chapter 32: Networking

Networking Overview

Networking Programming Options

WebClient

WebRequest and WebResponse

TcpListener and TcpClient

Summary

Exercises

Chapter 33: Introduction to GDI+

Overview of Graphical Drawing

Drawing Lines Using the Pen Class

Drawing Shapes Using the Brush Class

Drawing Text Using the Font Class

Drawing Using Images

Advanced Capabilities of GDI+

Summary

Exercises

Chapter 34: Windows Presentation Foundation

What Is WPF?

Anatomy of a Basic WPF Application

WPF Fundamentals

Programming with WPF

Summary

Exercises

Chapter 35: Windows Communication Foundation

What Is WCF?

WCF Concepts

WCF Programming

Summary

Exercises

Chapter 36: Windows Workflow Foundation

Activities

The Workflow Runtime

Data Binding

Summary

Index

Advertisement

Beginning Microsoft® Visual C#® 2008

Published by

Wiley Publishing, Inc.

10475 Crosspoint Boulevard

Indianapolis, IN 46256

www.wiley.com

Copyright © 2008 by Wiley Publishing, Inc., Indianapolis, Indiana

Published simultaneously in Canada

ISBN: 978-0-470-19135-4

Library of Congress Cataloging-in-Publication Data is available from the publisher.

No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, scanning or otherwise, except as permitted under Sections 107 or 108 of the 1976 United States Copyright Act, without either the prior written permission of the Publisher, or authorization through payment of the appropriate per-copy fee to the Copyright Clearance Center, 222 Rosewood Drive, Danvers, MA 01923, (978) 750-8400, fax (978) 646-8600. Requests to the Publisher for permission should be addressed to the Legal Department, Wiley Publishing, Inc., 10475 Crosspoint Blvd., Indianapolis, IN 46256, (317) 572-3447, fax (317) 572-4355, or online at http://www.wiley.com/go/permissions.

Limit of Liability/Disclaimer of Warranty: The publisher and the author make no representations or warranties with respect to the accuracy or completeness of the contents of this work and specifically disclaim all warranties, including without limitation warranties of fitness for a particular purpose. No warranty may be created or extended by sales or promotional materials. The advice and strategies contained herein may not be suitable for every situation. This work is sold with the understanding that the publisher is not engaged in rendering legal, accounting, or other professional services. If professional assistance is required, the services of a competent professional person should be sought. Neither the publisher nor the author shall be liable for damages arising herefrom. The fact that an organization or Website is referred to in this work as a citation and/or a potential source of further information does not mean that the author or the publisher endorses the information the organization or Website may provide or recommendations it may make. Further, readers should be aware that Internet Websites listed in this work may have changed or disappeared between when this work was written and when it is read.

For general information on our other products and services please contact our Customer Care Department within the United States at (800) 762-2974, outside the United States at (317) 572-3993 or fax (317) 572-4002.

Trademarks: Wiley, the Wiley logo, Wrox, the Wrox logo, Wrox Programmer to Programmer, and related trade dress are trademarks or registered trademarks of John Wiley & Sons, Inc. and/or its affiliates, in the United States and other countries, and may not be used without written permission. Microsoft and Visual C# are registered trademarks of Microsoft Corporation in the United States and/or other countries. All other trademarks are the property of their respective owners. Wiley Publishing, Inc., is not associated with any product or vendor mentioned in this book.

Wiley also publishes its books in a variety of electronic formats. Some content that appears in print may not be available in electronic books.

Thanks to everyone at Wiley for helping me through this project and reigning in my strange British stylings, to assorted clients for giving me the time to write, and to Donna for keeping me sane and coping with my temperamental back. Thanks also to friends and family for being patient with my deadline-laden lifestyle.

—Karli Watson

To my wife, Beth, and our children, Nathaniel, Timothy, and Walter. Thanks for your support and encouragement. Also a big thank-you to my co-authors and the team at Wrox/Wiley.

—Jon Reid

To Mum and Dad: No words can express how much I love and miss you. Thanks for everything, you were brilliant.

—Morgan Skinner

About the Authors

Karli Watson is a freelance IT specialist, author, and developer. He is also a technical consultant for 3form Ltd. (www.3form.net) and Boost.net (www.boost.net), and an associate technologist with Content Master (www.contentmaster.com). For the most part, he immerses himself in .NET (in particular, C#) and has written numerous books in the field. He specializes in communicating complex ideas in a way that is accessible to anyone with a passion to learn, and spends much of his time playing with new technology to find new things to teach people.

During those rare times when he isn’t doing the above, Karli is probably wishing he were hurtling down a mountain on a snowboard or possibly trying to get his novel published. Either way, you’ll know him by his brightly colored clothes.

Christian Nagel is a software architect, trainer, and consultant, and an associate of Thinktecture (www.thinktecture.com), offering training and coaching based on Microsoft .NET technologies. His achievements in the developer community have earned him a position as Microsoft Regional Director and MVP for ASP.NET. He enjoys an excellent reputation as an author of several .NET books, such as Professional C#, Pro .NET Network Programming, and Enterprise Services with the .NET Frameworks, and he speaks regularly at international industry conferences.

Christian has more than 15 years of experience as a developer and software architect. He started his computing career on PDP 11 and VAX/VMS, covering a variety of languages and platforms. Since 2000, he has been working with .NET and C#, developing and architecting distributed solutions. He can be reached at www.christiannagel.com.

Jacob Hammer Pedersen is a systems developer at Fujitsu Service, Denmark. He’s been programming the PC since the early 1990s using various languages, including Pascal, Visual Basic, C/C++, and C#. Jacob has co-authored a number of .NET books and works with a wide variety of Microsoft technologies, ranging from SQL Server to Office extensibility. A Danish citizen, he works and lives in Aarhus, Denmark.

Jon D. Reid is the director of systems engineering at Indigo Biosystems, Inc. (www.indigobio.com), an independent software vendor for the life sciences, where he develops in C# for the Microsoft environment. He has co-authored many .NET books, including Beginning Visual C# 2005, Beginning C# Databases: From Novice to Professional, Pro Visual Studio .NET, ADO.NET Programmer’s Reference, and Professional SQL Server 2000 XML.

Morgan Skinner started programming at school in 1980 and has been hooked on computing ever since. He now works for Microsoft as an application development consultant where he helps customers with their architecture, design, coding, and testing. He’s been working with .NET since the PDC release in 2000, and has authored several MSDN articles and co-authored a couple of books on .NET. In his spare time he relaxes by fighting weeds on his allotment. You can reach Morgan at www.morganskinner.com.

Eric White is an independent software consultant with more than 20 years of experience in building management information systems and accounting systems. When he isn’t hunched over a screen programming in C#, he is most likely to be found with an ice axe in hand, climbing some mountain.

Credits

Acquisitions Editor

Katie Mohr

Development Editor

Maryann Steinhart

Technical Editor

Douglas Parsons

Production Editor

William A. Barton

Copy Editor

Luann Rouff

Editorial Manager

Mary Beth Wakefield

Production Manager

Tim Tate

Vice President and Executive Group Publisher

Richard Swadley

Vice President and Executive Publisher

Joseph B. Wikert

Project Coordinator, Cover

Lynsey Stanford

Proofreaders

Jeremy Bagai,

Josh Chase,

David Fine,

Reed Kellman,

Scott Klemp,

Josh Peterson,

Todd Spencer,

Word One

Indexer

Robert Swanson

Introduction

C# is a relatively new language that was unveiled to the world when Microsoft announced the first version of its .NET Framework in July 2000. Since then its popularity has rocketed, and it has arguably become the language of choice for both Windows and Web developers who use the .NET Framework. Part of the appeal of C# comes from its clear syntax, which derives from C/C++ but simplifies some things that have previously discouraged some programmers. Despite this simplification, C# has retained the power of C++, and there is now no reason not to move into C#. The language is not difficult, making it a great one to learn elementary programming techniques with. This ease of learning, combined with the capabilities of the .NET Framework, make C# an excellent way to start your programming career.

The latest release of C#, C# 3.0, which is included with version 3.5 of the .NET Framework, builds on the existing successes and adds even more attractive features. Some of these, again, have their roots in C++ — at least superficially — but some are entirely new. The latest releases of Visual Studio and the Express line of development tools also bring many tweaks and improvements to make your life easier and dramatically increase your productivity.

This book is intended to teach you about all aspects of C# programming, from the language itself, through Windows and Web programming, to making use of data sources, and finally to some advanced techniques such as graphics programming. You’ll also learn about the capabilities of Visual C# Express 2008, Visual Web Developer Express 2008, and Visual Studio 2008, and all the ways that these products can aid your application development. The book is written in a friendly, mentor-style fashion, with each chapter building on previous ones, and every effort is made to ease you into advanced techniques painlessly. At no point do technical terms appear from nowhere to discourage you from continuing; every concept is introduced and discussed as required. Technical jargon is kept to a minimum, but where it is necessary, it too is properly defined and laid out in context.

The authors of this book are all experts in their fields, and are enthusiastic in their passion for both the C# language and the .NET Framework. Nowhere will you find a group of people better qualified to take you under their collective wing and nurture your understanding of C# from first principles to advanced techniques. Along with the fundamental knowledge it provides, this book is packed full of helpful hints, tips, exercises, and full-fledged example code (available for download at p2p.wrox.com) that you will find yourself using time and again as your career progresses.

We pass this knowledge on without begrudging it, and hope that you will be able to use it to become the best programmer you can be. Good luck, and all the best!

Who This Book Is For

This book is for everyone who wants to learn how to program in C# using the .NET Framework. The early chapters cover the language itself, assuming no prior programming experience. If you have programmed in other languages before, then much of the material in these chapters will be familiar. Many aspects of C# syntax are shared with other languages, and many structures are common to practically all programming languages (such as looping and branching structures). However, even if you are an experienced programmer, you will benefit from looking through these chapters to learn the specifics of how these techniques apply to C#.

If you are new to programming, then you should start from the beginning. If you are new to the .NET Framework but know how to program, then you should read Chapter 1 and then skim through the next few chapters before beginning to apply the C# concepts you have learned. If you know how to program but haven’t encountered an object-oriented programming language before, then read the chapters from Chapter 8 onward.

Alternatively, if you already know the C# language, then you may wish to concentrate on the chapters dealing with the most recent .NET Framework and C# language developments, specifically the chapters on collections, generics, and C# 3.0 language additions (Chapters 11 to 14), or skip the first section of the book completely and start with Chapter 15.

The chapters in this book are written with a dual purpose in mind: They can be read sequentially to provide a complete tutorial in the C# language, and they can be dipped into as required as reference material.

In addition to the core material, each chapter also includes a selection of exercises that you can work through to ensure that you have understood the material. The exercises range from simple multiple choice or true/false questions to more involved questions that require you to modify or build applications. The answers to all the exercises are provided as a download from the book’s Web page at www.wrox.com.

How This Book Is Structured

This book is divided into six sections:

Introduction: What this book is about and for whom it was written.

The C# Language: Covers all aspects of the C# language, from the fundamentals to object-oriented techniques.

Windows Programming: How to write Windows applications in C#, and how to deploy them.

Web Programming: Web application development, Web services, and Web application deployment.

Data Access: Using data in your applications, including data stored in files on your hard disk, data stored in XML format, and data in databases.

Additional Techniques: This section examines some extra ways of using C# and the .NET Framework, including attributes, XML documentation, networking, and graphics programming with GDI+. It also looks at WPF, WCF, and WF — technologies introduced with .NET 3.0 and enhanced for .NET 3.5.

The following sections describe the chapters in the five major sections of this book.

The C# Language (Chapters 1–14)

Chapter 1 introduces you to C# and how it fits into the .NET landscape. You’ll learn the fundamentals of programming in this environment, and how Visual C# Express (VCE) and Visual Studio (VS) fit in.

Chapter 2 starts you off with writing C# applications. You’ll look at the syntax of C# and put the language to use with sample command-line and Windows applications. These examples demonstrate just how quick and easy it can be to get up and running, and along the way you’ll be introduced to the VCE and VS development environments and the basic windows and tools that you’ll be using throughout the book.

Next, you’ll learn more about the basics of the C# language. In Chapter 3, you’ll learn what variables are and how to manipulate them. You’ll enhance the structure of your applications with flow control (looping and branching) in Chapter 4, and learn some more advanced variable types such as arrays in Chapter 5. In Chapter 6, you’ll start to encapsulate your code in the form of functions, which make it much easier to perform repetitive operations, and make your code much more readable.

By the start of Chapter 7 you’ll have a handle on the fundamentals of the C# language, and will focus on debugging your applications. This involves looking at outputting trace information as your applications are executed, and at how VS can be used to trap errors and lead you to solutions for them with its powerful debugging environment.

From Chapter 8 onward you’ll learn about object-oriented programming (OOP), starting with a look at what this term means, and an answer to the eternal question, “What is an object?” OOP can seem quite difficult at first. The whole of Chapter 8 is devoted to demystifying it and explaining what makes it so great, and you won’t actually deal with much C# code until the very end of the chapter.

Everything changes in Chapter 9, when you put theory into practice and start using OOP in your C# applications. This is where the true power of C# lies. You’ll begin by looking at how to define classes and interfaces, and then move on to class members (including fields, properties, and methods) in Chapter 10. At the end of that chapter, you’ll start to assemble a card game application, which is developed over several chapters and helps to illustrate OOP.

Once you’ve learned how OOP works in C#, you’ll move on in Chapter 11 to look at common OOP scenarios, including dealing with collections of objects, and comparing and converting objects. Chapter 12 takes a look at a new and very useful feature of C# in .NET 2.0, generics, which enable you to create very flexible classes. Chapter 13 continues the discussion of the C# language and OOP with some additional techniques, and notable events, which become very important in, for example, Windows programming. Finally, Chapter 14 focuses on C# language features that were introduced with version 3.0 of the language.

Windows Programming (Chapters 15–18)

Chapter 15 starts by introducing you to what is meant by Windows programming, and looks at how this is achieved in VCE and VS. As before, you’ll start with the basics and build up your knowledge over the chapter. Then, in Chapter 16, you will see how you can use the wealth of controls supplied by the .NET Framework in your applications. You’ll quickly understand how .NET enables you to build Windows applications in a graphical way, and assemble advanced applications with the minimum of effort and time.

Chapter 17 looks at some commonly used features that can add specialized features with ease, such as file management, printing, and so on. Chapter 18 then discusses deploying your applications, including making installation programs to enable your users to get up and running with your applications as soon as possible.

Web Programming (Chapters 19–23)

This section is structured in a similar way to the Windows programming section. Chapter 19 describes the controls that make up the simplest of Web applications, and how you can fit them together and make them perform tasks using ASP.NET. Chapter 20 builds on this and introduces more advanced techniques, versatile controls, and state management in the context of the Web, as well as conforming to Web standards.

Chapter 21 is an excursion into the wonderful world of Web services, which provide programmatic access to information and capabilities across the Internet. Web services enable you to expose complex data and functionality to Web and Windows applications in a platform-independent way. This chapter discusses how to use and create Web services, and the additional tools that .NET provides, including security.

Chapter 22 looks at Ajax programming, which is a way to add dynamic, client-side functionality to Web applications. Version 3.5 of the .NET Framework provides Ajax functionality through ASP.NET Ajax, and this chapter explains how to use it.

Chapter 23 examines the deployment of Web applications and services — in particular, the new features of VS and VWD that enable you to publish applications to the Web with the click of a button.

Data Access (Chapters 24–29)

Chapter 24 looks at how your applications can save data to and retrieve it from a disk, both as simple text files and as more complex representations of data. You’ll also see how to compress data, how to work with legacy data such as comma-separated value (CSV) files, and how to monitor and act on file system changes.

In Chapter 25 you’ll learn about what is fast becoming the de facto standard for data exchange — namely, XML. You will have touched on XML several times in preceding chapters, but this chapter lays out the ground rules and shows you what all the excitement is about.

The remainder of this section looks at LINQ, which is a query language built in to the latest versions of the .NET Framework. Chapter 26 provides a general introduction to LINQ, and then you will use LINQ to access database data in Chapter 27. In Chapter 28, you will see how LINQ can be used alongside the older ADO.NET data access technology. Finally, in Chapter 29 you will learn how to use LINQ with XML data.

Additional Techniques (Chapters 30–36)

The last section of the book looks at a wide variety of additional C# and .NET subjects. In Chapter 30 you’ll look at attributes, a powerful way to both include additional information about types in assemblies and add functionality that would otherwise be difficult to implement.

Chapter 31 deals with XML documentation and how you can document your applications at the source code level. You’ll see how to add this information and how to use and extract it. You’ll take this to the point where you can generate expansive, MSDN-style documentation from your code.

Next, you’ll look at networking in Chapter 32, and how your applications can communicate with each other and with other services across various types of networks. Chapter 33 provides a bit of light relief from many of the involved techniques you’ll have seen earlier in the book, as it covers the subject of graphics programming with GDI+. You’ll learn how to manipulate graphics and style your applications, opening the door to a vast array of C# applications and having a bit of fun along the way.

Finally, you will look at some exciting new technologies that have emerged with the latest .NET Framework release. In Chapter 34, you will get to play with Windows Presentation Foundation (WPF) and see how it promises enormous changes to both Windows and Web development. Chapter 35 looks at Windows Communication Foundation (WCF), which extends and enhances the concept of Web services to an enterprise-level communication technology. The last chapter of the book, Chapter 36, looks at Windows Workflow Foundation (WF). WF enables you to implement workflow functionality in your applications, meaning that you can define operations that are performed in a specific order controlled by external interactions, which is very useful for many types of applications.

What You Need to Use This Book

The code and descriptions of C# and the .NET Framework in this book apply to .NET 3.5. You don’t need anything other than the framework to understand this aspect of the book, but many of the examples require a development tool. This book uses Visual C# Express 2008 as its primary development tool, although some chapters use Visual Web Developer Express 2008. In addition, some functionality is only available in Visual Studio 2008, and where appropriate this is noted.

Conventions

A number of conventions are used throughout the book to help you get the most from the text and keep track of what’s happening.

Try It Out

The Try It Out is an exercise you should work through, following the text in the book.

1. They usually consist of a set of steps.

2. Each step has a number.

3. Follow the steps through with your copy of the database.

How It Works

After most of the Try It Out sections, the code you have typed is explained in detail.

Boxes like this one hold important, not-to-be forgotten information that is directly relevant to the surrounding text.

Tips, hints, tricks, and asides to the current discussion are offset and placed in italics like this.

As for styles in the text:

New terms and important words are italicized when introduced.

Keyboard strokes look like this: Ctrl+A.

Filenames, URLs, and code within the text look like so: persistence.properties.

Code is presented in two different ways:

Code examples nearly always look like this.

Gray highlighting is used to show where new code is added to existing code, or to point out a specific section of code that's being explained in the text.

Source Code

As you work through the examples in this book, you may choose either to type in all the code manually or to use the source code files that accompany the book. All of the source code used in this book is available for download at www.wrox.com. Once at the site, simply locate the book’s title (either by using the Search box or by using one of the title lists) and click the Download Code link on the book’s detail page to obtain all the source code for the book.

Because many books have similar titles, you may find it easiest to search by ISBN; for this book, the ISBN is 978-0-470-19135-4

Once you download the code, just decompress it with your favorite compression tool. Alternatively, you can go to the main Wrox code download page at www.wrox.com/dynamic/books/download.aspx to see the code available for this book and all other Wrox books.

Errata

Every effort is made to ensure that there are no errors in the text or in the code. However, no one is perfect, and mistakes do occur. If you find an error such as a spelling mistake or a faulty piece of code in one of our books, we would be grateful for your feedback. By sending in errata, you may save another reader hours of frustration, and at the same time you will be helping us provide even higher quality information.

To find the errata page for this book, go to www.wrox.com and locate the title using the Search box or one of the title lists. Then, on the book details page, click the Book Errata link. On this page you can view all errata that have been submitted for this book and posted by Wrox editors. A complete book list, including links to each book’s errata, is also available at www.wrox.com/misc-pages/booklist.shtml.

If you don’t spot “your” error on the Book Errata page, go to www.wrox.com/contact/techsupport.shtml and complete the form there to send us the error you have found. We’ll check the information and, if appropriate, post a message to the book’s errata page and fix the problem in subsequent editions of the book.

p2p.wrox.com

For author and peer discussion, join the P2P forums at p2p.wrox.com. The forums are a Web-based system for you to post messages relating to Wrox books and related technologies and interact with other readers and technology users. The forums offer a subscription feature to e-mail you topics of interest of your choosing when new posts are made to the forums. Wrox authors, editors, other industry experts, and your fellow readers are present on these forums.

At http://p2p.wrox.com you will find a number of different forums that can help you not only as you read this book, but also as you develop your own applications. To join the forums, just follow these steps:

1. Go to p2p.wrox.com and click the Register link.

2. Read the terms of use and click Agree.

3. Complete the required information to join as well as any optional information you want to provide and click Submit.

4. You will receive an e-mail with information describing how to verify your account and complete the joining process.

You can read messages in the forums without joining P2P, but to post your own messages you must join.

Once you join, you can post new messages and respond to messages other users post. You can read messages at any time on the Web. If you would like to have new messages from a particular forum e-mailed to you, click the Subscribe to this Forum icon by the forum name in the forum listing.

For more information about how to use the Wrox P2P, be sure to read the P2P FAQs for answers to questions about how the forum software works as well as many common questions specific to P2P and Wrox books. To read the FAQs, click the FAQ link on any P2P page.

Part I

The C# Language

Chapter 1: Introducing C#

Chapter 2: Writing a C# Program

Chapter 3: Variables and Expressions

Chapter 4: Flow Control

Chapter 5: More About Variables

Chapter 6: Functions

Chapter 7: Debugging and Error Handling

Chapter 8: Introduction to Object-Oriented Programming

Chapter 9: Defining Classes

Chapter 10: Defining Class Members

Chapter 11: Collections, Comparisons, and Conversions

Chapter 12: Generics

Chapter 13: Additional OOP Techniques

Chapter 14: C# 3.0 Language Enhancements

Chapter 1

Introducing C#

Welcome to the first chapter of the first section of this book. This section will provide you with the basic knowledge you need to get up and running with C#. This chapter provides an overview of C# and the .NET Framework, including what these technologies are, the motivation for using them, and how they relate to each other.

First is a general discussion of the .NET Framework. This technology contains many concepts that are tricky to come to grips with initially. This means that the discussion, by necessity, covers many new concepts in a short amount of space. However, a quick look at the basics is essential to understanding how to program in C#. Later in the book you will revisit many of the topics covered here, exploring them in more detail.

After that general introduction, the chapter provides a basic description of C# itself, including its origins and similarities to C++. Finally, you look at the primary tools used throughout this book: Visual Studio 2008 (VS) and Visual C# 2008 Express Edition (VCE).

What Is the .NET Framework?

The .NET Framework is a new and revolutionary platform created by Microsoft for developing applications. The most interesting thing about this statement is how vague it is — but there are good reasons for this. For a start, note that it doesn’t “develop applications on the Windows operating system.” Although the Microsoft release of the .NET Framework runs on the Windows operating system, it is fast becoming possible to find alternative versions that will work on other systems. One example of this is Mono, an open-source version of the .NET Framework (including a C# compiler) that runs on several operating systems, including various flavors of Linux and Mac OS. More such projects are in the pipeline and may be available by the time you read this. In addition, you can use the Microsoft .NET Compact Framework (essentially a subset of the full .NET Framework) on personal digital assistant (PDA) class devices and even some smartphones. One of the key motivations behind the .NET Framework is its intended use as a means of integrating disparate operating systems.

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!

Lesen Sie weiter in der vollständigen Ausgabe!