Tài liệu học lập trình C# cho sinh viên và mọi người.
OFFICIAL MICROSOFT LEARNING PRODUCT 10266A Programming in C# with Microsoft® Visual Studio® 2010 Be sure to access the extended learning content on your Course Companion CD enclosed on the back cover of the book. ii Programming in C# with Microsoft® Visual Studio® 2010 Information in this document, including URL and other Internet Web site references, is subject to change without notice. Unless otherwise noted, the example companies, organizations, products, domain names, e-mail addresses, logos, people, places, and events depicted herein are fictitious, and no association with any real company, organization, product, domain name, e-mail address, logo, person, place or event is intended or should be inferred. Complying with all applicable copyright laws is the responsibility of the user. Without limiting the rights under copyright, no part of this document may be reproduced, stored in or introduced into a retrieval system, or transmitted in any form or by any means (electronic, mechanical, photocopying, recording, or otherwise), or for any purpose, without the express written permission of Microsoft Corporation. Microsoft may have patents, patent applications, trademarks, copyrights, or other intellectual property rights covering subject matter in this document. Except as expressly provided in any written license agreement from Microsoft, the furnishing of this document does not give you any license to these patents, trademarks, copyrights, or other intellectual property. The names of manufacturers, products, or URLs are provided for informational purposes only and Microsoft makes no representations and warranties, either expressed, implied, or statutory, regarding these manufacturers or the use of the products with any Microsoft technologies. The inclusion of a manufacturer or product does not imply endorsement of Microsoft of the manufacturer or product. Links may be provided to third party sites. Such sites are not under the control of Microsoft and Microsoft is not responsible for the contents of any linked site or any link contained in a linked site, or any changes or updates to such sites. Microsoft is not responsible for webcasting or any other form of transmission received from any linked site. Microsoft is providing these links to you only as a convenience, and the inclusion of any link does not imply endorsement of Microsoft of the site or the products contained therein. © 2010 Microsoft Corporation. All rights reserved. Microsoft, and Windows are either registered trademarks or trademarks of Microsoft Corporation in the United States and/or other countries. All other trademarks are property of their respective owners. Product Number: 10266A Part Number: 01918 Released: 09/2010 Programming in C# with Microsoft® Visual Studio® 2010 v Contents Module 1: Introducing C# and the .NET Framework Lesson 1: Introduction to the .NET Framework 4 1-4 Lesson 2: Creating Projects Within Visual Studio 2010 1-16 Lesson 3: Writing a C# Application 1-33 Lesson 4: Building a Graphical Application 1-44 Lesson 5: Documenting an Application 1-58 Lesson 6: Debugging Applications by Using Visual Studio 2010 1-66 Lab: Introducing C# and the .NET Framework 1-78 Module 2: Using C# Programming Constructs Lesson 1: Declaring Variables and Assigning Values 2-4 Lesson 2: Using Expressions and Operators 2-23 Lesson 3: Creating and Using Arrays 2-36 Lesson 4: Using Decision Statements 2-49 Lesson 5: Using Iteration Statements 2-63 Lab: Using C# Programming Constructs 2-78 Module 3: Declaring and Calling Methods Lesson 1: Defining and Invoking Methods 3-3 Lesson 2: Specifying Optional Parameters and Output Parameters 3-29 Lab: Declaring and Calling Methods 3-39 Module 4: Handling Exceptions Lesson 1: Handling Exceptions 4-3 Lesson 2: Raising Exceptions 4-23 Lab: Handling Exceptions 4-34 vi Programming in C# with Microsoft® Visual Studio® 2010 Module 5: Reading and Writing Files Lesson 1: Accessing the File System 5-3 Lesson 2: Reading and Writing Files by Using Streams 5-27 Lab: Reading and Writing Files 5-45 Module 6: Creating New Types Lesson 1: Creating and Using Enumerations 6-3 Lesson 2: Creating and Using Classes 6-12 Lesson 3: Creating and Using Structures 6-33 Lesson 4: Comparing References to Values 6-41 Lab: Creating New Types 6-55 Module 7: Encapsulating Data and Methods Lesson 1: Controlling Visibility of Type Members 7-4 Lesson 2: Sharing Methods and Data 7-15 Lab: Encapsulating Data and Methods 7-29 Module 8: Inheriting from Classes and Implementing Interfaces Lesson 1: Using Inheritance to Define New Reference Types 8-3 Lesson 2: Defining and Implementing Interfaces 8-27 Lesson 3: Defining Abstract Classes 8-45 Lab: Inheriting from Classes and Implementing Interfaces 8-56 Module 9: Managing the Lifetime of Objects and Controlling Resources Lesson 1: Introduction to Garbage Collection 9-4 Lesson 2: Managing Resources 9-21 Lab: Managing the Lifetime of Objects and Controlling Resources 9-35 Module 10: Encapsulating Data and Defining Overloaded Operators Lesson 1: Creating and Using Properties 10-4 Lab A: Creating and Using Properties 10-26 Lesson 2: Creating and Using Indexers 10-38 Lab B: Creating and Using Indexers 10-50 Programming in C# with Microsoft® Visual Studio® 2010 vii Lesson 3: Overloading Operators 10-60 Lab C: Overloading Operators 10-79 Module 11: Decoupling Methods and Handling Events Lesson 1: Declaring and Using Delegates 11-4 Lesson 2: Using Lambda Expressions 11-14 Lesson 3: Handling Events 11-22 Lab: Decoupling Methods and Handling Events 11-38 Module 12: Using Collections and Building Generic Types Lesson 1: Using Collections 12-4 Lab A: Using Collections 12-22 Lesson 2: Creating and Using Generic Types 12-28 Lesson 3: Defining Generic Interfaces and Understanding Variance 12-42 Lesson 4: Using Generic Methods and Delegates 12-56 Lab B: Building Generic Types 12-69 Module 13: Building and Enumerating Custom Collection Classes Lesson 1: Implementing a Custom Collection Class 13-3 Lesson 2: Adding an Enumerator to a Custom Collection Class 13-21 Lab: Building and Enumerating Custom Collection Classes 13-37 Module 14: Using LINQ to Query Data Lesson 1: Using the LINQ Extension Methods and Query Operators 14-3 Lesson 2: Building Dynamic LINQ Queries and Expressions 14-28 Lab: Using LINQ to Query Data 14-47 Module 15: Integrating Visual C# Code with Dynamic Languages and COM Components Lesson 1: Integrating Visual C# Code with Ruby and Python 15-4 Lesson 2: Accessing COM Components from Visual C# 15-19 Lab: Integrating Visual C# Code with Dynamic Languages and COM Components 15-36 viii Programming in C# with Microsoft® Visual Studio® 2010 Appendix: Lab Answer Keys Module 1 Lab: Introducing C# and the .NET Framework L1-1 Module 2 Lab: Using C# Programming Constructs L2-1 Module 3 Lab: Declaring and Calling Methods L3-1 Module 4 Lab: Handling Exceptions L4-1 Module 5 Lab: Reading and Writing Files L5-1 Module 6 Lab: Creating New Types L6-1 Module 7 Lab: Encapsulating Data and Methods L7-1 Module 8 Lab: Inheriting from Classes and Implementing Interfaces L8-1 Module 9 Lab: Managing the Lifetime of Objects and Controlling Resources L9-1 Module 10 Lab A: Creating and Using Properties L10A-1 Module 10 Lab B: Creating and Using Indexers L10B-1 Module 10 Lab C: Overloading Operators L10C-1 Module 11 Lab: Decoupling Methods and Handling Events L11-1 Module 12 Lab A: Using Collections L12A-1 Module 12 Lab B: Building Generic Types L12B-1 Module 13 Lab: Building and Enumerating Custom Collection Classes L13-1 Module 14 Lab: Using LINQ to Query Data L14-1 Module 15 Lab: Integrating Visual C# Code with Dynamic Languages and COM Components L15-1 Introducing C# and the .NET Framework 1-1 Module 1 Introducing C# and the .NET Framework Contents: Lesson 1: Introduction to the .NET Framework 4 1-4 Lesson 2: Creating Projects Within Visual Studio 2010 1-16 Lesson 3: Writing a C# Application 1-33 Lesson 4: Building a Graphical Application 1-44 Lesson 5: Documenting an Application 1-58 Lesson 6: Debugging Applications by Using Visual Studio 2010 1-66 Lab: Introducing C# and the .NET Framework 1-78 1-2 Programming in C# with Microsoft® .Visual Studio® 2010 Module Overview Microsoft® Visual Studio® 2010 and the Microsoft .NET Framework 4 provide a comprehensive development platform to enable you to build, debug, deploy, and manage applications. This module describes the purpose of the .NET Framework 4, and how to build applications by using Visual Studio 2010. Objectives After completing this module, you will be able to: • Explain the purpose of the .NET Framework 4. • Create Microsoft Visual C#® projects by using Visual Studio 2010. • Explain the structure of a Visual C# application. • Use the Windows® Presentation Foundation (WPF) Application template to build a simple graphical application. Introducing C# and the .NET Framework 1-3 • Use XML comments to document an application. • Use the debugger to step through a program. 1-4 Programming in C# with Microsoft® .Visual Studio® 2010 Lesson 1 Introduction to the .NET Framework 4 This lesson introduces the .NET Framework 4, and describes the key concepts of .NET and some of the tools that are provided to help simplify development. Objectives After completing this lesson, you will be able to: • Describe the purpose of the .NET Framework 4. • Describe the role of Visual C# for writing the code for .NET Framework 4 applications. • Describe the purpose of an assembly. • Explain how the common language runtime (CLR) compiles and runs assemblies. • Describe the tools that the .NET Framework 4 provides. [...]... found in most Visual Studio solutions Introducing C# and the NET Framework 1-17 • Explain how to create a console application by using the Console Application template in Visual Studio 2010 • Use Visual Studio to compile and run an application 1-18 Programming in C# with Microsoft® Visual Studio 2010 Key Features of Visual Studio 2010 Key Points Visual Studio 2010 presents a single development environment... These include Refactor, Organize, Create Unit Tests, Go To Definition, Find All References, and Outline These features will be covered in more detail in later modules 1-28 Programming in C# with Microsoft® Visual Studio 2010 Question: What is the purpose of code snippets? Introducing C# and the NET Framework 1-29 Building and Running a NET Framework Application Key Points Visual Studio provides an integrated... following advantages: • It enables you to work on multiple projects within a single Visual Studio 2010 session • It enables you to apply configuration settings globally to multiple projects Introducing C# and the NET Framework • It enables you to deploy multiple projects within a single solution The following table describes the solution definition files File Description sln A Visual Studio 2010 solution... Visual Studio 2010 over a text editor such as Notepad++? 1-20 Programming in C# with Microsoft® Visual Studio 2010 Templates in Visual Studio 2010 Key Points Visual Studio 2010 supports the development of different types of applications such as Windows-based client applications, Web-based applications, services, and libraries To help you get started, Visual Studio 2010 provides several application templates... configuration settings that make up a single NET Framework application or library When you create a project in Visual Studio, the project is automatically organized into a solution The following table describes some of the common file types that you will find in a Visual Studio project 1-24 Programming in C# with Microsoft® Visual Studio 2010 File cs Description Code files that can belong to a single project... implementation of Visual C# 2010, see the Visual C# page http://go.microsoft.com/fwlink/?LinkId=192877 For more information about the new features of C# 4.0, see the What's New in Visual C# 2010 page at http://go.microsoft.com/fwlink/?LinkId=192878 Introducing C# and the NET Framework 1-9 What Is an Assembly? Key Points When you compile a Visual C# application by using Visual Studio 2010, the compiler generates... and which tool would you use? Additional Reading For more information about the tools that the NET Framework provides, see the NET Framework Tools page at http://go.microsoft.com/fwlink/?LinkId=192882 1-16 Programming in C# with Microsoft® Visual Studio 2010 Lesson 2 Creating Projects Within Visual Studio 2010 This lesson introduces you to Visual Studio 2010 and describes how it can help simplify the... Programming in C# with Microsoft® Visual Studio 2010 Edition, which provides a subset of the features that are provided with Visual Studio Note: C# is an evolving language Visual C# 2010 uses C# 4.0, which contains several extensions to the C# language that are not yet part of the ECMA standard Question: Which programming languages have you used? Additional Reading For more information about the Microsoft implementation... versioning, see the Assembly Versioning page at http://go.microsoft.com/fwlink/?LinkId=192880 For more information about assembly signing, see the SignTool.exe (Sign Tool) page at http://go.microsoft.com/fwlink/?LinkId=192881 1-12 Programming in C# with Microsoft® Visual Studio 2010 How the Common Language Runtime Loads, Compiles, and Runs Assemblies Key Points Assemblies contain MSIL code, which is... console project by using the Console Application template in Visual Studio 2010 1 Open Visual Studio 2010 2 On the File menu, point to New, and then click Project 3 In the New Project dialog box, specify the following settings for the project, and then click OK: a In the Installed Templates list, under Visual C#, click Windows b In the center pane, click Console Application Introducing C# and the NET Framework