John has also authored Microsoft Windows Communication Foundation Step by Step and Microsoft Visual J# Core Reference, both published by Microsoft Press..[r]
Trang 2PUBLISHED 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 2 3 4 5 6 7 8 9 QWT 2 1 0 9 8 7
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.
Body Part No X14-22686
Trang 3iii
Contents at a Glance
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
21 Operator Overloading 395
Trang 4iv Contents at a Glance
22 Introducing Windows Presentation Foundation 415
23 Working with Menus and Dialog Boxes 451
24 Performing Validation 473
25 Querying Information in a Database 499
26 Displaying and Editing Data by Using Data Binding 529
27 Introducing ASP.NET 559
28 Understanding Web Forms Validation Controls 587
29 Protecting a Web Site and Accessing Data with
Web Forms 597
30 Creating and Using a Web Service 623 Index 645
Trang 5v 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 do 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 3
Writing Your First Program 8
Using Namespaces 14
Creating a Graphical Application 17
Chapter 1 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
Associativity and the Assignment Operator 45
Trang 6644 Part VI Building Web Applications
Chapter 30 Quick Reference
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.
Invoke a Web method Create an instance of the proxy class Call the Web method using the
proxy class.
Trang 7John Sharp
John Sharp is a Principal Technologist at Content Master (www.contentmaster.com), part
of CM Group, a technical authoring company in the United Kingdom He researches and develops technical content for training courses, seminars, and white papers John is deeply involved with Microsoft NET Framework application development and interoperability He has written papers and courses, built tutorials, and delivered conference presentations cover-ing distributed systems and Web services, application migration and interoperability between Microsoft Windows/.NET Framework and UNIX/Linux/Java, as well as development using the C# and J# languages John has also authored Microsoft Windows Communication Foundation Step by Step and Microsoft Visual J# Core Reference, both published by Microsoft Press.