Tài liệu Module 2: Introduction to a Managed Execution Environment - Pdf 84


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

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


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


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.

!
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

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

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

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 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

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-

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


Nhờ tải bản gốc

Tài liệu, ebook tham khảo khác

Music ♫

Copyright: Tài liệu đại học © DMCA.com Protection Status