Introducing C# and the .NET Framework 1-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
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.
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
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.
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
Describe the purpose of an assembly.
Explain how the common language runtime (CLR) compiles and runs
Describe the tools that the .NET Framework 4 provides.
Introducing C# and the .NET Framework 1-5
What Is the .NET Framework 4?
The .NET Framework 4 provides a comprehensive development platform that offers a fast and efficient way to build applications and services. Using Visual Studio 2010, developers can utilize the .NET Framework 4 to create a wide range of solutions that operate across a broad range of computing devices.
The .NET Framework 4 provides three principal elements: the CLR, the .NET Framework class library, and a collection of development frameworks.
The Common Language Runtime
The .NET Framework 4 provides an environment called the CLR. The CLR manages the execution of code and simplifies the development process by providing a robust and secure execution environment that provides common services such as memory management, transactions, interprocess communications, multithreading, and many other features.
1-6 Programming in C# with Microsoft .Visual Studio 2010
The .NET Framework Class Library
The .NET Framework 4 provides a library of reusable classes that developers can use to build applications. The classes provide a foundation of common functionality and constructs that help simplify application development and remove the requirement for developers to constantly reinvent logic. For example, the System.IO.File class contains functionality that enables developers to manipulate files on the Windows file system. In addition to using the classes in the .NET Framework class library, you can extend these classes by creating your own libraries of classes.
The .NET Framework 4 provides several development frameworks that you can use to build common types of applications. These frameworks provide the necessary components and infrastructure to get you started. The development frameworks include:
ASP.NET. Enables you to build server-side Web applications.
WPF. Enables you to build rich client applications.
Windows Communication Foundation (WCF). Enables you to build secure and reliable service-oriented applications.
Windows Workflow Foundation (WF). Enables you to build workflow solutions 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?
For more information about the .NET Framework, see the Microsoft .NET page at http://go.microsoft.com/fwlink/?LinkId=192876.
Introducing C# and the .NET Framework 1-7
The Purpose of Visual C#
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 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?
For more information about the Microsoft 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?
When you compile a Visual C# application by using Visual Studio 2010, the compiler generates an executable file that the CLR can run. This file is called an assembly. An assembly contains code in an intermediate format called Microsoft intermediate language (MSIL). All compilers for the .NET Framework generate code in this format, regardless of the programming language that was used to write an application. This enables the CLR to run code in the same way, regardless of the language that the developer used.
Assemblies are the building blocks of .NET Framework applications; they form the fundamental unit of deployment, version control, reuse, and security.
You can think of an assembly as a collection of types and resources that work together and form a logical unit of functionality. An assembly provides the CLR with the information that it needs to be aware of type implementations.
An assembly can be of two types: an executable program, or a library that contains executable code that other programs can reuse. By using a library, developers can modularize the development of their applications into logical components.
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 version information is stored in the assembly manifest and is used with the assembly name and culture to derive the assemblys identity. An assembly version number consists of the following:
Major version number
Minor version number
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 (GAC), so yo