John has also authored Microsoft Windows Communication Foundation Step by Step and Microsoft Visual J# Core Reference, both published by Microsoft Press..[r]
(1)(2)PUBLISHED BY Microsoft Press
A Division of Microsoft Corporation One Microsoft Way
Redmond, Washington 98052-6399 Copyright © 2008 by John Sharp
All rights reserved No part of the contents of this book may be reproduced or transmitted in any form or by any means without the written permission of the publisher
Library of Congress Control Number: 2007939305
Printed and bound in the United States of America
1 QWT
Distributed in Canada by H.B Fenn and Company Ltd
A CIP catalogue record for this book is available from the British Library
Microsoft Press books are available through booksellers and distributors worldwide For further infor-mation about international editions, contact your local Microsoft Corporation office or contact Microsoft Press International directly at fax (425) 936-7329 Visit our Web site at www.microsoft.com/mspress Send comments to mspinput@microsoft.com
Microsoft, Microsoft Press, MSDN, SQL Server, Excel, Intellisense, Internet Explorer, Jscript, Silverlight, Visual Basic, Visual C#, Visual Studio, Win32, Windows, Windows Server, and Windows Vista are either registered trademarks or trademarks of Microsoft Corporation in the United States and/or other countries Other product and company names mentioned herein may be the trademarks of their respective owners
The example companies, organizations, products, domain names, e-mail addresses, logos, people, places, and events depicted herein are fictitious No association with any real company, organization, product, domain name, e-mail address, logo, person, place, or event is intended or should be inferred
7KLVERRNH[SUHVVHVWKHDXWKRU¶VYLHZVDQGRSLQLRQV7KHLQIRUPDWLRQFRQWDLQHGLQWKLVERRNLVSURYLGHG without any express, statutory, or implied warranties Neither the authors, Microsoft Corporation, nor its resellers, or distributors will be held liable for any damages caused or alleged to be caused either directly or indirectly by this book
Acquisitions Editor:Ben Ryan
Developmental and Project Editor:Lynn Finnel Editorial Production:Waypoint Press
Technical Reviewer:Kurt Meyer; Technical Review services provided by Content Master, a member of CM Group, Ltd
(3)iii
Contents at a Glance
Part I Introducing Microsoft Visual C# and
Microsoft Visual Studio 2008
1 Welcome to C# 3
2 Working with Variables, Operators, and Expressions 29
3 Writing Methods and Applying Scope 49
4 Using Decision Statements 67
5 Using Compound Assignment and Iteration Statements 85
6 Managing Errors and Exceptions 103
Part II Understanding the C# Language 7 Creating and Managing Classes and Objects 123
8 Understanding Values and References 145
9 Creating Value Types with Enumerations and Structures 167
10 Using Arrays and Collections 185
11 Understanding Parameter Arrays 207
12 Working with Inheritance 217
13 Creating Interfaces and Defi ning Abstract Classes 239
14 Using Garbage Collection and Resource Management 257
Part III Creating Components 15 Implementing Properties to Access Fields 275
16 Using Indexers 295
17 Interrupting Program Flow and Handling Events 311
18 Introducing Generics 333
19 Enumerating Collections 355
20 Querying In-Memory Data by Using Query Expressions 371
(4)iv Contents at a Glance
Part IV Working with Windows Applications
22 Introducing Windows Presentation Foundation 415 23 Working with Menus and Dialog Boxes 451 24 Performing Validation 473
Part V Managing Data
25 Querying Information in a Database 499 26 Displaying and Editing Data by Using Data Binding 529
Part VI Building Web Applications
27 Introducing ASP.NET 559 28 Understanding Web Forms Validation Controls 587 29 Protecting a Web Site and Accessing Data with
(5)v
www.microsoft.com/learning/booksurvey
Microsoft is interested in hearing your feedback so we can continually improve our books and learning resources for you To participate in a brief online survey, please visit:
What you think of this book? We want to hear from you!
Table of Contents
Acknowledgments xvii
Introduction xix
Part I Introducing Microsoft Visual C# and Microsoft Visual Studio 2008 1 Welcome to C# 3
Beginning Programming with the Visual Studio 2008 Environment
Writing Your First Program
Using Namespaces 14
Creating a Graphical Application 17
Chapter Quick Reference 28
2 Working with Variables, Operators, and Expressions 29
Understanding Statements 29
Using Identifi ers 30
Identifying Keywords 30
Using Variables 31
Naming Variables 32
Declaring Variables 32
Working with Primitive Data Types 33
Displaying Primitive Data Type Values 34
Using Arithmetic Operators 38
Operators and Types 39
Examining Arithmetic Operators 40
Controlling Precedence 43
Using Associativity to Evaluate Expressions 44
(6)644 Part VI Building Web Applications
Chapter 30 Quick Reference
To Do this
Create a Web service Use the WCF Service template Defi ne a service contract that specifi es the Web methods exposed by the Web service by creating an interface with the ServiceContract attribute Tag each method with the OperationContract attribute Create a class that implements this interface
Display the description of a Web service
Right-click the svc fi le in Solution Explorer, and click View in Browser Internet Explorer runs, moves to the Web service URL, and displays a page describing how to create a client application that can access the Web service Click the WSDL link to display the WSDL description of the Web service
Pass complex data as Web method parameters and return values
Defi ne a class to hold the data and tag it with the DataContract attribute Ensure that each item of data is accessible either as a public fi eld or through a public property that provides get and set access Ensure that the class has a default constructor (which might be empty)
Add a service reference to a client application and create a proxy class
On the Project menu, click Add Service Reference Type the URL of the Web service in the Address text box at the top of the dialog box, and then click Go Specify the namespace for the proxy class, and then click OK
(7)John Sharp