Tải bản đầy đủ (.pdf) (22 trang)

Introduction to .NET Framework ppsx

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (921.71 KB, 22 trang )

Introduction to .NET
Framework
Chapter 14
The .NET Framework enables you to create robust
and scalable applications. The .NET Framework
consists of Common Language Runtime, Common
Language Specification, and the Just-In-Time
compiler.
Before you can use Visual Studio .NET for creating
a console-based application, you need to understand
the .NET Framework and the Visual Studio .NET
Integrated Development Environment.
This chapter introduces the features and components
of the .NET Framework. It explains the process of
creating and executing a console application in the
Visual Studio .NET IDE.
In this chapter, you will learn to:
 Identify the components of the .NET
Framework
 Use the Visual Studio .NET IDE
Objectives

Introduction to .NET Framework 14.3
¤NIIT
N
ote
Microsoft introduced the .NET Framework with the intention of bridging the gap in
interoperability between applications. This framework aims at integrating various
programming languages and services. It is designed to make significant improvements in
code reuse, code specialization, resource management, multilanguage development,
security, deployment, and administration. It consists of all the technologies that help in


creating and running robust, scalable, and distributed applications. The .NET offers a
complete suite for developing and deploying applications. This suite consists of the .NET
Products, .NET Services, and .NET Framework:
 .NET Products: Microsoft has already introduced Visual Studio .NET, which is a
tool for developing .NET applications by using programming languages such as
Visual Basic, Visual C#, and Visual C++.
These products aim at allowing developers to create applications, which are capable
of interacting seamlessly with each other. To ensure interaction between various
applications, all .NET products use eXtensible Markup Language (XML) for
describing and exchanging data between applications.
XML is a –platform independent markup language. It allows computers to store data in
a format, which can be interpreted by any other computer system. Therefore, XML can
be used to transfer structured data between heterogeneous systems. XML is used as a
common data interchange format in a number of applications.
 .NET Services: .NET helps you to create software as Web services. A Web Service
is an application or business logic that is accessible through standard Internet
protocols such as Hypertext Transfer Protocol (HTTP) and Simple Object Access
Protocol (SOAP). You can identify the service by a Uniform Resource Locator
(URL). Its public interfaces and bindings are described using XML. Therefore, users
can subscribe to such a service and use it as long as they need it, regardless of the
hardware and software platform.
Microsoft has come up with its own set of Web services, known as My Services.
These services are based on the Microsoft Passport Authentication service, which is
used in their Web applications such as Hotmail. This service allows users to access
data by linking calendars, phonebooks, address books, and personal references to the
passport authentication service.
In addition, third party products and services can be integrated easily with the .NET
environment.
Identifying the Components of the .NET Framework
14.4 Introduction to .NET Framework ¤NIIT


.NET Framework: It is the foundation on which you design, develop, and deploy
applications. It is consistent and simplified programming model that helps you to
easily build robust applications. It is the core of the .NET infrastructure because it
exists as a layer between the .NET applications and the underlying operating system.
In other words, the .NET Framework encapsulates much of the basic functionality,
such as debugging and security services, which was earlier built in various
programming languages, in the form of a collection of services and classes.
The following figure shows the various components of the .NET Framework.
The Components of the .NET Framework
The .NET Framework consists of three main components: Common Language Runtime,
.NET Framework Base Classes, and the user and program interfaces.
Common Language Runtime (CLR)
The CLR is one of the most essential components of the .NET Framework. CLR is the
environment where all programs using .NET technologies are executed. It provides
services such as code compilation, memory allocation, and garbage collection. The CLR
allows the execution of code across different platforms by translating code into
Intermediate Language (IL). IL is a low level language that the CLR understands.
IL is converted into machine language during execution by the Just-In-Time (JIT)
compiler. During JIT compilation, code is also checked for type safety. Type safety
ensures that objects are always accessed in a compatible way. If you try to assign an
8-byte value to a variable of size 4 bytes, the CLR will detect and trap such an attempt.
Common Language Runtime
.NET Framework Base Class Libraries
Windows Forms
Web Forms and Web
Services
Console
Applications
Components of the .NET Framework

Introduction to .NET Framework 14.5
¤NIIT
CLR consists of a set of common rules followed by all the languages of the .NET
Framework. This set of rules is known as Common Language Specification (CLS). CLS
enables an object or application to interact with the objects or applications of other
languages. The classes that follow the rules specified by CLS are termed as
CLS-compliant classes. The classes defined in the .NET Framework class library are
CLS-compliant.
One of the specifications defined in CLS is Common Type System (CTS), which provides
a type system that is common across all languages. CTS define how data types are
declared, used, and managed in the code at run time.
The CTS also defines the rules that ensure that the data types of objects written in various
languages are able to interact with each other. For example, the size of integer and long
variables is the same across all CLS-compliant programming languages.
While executing the program, CLR plays an important role.
Identifying the Process of Compilation
Compilation is the process of creating an executable program from a source code. The
source code consists of instructions for the compiler, and an executable program consists
of instructions for the processor. Therefore, compilation converts source code to machine
language.
However, when you compile a program in .NET, the conversion of source code to
machine language happens in two stages. In the first stage, the compiler translates code
into an IL instead of machine language or assembly language. In the second stage the
conversion of IL to machine language is done at run time by the JIT compiler.
Irrespective of the CLS compliant language used to develop the application, the source
code always gets translated into IL. In addition, during the process of compilation, the
compiler also produces metadata about code. Metadata contains the description of code,
such as classes and interfaces, dependencies, and the versions of the components, used in
the application. IL and metadata constitute an assembly.
Assemblies contain metadata, which describe the assembly’s internal version number and

details of all the data and object types they contain. When you compile a VC# application,
Visual Studio .NET creates an assembly that is a single file with the extension .exe or .dll.
Assemblies can also contain one or more modules. For example, larger game projects may
be planned in such a way that several individual developers work on separate modules, all
creating a single assembly.
14.6 Introduction to .NET Framework ¤NIIT
The following figure shows the process of code compilation.
Program Code
Compiler
IL
Metadata
Assembly
+
Code Compilation
Identifying the Process of Code Execution
During execution, CLR performs the following steps:
 Loading assemblies and identifying namespaces: Assemblies are loaded in the
memory. After loading assemblies, CLR identifies namespaces for code in
assemblies. Namespaces are a collection of classes. The .NET Framework uses
namespaces to organize its classes in a hierarchy. Namespaces implicitly have public
access and this cannot be changed.
 JIT compilation: Before execution, IL is converted into machine language by the
JIT compiler. Next, during the verification process, The IL code is examined to
confirm the following points:
z The memory locations that code needs to access are available
z Methods are called only through properly defined types
z IL has been correctly generated
 Garbage collection: The garbage collection process begins after the JIT compilation
and manages the allocation and deallocation of memory for an application.
Whenever you create an object, the CLR allocates memory for the object from the

managed heap. A managed heap is a region of the memory that is available for
program execution. If sufficient memory is not available on the managed heap, the
garbage collection process is invoked.
The code developed in .NET is called managed code. The CLR manages the compilation
and execution of the managed code to ensure proper functioning of the code. For
example, the CLR takes care of garbage collection, exception handling, and type safety
for the managed code.
The unit of execution in the CLR is an assembly. An assembly contains IL and metadata
that was generated during compilation. It contains code that the CLR executes. All
assemblies contain a manifest, which contains information such as assembly name,
Introduction to .NET Framework 14.7
¤NIIT
version, and the list of files that form the assembly. The IL code cannot be executed if it
does not have an associated assembly.
Instead of compiling the complete IL code, the JIT compiler compiles only the code that
is required during execution. It saves the time and memory required to convert the
complete IL into machine language.
The following figures shows the process of code compilation and execution.
SetTopScore()
IgnoreScore()
SetTopScore()
Visual C# Code IL Code
IgnoreScore()
Code Compilation
Code Execution
In the preceding figures, two methods are shown, SetTopScore() and IgnoreScore().
The
SetTopScore() method will be invoked only if the current score of the player is
higher than the top score of the game. The
IgnoreScore() method will be invoked when

SetTopScore()
IgnoreScore()
SetTopScore()
JIT
SetTopScore()
is invoked
IL Code Machine Language
14.8 Introduction to .NET Framework ¤NIIT
the player is not the top scorer and you do not want to add the player to the list of top
scorers.
If the player has scored more than the top score, only the method
SetTopScore()will be
invoked. When it is invoked, the code for the
SetTopScore() method will be compiled by
the JIT compiler. However, the code for the method
IgnoreScore() will not be converted
to machine language by the JIT compiler because this method is not invoked.
The .NET Framework Class Library
The .NET Framework class library works with any .NET language, such as VB.NET,
VC++ .NET, and VC#. This class library is built on the object-oriented nature of the
runtime. The library provides classes that can be used in the code to accomplish a range of
common programming tasks, such as string management, data collection, database
connectivity, and file access.
One of the most important features of the .NET Framework class library is that it can be
used in a consistent manner across multiple languages. This means that you can use the
same set of classes for performing a specific task in VC# as well as in VC++.
The .NET Framework class library comprises namespaces, which are contained within
assemblies. Let us look at what these two terms mean.
Namespaces
Namespaces help you to create logical groups of related classes and interfaces, which can

be used by any language targeting the .NET Framework. Namespaces allow you to
organize your classes so that they can be easily accessed in other applications.
Namespaces can be used to avoid any naming conflicts between classes, which have the
same names. For example, you can use two classes with the same name in an application
provided they belong to different namespaces.
You can access the classes belonging to a namespace by simply importing the namespace
into the application. The .NET Framework uses a dot (.) as a delimiter between classes
and namespaces. For example,
System.Console represents the Console class of the
System namespace. Namespaces are also stored in assemblies.
Assemblies
An assembly is a single deployable unit that contains all the information about the
implementation of classes, structures, and interfaces. The assembly stores all the
information about itself. This information is called metadata and includes the name and
version number of the assembly, security information, information about the
dependencies, and a list of the files that constitute the assembly.
Introduction to .NET Framework 14.9
¤NIIT
All the applications developed using the .NET Framework are made up of assemblies.
Assemblies and the metadata provide the CLR with the information required for
executing an application. For example, if an application uses a component, the assembly
keeps track of the version number of the component used in the application. The assembly
provides this information to the CLR while the application is being executed. Assemblies
also play an important role in deployment and versioning.
User and Program Interfaces
At the presentation layer, .NET provides three types of user interfaces. They are Windows
Forms, Web Forms, and Console Applications. Windows Forms are used in
Windows-based applications, whereas Web Forms are used in Web-based applications for
providing an interactive user interface. They provide a Web browser-based user interface.
You can create character-based console applications that can be executed from the

command line.
.NET provides a program interface, Web Services, to communicate with remote
components.
Advantages of the .NET Framework
Some of the advantages offered by the .NET Framework are:
 Consistent programming model: The .NET Framework provides a common OOPs
model across languages. This object model can be used in code to perform several
tasks, such as reading from and writing to files, connecting to databases, and
retrieving data.
 Multi-platform applications: There are several versions of Windows most of which
run on x86 CPUs. Some versions, such as Windows CE and 64-bit Windows, run on
non-x86 CPUs as well. A .NET application can execute on any architecture that is
supported by the CLR. In future, a CLR version could even be built for
non-windows platforms.
 Multi-language integration: .NET allows multiple languages to be integrated. For
example, it is possible to create a class in VC# that derives from a class implemented
in VB.NET. To enable objects to interact with each other regardless of the language
used to develop them, a set of language features has been defined in CLS.
This specification includes the basic language features required by many
applications. The CLS enhances language interoperability. The CLS also establishes
certain requirements, which help you to determine whether your managed code
conforms to the CLS. Most of the classes defined in the .NET Framework class
library are CLS-compliant.
 Automatic resource management: While creating an application, a programmer
may be required to write code for managing resources such as files, memory,
14.10 Introduction to .NET Framework ¤NIIT
network connections, and database resources. If a programmer does not free these
resources, the application may not execute properly. The CLR automatically tracks
resource usage and relieves a programmer of the task of manual resource
management.

 Ease of deployment: One of the goals of the .NET Framework is to simplify
application deployment. .NET applications can be deployed simply by copying files
to the target computer. Deployment of components has also been simplified. Till
now, Microsoft’s Component Object Model (COM) has been used for creating
components. However, COM suffers from various problems relating to deployment.
For example, every COM component needs to be registered before it can be used in
an application.
Moreover, two versions of a component cannot run side-by-side. In such a case, if
you install a new application that installs the newer version of the component, the
newly installed application may function normally. However, the existing
applications that depend on the earlier version of the component may stop
functioning. As against this, the .NET Framework provides zero-impact deployment.
Installation of new applications or components does not have an adverse effect on
the existing applications.
In the .NET Framework, applications are deployed in the form of assemblies. An
assembly stores metadata. Therefore, registry entries are not required for storing
information about components and applications. In addition, assemblies also store
information about the versions of components used by an application. Therefore, the
problems relating to versioning are also eliminated in the .NET Framework.
Introduction to .NET Framework 14.11
¤NIIT
The Visual Studio .NET IDE provides you with a common interface for developing
various kinds of projects for the .NET Framework. The IDE also provides you with a
centralized location for designing the user interface for an application, writing code, and
compiling and debugging the application.
The Visual Studio .NET IDE is available to all the programmers who use languages in the
Visual Studio .NET suite. Before you start using Visual Studio .NET for creating VC#
applications, you need to know the various components of the Visual Studio .NET IDE.
In Visual Studio .NET, an application can be made up of one or more items, such as files
and folders. To organize these items efficiently, Visual Studio .NET has provided two

types of containers: projects and solutions.
A project typically contains items that make up the application. These items are
interrelated. A project allows you to manage, build, and debug the items that make up an
application. When you build a project, it usually results in the creation of an executable
.exe or .dll files. These files that are created as a result of building the project are called
the project output.
A solution usually acts as a container for one or more projects. For example, you may
create a solution containing two projects, one for manipulation of data and the other for
generation of reports for the sales division of an organization. Thus, a solution allows you
to work on multiple projects within the same instance of the Visual Studio .NET IDE.
Creating Projects and Solutions
Using Visual Studio .NET IDE
14.12 Introduction to .NET Framework ¤NIIT
A solution containing multiple projects is shown in the following figure.
Solution Containing Multiple Projects
To create a console application in Visual Studio, you need to create a project. To create a
project, you need to perform the following steps:
1. Select StartÆAll ProgramsÆMicrosoft Visual Studio 2005ÆMicrosoft Visual
Studio 2005. The Start Page - Microsoft Visual Studio window will be displayed.
2. Select FileÆNewÆProject. The New Project dialog box will be displayed.
Solution
Project 1
Project 2
Miscellaneous
Files
Project 1
Items
Project 2
Items
Introduction to .NET Framework 14.13

¤NIIT
3. In the New Project dialog box, select Visual C# from the Project Types pane and
Console Application from the Templates pane.
There are many templates available in the Templates pane for a Visual C# project.
You can select any one template described in the following table.
Template Description
Windows Application Creates an application with Windows user interface.
Class Library Creates a class or a reusable component that exposes
some functionality to be used in various projects.
Windows Control Library Creates a custom control that can be added to the
user interface.
Console Application Creates a console application that can run from the
command line. A console application has a command
line interface.
Device Application Creates a forms application for Pocket PC 2003 and
later.
Crystal Reports Application Adds a Windows user interface and a sample Crystal
Report in the Visual C# application.
Templates Available in a Visual C# Project
4. Specify the name of the application in the Name text box.
5. Specify the location where the new project is to be created in the Location combo
box. You can use the Browse button to browse to the folder in which the new project
is to be created.
6. Click the OK button.
14.14 Introduction to .NET Framework ¤NIIT
User Interface Elements of Visual Studio .NET IDE
When you work with a console application project in Visual Studio .NET, you can use the
following main elements in Visual Studio .NET IDE.
The Elements of Visual Studio .NET IDE
The preceding figure contains various standard interface elements found in the Windows

environment, such as the menu bar and the toolbar. In addition to these standard interface
elements, the Visual Studio .NET IDE contains other elements. These include the
Solution Explorer window, Output window, Task List window, Class View window, and
the Code Editor window.
In addition, Visual Studio .NET provides the Start Page and the Project Properties
window. The Start Page allows you to perform several tasks and the Project Properties
helps you to set the projects compilation and configuration values.
Standard
Toolbar
Solution
Explorer
Window
Code
Editor
Output
Window
Introduction to .NET Framework 14.15
¤NIIT
Standard Toolbar
The Standard toolbar is located below the menu bar. It provides shortcuts for menu
commands. The first few buttons of the toolbar enable you to perform tasks common to
many Windows-based programs, such as opening a new or an existing file, saving or
printing a file, cutting and pasting text and objects, and undoing or redoing the most
recent actions. Other standard toolbar buttons offer functions more specific to Visual
Studio .NET applications. The name and functions of the various tools of the Standard
toolbar are listed in the following table.
Name Button Function
New Project Creates a new project
Add New Item Add new item in the
project

Save Saves the Program
Save All Save all the unsaved
items of the
application
Cut Places selected text or
objects on the
Windows Clipboard
Copy Places a copy of
selected text or objects
on the Windows
Clipboard
Paste
Pastes the contents of
the Clipboard on the
document
Start Debugging Compiles and
Executes the current
project
Standard Toolbar Buttons
14.16 Introduction to .NET Framework ¤NIIT
The Start Page
When you start Visual Studio .NET, it displays the Start Page - Microsoft Visual Studio
window.
The Start Page is the default home page for the browser provided within the Visual Studio
.NET IDE. This allows you to perform several tasks, such as specifying your preferences,
searching for information on the new features of .NET, communicating with developers
working on the .NET platform, and searching for more information in the MSDN Online
Library.
When you open Visual Studio .NET, the Projects tab on the Start Page is selected by
default. This tab displays some of the recent projects and the last date of their

modification. You can open any one of these projects. If the project that you wish to work
on is not listed, click the Open Project button on the Start Page. If you wish to start with a
new project, you can click the New Project button on the Start Page.
The Solution Explorer Window
The Solution Explorer window lists the solution name, the project name, and all the
classes that are used in the project. You can open a particular file existing in a project by
double-clicking the file in the Solution Explorer window. The Solution Explorer window
is shown in the following figure.
The Solution Explorer Window
Introduction to .NET Framework 14.17
¤NIIT
The Output Window
The Output window displays messages for the status of various features provided in the
Visual Studio .NET IDE. For example, when you compile an application, this window
displays the current status. After the compilation process is complete, it specifies the
number of errors that occurred during compilation. If this window is not visible, you can
open it by selecting ViewÆOutput Window or by pressing the Ctrl+W, O keys
simultaneously.
The Output window is shown in the following figure.
The Output Window
14.18 Introduction to .NET Framework ¤NIIT
The Error List Window
The Error List window displays a list of errors along with the source (the file and the line
number) of the error. It helps you identify and locate problems that are detected
automatically as you edit or compile code. You can locate the source of a particular error
by simply double-clicking the error in the Error List window. You can open the Error List
window by clicking ViewÆError List window. Alternatively, you can press the Ctrl+W,
E keys simultaneously. The Error List window is as shown in the following figure.
The Error List Window
The Class View Window

The Class View window displays the classes, methods, and properties associated with a
particular file. They are displayed in a hierarchical tree-view depicting the containership
of these items. When you double-click any one of the items in the Class View window, it
brings up the Code Editor window for that item, making it convenient to move through
the code in the project.
The Class View window has two buttons, one for sorting the items in the Class View
window and the other for creating a new folder. If the Class View window is not visible,
you can open it either by selecting ViewÆClass View or by pressing the Ctrl+Shift+C
keys simultaneously.
Introduction to .NET Framework 14.19
¤NIIT
The Class View window is shown in the following figure.
The Class View Window
The Code Editor
The code editor allows you to enter and edit code. You may use this editor to add code for
your class.
14.20 Introduction to .NET Framework ¤NIIT
The code editor is shown in the following figure.
The Code Editor
To compile and execute the application, you need to perform the following steps:
1. Select BuildÆBuild Solution or press F6 to compile the application.
2. Select DebugÆStart Debugging or press F5 to execute the application.
Compiling and Executing Project
Introduction to .NET Framework 14.21
¤NIIT
1. What is Just-In-Time compilation?
2. Which CLR feature ensures that data types are accessible across different
applications?
3. What is project output?
4. Which Visual C# template allows you to create a custom control that can be used in a

GUI game?
a. Windows Control Library
b. Device Application
c. Class Library
d. Crystal Reports Application
5. You need to select the option from the Debug menu to execute a game developed in
Visual Studio .NET.
6. State whether the given statement is true or false:
JIT compilation saves time and memory required to convert the complete IL into
machine language.
Practice Questions
14.22 Introduction to .NET Framework ¤NIIT
In this chapter, you learned that:
 The .NET Framework is made up of many components, such as CLS, CLR, and JIT
compiler.
 CLS is a set of rules that are followed by all the languages of the .NET Framework.
 When a program is compiled using Visual Studio .NET the compiler translates the
code into the IL instead of machine language.
 The JIT compiler is used to translate code from IL into machine language.
 The CLR is the environment where all .NET applications are executed.
 The Visual Studio .NET IDE provides you with a common interface for developing
various kinds of applications for the .NET Framework.
 Visual Studio .NET provides two types of containers, projects and solutions, to
organize the constituents of an application.
Summary

×