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
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
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
• 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.
Introducing C# and the .NET Framework 1-5
What Is the .NET Framework 4?
Key Points
The .NET Framework 4 provides a comprehensive development platform that
offers a fast and efficient way to build applications and services. Using Visual
to fulfill the complex business requirements of modern organizations.
Question: What is the purpose of the .NET Framework 4, and the three main
components that it provides?
Additional Reading
For more information about the .NET Framework, see the Microsoft .NET page at Introducing C# and the .NET Framework 1-7
The Purpose of Visual C#
Key Points
The CLR runs executable code that is generated by using a compiler. You can build
applications for the .NET Framework by using any language that has a compiler
that can generate executable code in the format that the CLR recognizes. Visual
Studio 2010 provides compilers for C++, Visual Basic, F#, and C#. Compilers for
other languages are available from a variety of third-party vendors.
C# is the language of choice for many developers. It uses a syntax that is very
similar to C, C++, and Java, and has several extensions and features that are
designed for operation with the .NET Framework. Because of its heritage, many
developers who are familiar with other programming languages find C# easy to
learn and can be productive very quickly.
The C# language has been standardized and is described by the ECMA-334 C#
Language Specification. Several vendors apart from Microsoft produce C#
compilers. The Microsoft implementation is called Visual C#, and is integrated into
Visual Studio. Visual Studio supports Visual C# with a full-featured code editor,
compiler, project templates, designers, code wizards, a powerful and easy-to-use
debugger, and other tools. C# is also available from Microsoft as Visual C# Express
1-8 Programming in C# with Microsoft® .Visual Studio® 2010
Edition, which provides a subset of the features that are provided with Visual
1-10 Programming in C# with Microsoft® .Visual Studio® 2010
Typically, when you are distributing assemblies to customers as part of your
application, you will want to ensure that the assembly contains versioning
information, and that the assembly is signed.
Versioning your assemblies is important because ultimately, any applications that
you build will have multiple releases. Versioning information can help you identify
which versions customers already have and enable you to perform the necessary
steps to upgrade the application. Similarly versioning information can also help
when documenting and fixing bugs.
Signing your assemblies is equally important because it ensures that your assembly
cannot easily be modified or replaced by an alternative implementation from a
malicious source, and because it gives the assembly a strong name.
Information such as the assembly version and security identity is stored as
metadata in an assembly manifest. The manifest also contains metadata that
describes the scope of the assembly, and any references to classes and resources.
The manifest is typically stored in a portable executable (PE) file.
Assembly Versioning
Assembly version information is stored in the assembly manifest and is used with
the assembly name and culture to derive the assembly’s identity. An assembly
version number consists of the following:
• Major version number
• Minor version number
• Build number
• Revision number
Assembly Signing
Assembly signing is an important step that developers should include in their build
process because it provides the following benefits:
• It protects assemblies from modification.
• It enables you to include the signed assembly in the Global Assembly Cache
The assemblies will already be compiled into MSIL.
2. The MSIL-to-native compiler verifies the MSIL code and then compiles all
assemblies into machine code ready for execution.
Introducing C# and the .NET Framework 1-13
Note: The CLR performs the verification step because it is possible to write your own
MSIL code. If you use a C# compiler, the MSIL code will be valid, but the CLR cannot
make any assumptions.
3. The Code Manager loads the executable assembly and runs the Main method.
4. The Garbage Collector provides automatic lifetime memory management of all
objects that your application creates. The Garbage Collector disposes of any
objects that your application is no longer using.
5. The Exception Manager provides structured exception handling for .NET
applications, which is integrated with Windows structured exception
handling.
Question: What steps does the CLR perform when you run your application?
1-14 Programming in C# with Microsoft® .Visual Studio® 2010
What Tools Does the .NET Framework Provide?
Key Points
The .NET Framework provides several tools to help simplify the development of
.NET applications. The following table describes some of the key tools.
Tool Description
Code Access Security Policy
Tool (Caspol.exe)
Enables users to modify the machine, user, and
enterprise security policy. This can include defining
a custom permission set and adding assemblies to
Strong Name Tool (Sn.exe) Enables users to sign assemblies with strong names.
The Strong Name Tool includes commands to
create a new key pair, extract a public key from a
key pair, and verify assemblies.
Question: You have created two applications that both use an assembly called
Contoso.ReportGenerator.dll. Both applications will run on the same machine.
What is the best approach to share the Contoso.ReportGenerator.dll assembly
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
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 development of .NET applications through the use of predefined
application templates, and features of the integrated development environment
(IDE).
Objectives
After completing this lesson, you will be able to:
• Describe the features that are available in Visual Studio 2010 that aid
programming productivity.
• Describe the various project types that Visual Studio 2010 supports and when
to use them.
• Describe the primary files that are found in most Visual Studio solutions.
Introducing C# and the .NET Framework 1-17
displays any errors, warnings, or messages that are produced as you edit and
build your code.
• Help and documentation. Visual Studio 2010 also provides help and guidance
through Microsoft IntelliSense®, code snippets, and the integrated help system,
which contains documentation and samples.
Question: What are the main reasons why you may choose Visual Studio 2010
over a text editor such as Notepad++?