Tải bản đầy đủ

Tài liệu Module 2: Introduction to a Managed Execution Environment ppt








Contents
Overview 1
Writing a .NET Application 2
Compiling and Running a .NET Application 11
Lab 2: Building a Simple .NET Application 29
Review 32

Module 2: Introduction
to a Managed Execution
Environment


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.

 2001-2002 Microsoft Corporation. All rights reserved.

Microsoft, ActiveX, BizTalk, IntelliMirror, Jscript, MSDN, MS-DOS, MSN, PowerPoint,
Visual Basic, Visual C++, Visual C#, Visual Studio, Win32, Windows, Windows Media, and
Window NT are either registered trademarks or trademarks of Microsoft Corporation in the U.S.A.
and/or other countries.

The names of actual companies and products mentioned herein may be the trademarks of their
respective owners.



Module 2: Introduction to a Managed Execution Environment iii


Instructor Notes
After completing this module, students will be able to:
!
Create simple console applications in C#.
!
Explain how code is compiled and executed in a managed execution
environment.
!
Explain the concept of garbage collection.

Materials and Preparation
This section provides the materials and preparation tasks that you need to teach
this module.
Required Materials
To teach this module, you need the following materials:
!
Microsoft
®
PowerPoint
®
file 2349B_02.ppt
!
Sample managed module HelloDemoCS.exe

Preparation Tasks
To prepare for this module, you should:
!
Read all of the materials for this module.
!
Practice the demonstrations.
!
Review the animation.
!
Complete the lab.

Presentation:
45 Minutes

Lab:
20 Minutes
iv Module 2: Introduction to a Managed Execution Environment


Demonstrations
This section provides demonstration procedures that will not fit in the margin
notes or are not appropriate for the student notes.
Hello World
This demonstration shows how to build a simple application in C#.
In the following procedures, use Notepad to create the simple Hello World
application, and build and run the HelloDemoCS.exe application from the
command line.
!
To create the source code in C#
1. Open Notepad and type the following code:
// Allow easy reference to System namespace classes
using System;

// Create class to hold program entry point
class MainApp {
public static void Main() {

// Write text to the console
Console.WriteLine(“Hello World using C#!”);
}
}

2. Save the file as HelloDemoCS.cs.


To use Microsoft Visual Studio
®
.NET tools within a command
prompt window, the command prompt window must have the proper
environment settings. The Visual Studio .NET Command Prompt window
provides such an environment. To run a Visual Studio .NET Command Prompt
window, click Start, All Programs, Microsoft Visual Studio .NET,
Visual Studio .NET Tools, and Visual Studio .NET Command Prompt.

Important
Module 2: Introduction to a Managed Execution Environment v


!
To compile the source code and build an executable program
• From a Visual Studio .NET Command Prompt window, type the following
syntax:
csc HelloDemoCS.cs

Running the resulting executable file will generate the following output:
Hello World using C#!


Viewing Assembly Metadata by Using the MSIL
Disassembler
This demonstration shows how to use the Microsoft Intermediate Language
(MSIL) Disassembler (Ildasm.exe) to view an assembly’s metadata.
The code for this demonstration is contained in one project and is located in
<install folder>\Democode\Mod02. To demonstrate how to use the MSIL
Disassembler to view the contents of the HelloDemoCS.exe assembly, follow
the directions in Demonstration: Using the MSIL Disassembler in this module.
vi Module 2: Introduction to a Managed Execution Environment


Multimedia
This section lists the multimedia items that are part of this module. Instructions
for launching and playing the multimedia are included with the relevant slides.
Application Loading and Single-File Assembly Execution
This animation will show students how a single-file private assembly is loaded
and executed.
Module Strategy
Use the following strategy to present this module:
!
Writing a .NET Application
Stress the importance of understanding the process of compiling and
running Microsoft .NET Framework applications, by using the simple Hello
World application. Focus primarily on the compilation and execution
processes.
!
Compiling and Running a .NET Application
This section introduces basic concepts of a managed execution environment
and presents new terminology. Many of these concepts are covered in
greater detail in subsequent modules in this course, in subsequent courses,
and in the .NET Framework software development kit (SDK)
documentation.
Emphasize that you are primarily introducing new concepts and
terminology. Be prepared to postpone answering questions that pertain to
information that is covered in later modules. Encourage students to start
reading the .NET Framework SDK documentation.

Module 2: Introduction to a Managed Execution Environment 1


Overview
!
Writing a .NET Application
!
Compiling and Running a .NET Application

*****************************
ILLEGAL FOR NON
-
TRAINER USE
******************************
This module introduces the concept of managed execution and shows you how
to quickly build applications that use the Microsoft
®
.NET Framework common
language runtime environment. A simple Hello World version of a console
application illustrates most of the concepts that are introduced in this module.
Because this course is an introduction to programming in the .NET Framework,
you should spend some time reading the .NET Framework software
development kit (SDK) documentation. In fact, the labs, demonstrations, and
material for this module and other modules in this course are based on several
tutorials in the .NET Framework SDK.
After completing this module, you will be able to:
!
Create simple console applications in C#.
!
Explain how code is compiled and executed in a managed execution
environment.
!
Explain the concept of garbage collection.

Topic Objective
To provide an overview of
the module topics and
objectives.
Lead-in
This module introduces the
concept of managed
execution and shows you
how to quickly build
applications that use the
Microsoft .NET Framework
common language runtime
environment.
2 Module 2: Introduction to a Managed Execution Environment


"
""
"

Writing a .NET Application
!
Using a Namespace
!
Defining a Namespace and a Class
!
Entry Points, Scope, and Declarations
!
Console Input and Output
!
Case Sensitivity

*****************************
ILLEGAL FOR NON
-
TRAINER USE
******************************
Because all supported languages use the Common Type System and the .NET
Framework base class library, and run in the common language runtime,
programs in the supported languages are similar. The most significant
difference in programming with the supported languages is syntax.

In this module, and in Modules 3 and 4, Notepad is used as the source
code editor, instead of the Microsoft Visual Studio
®
.NET development
environment. The examples in these modules are simple enough to be compiled
and built directly from a command prompt window. Working in Notepad will
allow you to focus on the compilation and execution processes.

Topic Objective
To introduce the topics in
the section.
Lead-in
Because all supported
languages use the Common
Type System and the .NET
Framework class library,
and run in the common
language runtime, programs
in the supported languages
are similar.
Delivery Tip
Stress the importance of
understanding the process
of compiling and running the
.NET applications. Using
Visual Studio .NET at this
time may obscure the
underlying processes.
Note
Module 2: Introduction to a Managed Execution Environment 3


Demonstration: Hello World

*****************************
ILLEGAL FOR NON
-
TRAINER USE
******************************
In this demonstration, you will learn how to build a simple application in C#.
!
To create the source code in C#
1. Open Notepad and type the following code:
// Allow easy reference to System namespace classes
using System;

// Create class to hold program entry point
class MainApp {
public static void Main() {

// Write text to the console
Console.WriteLine(“Hello World using C#!”);
}
}

2. Save the file as HelloDemoCS.cs

Topic Objective
To demonstrate how to build
a simple application in C#.
Lead-in
In this demonstration, you
will learn how to build a
simple application in C#.
Delivery Tip
As this is a short, simple
demonstration, you may
want to let students try it
themselves.
4 Module 2: Introduction to a Managed Execution Environment


!
To compile the source code and build an executable program

To use Visual Studio .NET tools within a command prompt
window, the command prompt window must have the proper environment
settings. The Visual Studio .NET Command Prompt window provides such an
environment. To run a Visual Studio .NET Command Prompt window, click
Start, All Programs, Microsoft Visual Studio .NET, Visual Studio .NET
Tools, and Visual Studio .NET Command Prompt.

• From a Visual Studio .NET Command Prompt window, type the following
syntax:
csc HelloDemoCS.cs

Running the resulting executable will generate the following output:
Hello World using C#!


Important
Module 2: Introduction to a Managed Execution Environment 5


Using a Namespace
!
Classes Can Be Fully Referenced
!
Or the Namespace of a Class Can Be Referenced
#
No need to fully qualify contained class names
// declares a FileStream object
System.IO.FileStream aFileStream;
// declares a FileStream object
System.IO.FileStream aFileStream;
using System.IO;
...
FileStream aFileStream;
using System.IO;
...
FileStream aFileStream;

*****************************
ILLEGAL FOR NON
-
TRAINER USE
******************************
You can fully reference classes, as in the following example, in which an
instance of System.IO.FileStream is declared by using C#:
System.IO.Filestream aFileStream;

However, it is more convenient to reference the required namespaces in your
program. Using the namespace effectively disposes of the need to qualify all
class library references, as in the following example:
using System.IO;
...
FileStream aFileStream;

For example, in order to have convenient access to System objects, you must
use the System namespace.
Topic Objective
To describe how to use
namespaces in the .NET
Framework.
Lead-in
You can fully reference
classes in which an instance
of System.IO.FileStream is
declared by using C#:
6 Module 2: Introduction to a Managed Execution Environment


Defining a Namespace and a Class
!
C# Supports Creation of Custom Namespaces and
Classes Within Those Namespaces
namespace CompCS {
public class StringComponent {
...
}
}
namespace CompCS {
public class StringComponent {
...
}
}

*****************************
ILLEGAL FOR NON
-
TRAINER USE
******************************
C# supports the creation of custom namespaces and classes within those
namespaces.

The following is the general rule for naming namespaces:
CompanyName.TechnologyName
For example:
Microsoft.Office
This is merely a guideline. Third-party companies can choose other names.

Namespaces in C#
In C#, you use the namespace statement to define a new namespace, which
encapsulates the classes that you create, as in the following example:
namespace CompCS {
public class StringComponent {
...
}
}

Note that a namespace may be nested in other namespaces, and a single
namespace may be defined in multiple files. A single source code file may also
define multiple namespaces.
Topic Objective
To describe how to define
namespaces and classes in
C#.
Lead-in
C# supports the creation of
custom namespaces and
classes within those
namespaces.
Tip
Module 2: Introduction to a Managed Execution Environment 7


Entry Points, Scope, and Declarations
!
In C#, the External Entry Point for a Program Is in a Class
!
C# Supports the Use of a Period As a Scope Resolution Operator
!
In C#, Objects Must Be Declared Before They Can Be Used and Are
Instantiated Using the New Keyword
class MainApp
{ public static void Main()
{. . .}
}
class MainApp
{ public static void Main()
{. . .}
}
Console.WriteLine ("First String");
Console.WriteLine ("First String");
Lib.Comp myComp = new Lib.Comp();
Lib.Comp myComp = new Lib.Comp();

*****************************
ILLEGAL FOR NON
-
TRAINER USE
******************************
Every executable program must contain an external entry point, where the
application begins its execution. In C#, all code must be contained in methods
of a class.
Entry Points in C#
To accommodate the entry point code in C#, you must first specify the class, as
in the following example:
class MainApp {...}

Next, you specify the entry point for your program. The compiler requires this
entry point to be a public static method called Main, as in the following
example:
public static void Main () {...}

Scope
C# uses the period as a scope resolution operator. For example, you use the
syntax
Console.WriteLine
when referencing the WriteLine method of the
Console object.
Declaring and Instantiating Variables
In C#, you must declare a variable before it can be used. To instantiate the
object, use the new keyword. The following example in C# shows how to
declare an object of type Comp, in namespace Lib, with the name myComp:
Lib.Comp myComp = new Lib.Comp();

Topic Objective
To describe how to create
program entry points in C#.
Lead-in
Every executable program
must contain an external
entry point, where the
application begins its
execution.
8 Module 2: Introduction to a Managed Execution Environment


Console Input and Output
!
Console Class Methods
#
Read, ReadLine, Write, and WriteLine
Console.WriteLine("Hello World using C#!");
Console.WriteLine("Hello World using C#!");

*****************************
ILLEGAL FOR NON
-
TRAINER USE
******************************
You can use the common language runtime Console class of the System
namespace for input and output to the console of any string or numeric value by
using the Read, ReadLine, Write, and WriteLine methods.
The following example shows a C# program that outputs a string to the console:
using System;

class MainApp {
public static void Main() {

// Write text to the console
Console.WriteLine(“Hello World using C#!”);
}
}

Topic Objective
To describe how to use
Console
class methods
in C#.
Lead-in
You can use the runtime
Console
class of the
System
namespace for
input and output to the
console of any string or
numeric value by using the
Read
,
ReadLine
,
Write
,
and
WriteLine
methods.
Module 2: Introduction to a Managed Execution Environment 9


Case Sensitivity
!
Do Not Use Names That Require Case Sensitivity
#
Components should be fully usable from both case-
sensitive and case-insensitive languages
#
Case should not be used to distinguish between
identifiers within a single name scope
!
Avoid the Following
class customer {...}
class Customer {...}
void foo(int X, int x)
class customer {...}
class Customer {...}
void foo(int X, int x)

*****************************
ILLEGAL FOR NON
-
TRAINER USE
******************************
Microsoft Visual C++
®
and C# are case-sensitive, but Microsoft Visual Basic
®

is not case-sensitive. To ensure that a program is compliant with the Common
Language Specification (CLS), however, you must take special care with public
names. You cannot use case to distinguish between identifiers within a single
name scope, such as types within assemblies and members within types.
The following examples show situations to avoid:
!
Do not have two classes or namespaces whose names differ only by case.
class customer { ... }
class Customer { ... }

!
Do not have a function with two parameters whose names differ only by
case.
void foo(int X, int x)


This constraint enables Visual Basic (and potentially other case-insensitive
languages) to produce and use components that have been created in other case-
sensitive languages. This constraint does not apply to your definitions of private
classes, private methods on public classes, or local variables.
Topic Objective
To describe case sensitivity
issues in programming
languages.
Lead-in
C++ and C# are case-
sensitive, but Visual Basic is
not case-sensitive.
10 Module 2: Introduction to a Managed Execution Environment



To fully interact with other objects regardless of the language they were
implemented in, objects must expose to callers only those features that are
common to all the languages they must interoperate with. For this reason, a set
of language features has been defined, called the Common Language
Specification (CLS), which includes common language features that are needed
by many applications. The CLS rules define a subset of the common type
system; that is, all the rules that apply to the common type system apply to the
CLS, except where stricter rules are defined in the CLS. If your component uses
only CLS features in the API that it exposes to other code (including derived
classes), the component is guaranteed to be accessible from any programming
language that supports the CLS. Components that adhere to the CLS rules and
use only the features included in the CLS are said to be CLS-compliant
components.

Note

Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay

×

×