Tài liệu học lập trình C# cho sinh viên và mọi người.
ptg ptg C# 2010 FOR PROGRAMMERS FOURTH EDITION DEITEL ® DEVELOPER SERIES ptg Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and the publisher was aware of a trademark claim, the designations have been printed with initial capital letters or in all capitals. The authors and publisher have taken care in the preparation of this book, but make no expressed or implied warranty of any kind and assume no responsibility for errors or omissions. No liability is assumed for incidental or consequential damages in connection with or arising out of the use of the information or programs contained herein. The publisher offers excellent discounts on this book when ordered in quantity for bulk purchases or special sales, which may include electronic versions and/or custom covers and content particular to your business, training goals, marketing focus, and branding interests. For more information, please contact: U. S. Corporate and Government Sales (800) 382-3419 corpsales@pearsontechgroup.com For sales outside the U. S., please contact: International Sales international@pearsoned.com Visit us on the Web: informit.com/ph Library of Congress Cataloging-in-Publication Data On file © 2011 Pearson Education, Inc. All rights reserved. Printed in the United States of America. This publication is protected by copyright, and permission must be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or transmis- sion in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information re- garding permissions, write to: Pearson Education, Inc. Rights and Contracts Department 501 Boylston Street, Suite 900 Boston, MA 02116 Fax (617) 671-3447 ISBN-13: 978-0-13261820-5 ISBN-10: 0-13-261820-6 Text printed in the United States on recycled paper at RR Donnelley in Crawfordsville, Indiana. First printing, October 2010 Download at www.wowebook.com ptg C# 2010 FOR PROGRAMMERS FOURTH EDITION D EITEL ® DEVELOPER SERIES Paul Deitel Deitel & Associates, Inc. Harvey Deitel Deitel & Associates, Inc. Upper Saddle River, NJ • Boston • Indianapolis • San Francisco New York • Toronto • Montreal • London • Munich • Paris • Madrid Capetown • Sydney • Tokyo • Singapore • Mexico City ptg Tr a d e m a r k s DEITEL, the double-thumbs-up bug and Dive Into are registered trademarks of Deitel & Associates, Inc. Microsoft, Windows, Silverlight, SQL Server, Visual Studio, Visual Basic, Visual C# and Visual Web Developer are either registered trademarks or trademarks of Microsoft Corporation in the United States and/or other countries. Download at www.wowebook.com ptg To Anders Hejlsberg, Chief Designer of Microsoft’s C# Programming Language Paul and Harvey Deitel Download at www.wowebook.com ptg This page intentionally left blank Download at www.wowebook.com ptg Preface xxi Before You Begin xxxiv 1 Introduction 1 1.1 Introduction 2 1.2 Microsoft’s Windows ® Operating System 2 1.3 C, C++, Objective-C and Java 3 1.4 C# 3 1.5 Extensible Markup Language (XML) 4 1.6 Introduction to Microsoft .NET 4 1.7 The .NET Framework and the Common Language Runtime 4 1.8 Test-Driving the Advanced Painter Application 5 1.9 Introduction to Object Technology 8 1.10 Wrap-Up 10 2 Dive Into® Visual C# 2010 Express 11 2.1 Introduction 12 2.2 Overview of the Visual Studio 2010 IDE 12 2.3 Menu Bar and Toolbar 17 2.4 Navigating the Visual Studio IDE 19 2.4.1 Solution Explorer 21 2.4.2 Toolbox 22 2.4.3 Properties Window 23 2.5 Using Help 24 2.6 Using Visual Programming to Create a Simple Program that Displays Text and an Image 27 2.7 Wrap-Up 38 2.8 Web Resources 39 3 Introduction to C# Applications 40 3.1 Introduction 41 3.2 A Simple C# Application: Displaying a Line of Text 41 Contents Download at www.wowebook.com ptg viii Contents 3.3 Creating a Simple Application in Visual C# Express 46 3.4 Modifying Your Simple C# Application 53 3.5 Formatting Text with Console.Write and Console.WriteLine 56 3.6 Another C# Application: Adding Integers 57 3.7 Arithmetic 59 3.8 Decision Making: Equality and Relational Operators 61 3.9 Wrap-Up 65 4 Introduction to Classes and Objects 66 4.1 Introduction 67 4.2 Classes, Objects, Methods, Properties and Instance Variables 67 4.3 Declaring a Class with a Method and Instantiating an Object of a Class 68 4.4 Declaring a Method with a Parameter 72 4.5 Instance Variables and Properties 75 4.6 UML Class Diagram with a Property 80 4.7 Software Engineering with Properties and set and get Accessors 81 4.8 Auto-Implemented Properties 82 4.9 Value Types vs. Reference Types 83 4.10 Initializing Objects with Constructors 84 4.11 Floating-Point Numbers and Type decimal 87 4.12 Wrap-Up 93 5 Control Statements: Part 1 94 5.1 Introduction 95 5.2 Control Structures 95 5.3 if Single-Selection Statement 97 5.4 if…else Double-Selection Statement 98 5.5 while Repetition Statement 102 5.6 Counter-Controlled Repetition 103 5.7 Sentinel-Controlled Repetition 107 5.8 Nested Control Statements 112 5.9 Compound Assignment Operators 115 5.10 Increment and Decrement Operators 115 5.11 Simple Types 118 5.12 Wrap-Up 119 6 Control Statements: Part 2 120 6.1 Introduction 121 6.2 Essentials of Counter-Controlled Repetition 121 6.3 for Repetition Statement 122 6.4 Examples Using the for Statement 127 6.5 do…while Repetition Statement 131 6.6 switch Multiple-Selection Statement 132 Download at www.wowebook.com ptg Contents ix 6.7 break and continue Statements 140 6.8 Logical Operators 142 6.9 Wrap-Up 148 7 Methods: A Deeper Look 149 7.1 Introduction 150 7.2 Packaging Code in C# 150 7.3 static Methods, static Variables and Class Math 151 7.4 Declaring Methods with Multiple Parameters 154 7.5 Notes on Declaring and Using Methods 157 7.6 Method-Call Stack and Activation Records 158 7.7 Argument Promotion and Casting 159 7.8 The .NET Framework Class Library 160 7.9 Case Study: Random-Number Generation 162 7.9.1 Scaling and Shifting Random Numbers 166 7.9.2 Random-Number Repeatability for Testing and Debugging 166 7.10 Case Study: A Game of Chance (Introducing Enumerations) 167 7.11 Scope of Declarations 172 7.12 Method Overloading 174 7.13 Optional Parameters 177 7.14 Named Parameters 178 7.15 Recursion 179 7.16 Passing Arguments: Pass-by-Value vs. Pass-by-Reference 182 7.17 Wrap-Up 185 8 Arrays 187 8.1 Introduction 188 8.2 Arrays 188 8.3 Declaring and Creating Arrays 189 8.4 Examples Using Arrays 190 8.5 Case Study: Card Shuffling and Dealing Simulation 199 8.6 foreach Statement 203 8.7 Passing Arrays and Array Elements to Methods 205 8.8 Passing Arrays by Value and by Reference 208 8.9 Case Study: Class GradeBook Using an Array to Store Grades 212 8.10 Multidimensional Arrays 217 8.11 Case Study: GradeBook Using a Rectangular Array 222 8.12 Variable-Length Argument Lists 227 8.13 Using Command-Line Arguments 229 8.14 Wrap-Up 231 9 Introduction to LINQ and the List Collection 232 9.1 Introduction 233 Download at www.wowebook.com [...]... conform to industry norms Using Fonts for Emphasis We place the key terms and the index’s page reference for each defining occurrence in bold text for easier reference On-screen components are emphasized in the bold Helvetica font (e.g., the File menu) and C# program text in the Lucida font (e.g., int x = 5) Web Access All of the source-code examples for C# 2010 for Programmers, 4/e are available for. .. III LiveLessons Video Product xxxi C# 2010 Fundamentals: Parts I, II and III LiveLessons Video Product Our C# 2010 Fundamentals: Parts I, II and III LiveLessons Camtasia-based video training product shows you what you need to know to start building robust, powerful software with C# 2010 and NET 4 It includes 20+ hours of expert training synchronized to C# 2010 for Programmers, 4/e Check out our growing... bold Helvetica font (for example, Properties window) and to emphasize program text in a sans-serif Lucida font (for example, bool x = true) A Note Regarding Software for the Book This textbook includes a DVD which contains the Microsoft® Visual Studio® 2010 Express Edition integrated development environments for Visual C# 2010, Visual Basic 2010, Visual C++ 2010, Visual Web Developer 2010 and SQL Server... Software for the Book We use Microsoft Visual Studio 2010 development tools, including the free Visual C# 2010 Express Edition, Visual Web Developer 2010 Express Edition and SQL Server 2008 Express Edition The Express Editions provide rich functionality and can be used to build robust NET applications They are appropriate for professionals who do not have access to a complete version of Visual Studio 2010. .. and related Resource Centers at www.deitel.com/ResourceCenters.html New and Updated Features Here are some key features of C# 2010 for Programmers, 4/e: • LINQ LINQ provides a uniform syntax for querying data Strong typing enables Visual Studio to provide IntelliSense support for LINQ operations and results LINQ can be used on different types of data sources, including collections and files (LINQ to... fragments no longer, only connect.” —Edgar Morgan Forster Welcome to Visual C# 2010, C# 4 and the world of Microsoft® Windows® and Internet and web programming with Microsoft’s NET 4 Framework! This book presents leadingedge computing technologies for professional software developers We believe the book will give you an informative, challenging and entertaining C# educational experience We use the Deitel... list of LiveLessons video products: • C# 2010 Fundamentals I, II, and III • C# 2008 Fundamentals I and II • Java Fundamentals I and II • C++ Fundamentals I and II • iPhone App-Development Fundamentals I and II • JavaScript Fundamentals I and II • Visual Basic 2010 Fundamentals I and II • C Fundamentals I and II • Android Fundamentals I and II For additional information about Deitel LiveLessons video... Osenkov (Visual C#) and Alex Turner (Visual C# Compiler Program Manager) Other Industry Reviewers Rizwan Ahmed a.k.a RizwanSharp (C# MVP, Sr Software Engineer, TEO), José Alarcón-Aguín (ASP.NET MVP, Krasis.com), Mostafa Arafa (C# MVP, Agility Logistics), Bonnie Berent (Microsoft C# MVP), Peter Bromberg (Senior Architect Merrill Lynch and C# MVP), Adam Calderon (C# MVP, InterKnowlogy), Stochio Goutsev (Independent... www.deitel.com/books/csharpfp4/ We hope you enjoy reading C# 2010 for Programmers, 4/e, as much as we enjoyed writing it! Paul J Deitel Dr Harvey M Deitel About the Authors Paul J Deitel, CEO and Chief Technical Officer of Deitel & Associates, Inc., is a graduate of MIT, where he studied Information Technology Through Deitel & Associates, Inc., he has delivered C#, Visual Basic, Java, C++, C and Internet programming... introduction to XML in Chapter 24 Fig 2 | Chapter dependency chart for the advanced-topic chapters Presentation Features C# 2010 for Programmers, 4/e, contains a rich collection of examples We concentrate on effective software engineering principles and stress program clarity in the context of hundreds of complete, working programs Syntax Shading For readability, we syntax shade the code, similar to the way