Vous êtes sur la page 1sur 16

C# Programming

Unit 1

Unit 1 Introduction to .Net: The C# Environment


Structure:
1.1
Introduction
Objectives
1.2

.NET Framework An Overview

1.3
1.4
1.5
1.6
1.7
1.8
1.9
1.10
1.11
1.12
1.13

Components of .NET
Common Language Specification (CLS)
Common Language Runtime (CLR)
Microsoft Intermediate Language ("MSIL" or "IL")
The Common Type System (CTS)
.NET Framework Base Classes
Web Services, Web Forms, and Windows Forms
The .Net Languages
Summary
Terminal Questions
Answers

1.1 Introduction
As you have already read the topics of OOPs with Java (Subject Code
BT 0074) covering programming with classes and objects, Website Design
(Subject Code BT 0078) covering the design and applications of a
Website, and Visual Basic (Subject Code BT 0082). These are the
prerequisites for you to understand the .Net Framework and usage of C#
language, which is a pure Object Oriented Language and have a
comfortable start with this subject.
The .NET Framework introduces a completely new model for the
programming and deployment of applications. .NET is Microsoft's vision of
"software as a Service", a development environment in which you can build,
create, and deploy your applications and the next generation of
components, the ability to use the Web rather than your own computer for
various services.
Microsoft introduced great technologies like COM, DCOM, COM+ etc., to
enable reuse of Software. Although these technologies are very powerful to
Sikkim Manipal University

Page No.: 1

C# Programming

Unit 1

reuse Software, they require a huge learning curve. According to this


aspect, Microsoft realized that it is time to come up with a new technology
within which each programming task is easily accomplished.
Objectives:
After studying this unit, you should be able to:
describe .NET framework and its components
explain
the concepts and usage of CLS (Common Language
Specification)
discuss the concepts and usage of CLR (Common Language Runtime)
explain the concepts and usage of MSIL (Microsoft Intermediate
language)
discuss the concepts and usage of CTS (Common Type Specification)
describe the functionality of .NET Base Classes
explain the usage of various .NET languages.

1.2 .NET Framework An Overview


The .NET Framework is an integral Windows component that supports
building and running the next generation of applications and XML Web
services.
The .NET Framework is designed to fulfill the following objectives:
To provide a consistent object-oriented programming environment
whether object code is stored and executed locally, executed locally but
Internet-distributed, or executed remotely.
To provide a code-execution environment that minimizes software
deployment and versioning conflicts.
To provide a code-execution environment that promotes safe execution
of code, including code created by an unknown or semi-trusted third
party.
To provide a code-execution environment that eliminates the
performance problems of scripted or interpreted environments.
To make the developer experience consistency across widely varying
types of applications, such as Windows-based applications and Webbased applications.
To build all communication on industry standards to ensure that code
based on the .NET Framework can integrate with any other code.
Sikkim Manipal University

Page No.: 2

C# Programming

Unit 1

The .NET Framework has two main components: the common language
runtime and the .NET Framework class library. The CLR is the foundation of
the .NET Framework. You can think of the runtime as an agent that
manages code at execution time, providing core services such as memory
management, thread management, and remoting, while also enforcing strict
type safety and other forms of code accuracy that promote security and
robustness.
In fact, the concept of code management is a fundamental principle of the
runtime. Code that targets the runtime is known as managed code, while
code that does not target the runtime is known as unmanaged code. The
class library, the other main component of the .NET Framework, is a
comprehensive, object-oriented collection of reusable types that you can
use to develop applications ranging from traditional command-line or
graphical user interface (GUI) applications to applications based on the
latest innovations provided by ASP.NET, such as Web Forms and XML Web
services.
The .NET Framework can be hosted by unmanaged components that load
the common language runtime into their processes and initiate the execution
of managed code, thereby creating a software environment that can exploit
both managed and unmanaged features. The .NET Framework not only
provides several runtime hosts, but also supports the development of thirdparty runtime hosts.
For example, ASP.NET hosts the runtime to provide a scalable, server-side
environment for managed code. ASP.NET works directly with the runtime to
enable ASP.NET applications and XML Web services, both of which are
discussed later.
Internet Explorer is an example of an unmanaged application that hosts the
runtime (in the form of a MIME type extension). Using Internet Explorer to
host the runtime enables you to embed managed components or Windows
Forms controls in HTML documents. Hosting the runtime in this way makes
managed mobile code (similar to Microsoft ActiveX controls) possible,
but with significant improvements that only managed code can offer, such
as semi-trusted execution and isolated file storage.

Sikkim Manipal University

Page No.: 3

C# Programming

Unit 1

The Figure 1.1 below shows the relationship of the common language
runtime and the class library to your applications and to the overall system.
It also shows how managed code operates within a larger architecture.

Fig. 1.1: Relationship between CLR and Class Library

The .NET Framework Class Library


The .NET Framework class library is a collection of reusable types that
tightly integrate with the Common Language Runtime (CLR). The class
library is object oriented, providing types from which your own managed
code can derive functionality. This not only makes the .NET Framework
types easy to use, but also reduces the time associated with learning new
features of the .NET Framework. In addition, third-party components can be
integrated seamlessly with classes in the .NET Framework.
For example, the .NET Framework collection classes implement a set of
interfaces that you can use to develop your own collection classes. Your
collection classes will blend seamlessly with the classes in the .NET
Framework.
As you would expect from an object-oriented class library, the .NET
Framework types enable you to accomplish a range of common
programming tasks, including tasks such as string management, data
collection, database connectivity, and file access. In addition to these
common tasks, the class library includes types that support a variety of
specialized development scenarios.
Sikkim Manipal University

Page No.: 4

C# Programming

Unit 1

The .NET Framework may be used to develop the following types of


applications and services:
Console applications.
Windows GUI applications (Windows Forms).
Windows Presentation Foundation (WPF) applications.
ASP.NET applications.
Web services.
Windows services.
Service-oriented
applications
using
Windows
Communication
Foundation (WCF).
Workflow-enabled applications using Windows Workflow Foundation
(WF).
Self Assessment Questions
1. A software framework is a _______ design for a ____________ system.
2. .NET is Microsoft's vision of "software as a Service", a development
environment in which you can _____, ______, and _____ your
applications.

1.3 The Components of .NET


The major components of the .Net framework are shown in figure 1.2 below:
Windows
Forms
Controls

ASP.Net
Web
Web
Services
Forms
ASP.NET Application Services

Drawing

Windows Application Services

.NET Framework Base Classes


ADO.NET
Net

XML
Security

Threading

IO

Diagnostics

Etc.

Common Language Runtime


Memory
Management

Common Type
System

Life Cycle
Monitoring

Fig. 1.2: Components of .Net Framework


Sikkim Manipal University

Page No.: 5

C# Programming

Unit 1

The .NET framework can only be exploited by languages that are compliant
with .NET. Most of Microsoft languages have been made to fully comply with
.NET.
.NET also introduces Web Forms, Web Services and Windows Forms. The
reason why they have been shown separately and not as a part of a
particular language is that these technologies can be used by any .NET
compliant language. For example Windows Forms is used by VC, VB.NET,
and C # all as a mode of providing GUI.
The next component of .NET is the .NET Framework Base Classes. These
are the common class libraries (much like Java packages) that can be used
by any .NET compliant language. These classes provide the programmers
with a high degree of functionality that they can use in their programs.
Example: Classes to handle reading, writing and manipulating XML
documents, enhanced ADOs etc.
The bottom most layer is the CLR.

1.4 The Common Language Specification (CLS)


One of the obvious themes of .NET is unification and interoperability
between various programming languages. In order to achieve this, certain
rules must be laid and all the languages must follow these rules, i.e. we
cannot have languages running around creating their own extensions and
their own fancy new data types.
CLS is the collection of the rules and constraints that define a subset of
common types and programming constructs that every language (that seeks
to achieve .NET compatibility) must follow. If you make use of a data type or
programming construct that is outside of the CLS, you cannot guarantee
that every .NET programming language can interact with your binary code
library.

1.5 The Common Language Runtime (CLR)


The Common Language Runtime or CLR is the .NET equivalent of Java
Virtual Machine (JVM). The primary goal of the CLR is to locate, load, and
manage .NET types. The term runtime can be understood as a collection of
external services that are required to execute a given compile unit of code.
Sikkim Manipal University

Page No.: 6

C# Programming

Unit 1

Example: Developers make use of the Microsoft Foundation Classes


(MFC) to create a new application, they are aware that their binary is
required to link with the MFC runtime library.
The .NET runtime provides a single well defined runtime layer that is shared
by all languages and platforms that are .NET aware. It supports cross
language interoperability.
The CLR provides a number of services that include:
Loading and Execution of programs.
Memory isolation for applications.
Verification of Type safety.
Compilation of IL into native executable code.
Providing meta data
Automatic memory management(Garbage Collection)
Enforcement of Security.
Interoperability with other systems.
Managing exceptions and errors.
Support for tasks such as debugging and profiling.

Common Language Runtime


Common Type System (CTS)
Intermediate Language(IL)
Execution Support Functions
Security
Garbage collector
Class Loader
Memory Layout
Fig. 1.3: CLR Components

The code that you develop with a language compiler targeting the runtime is
called Managed Code. It benefits from features such as cross-language
Sikkim Manipal University

Page No.: 7

C# Programming

Unit 1

integration, cross-language exception handling, enhanced security,


versioning and deployment support, a simplified model for component
interaction, and debugging and profiling services.
To enable the runtime to provide services to managed code, language
compilers must emit metadata that describes the types, members, and
references in your code. Metadata is stored with the code; every loadable
common language runtime Portable Executable (PE) file contains metadata.
The runtime uses metadata to locate and load classes, lay out instances in
memory, resolve method invocations, generate native code, enforce
security, and set run-time context boundaries.
As part of their metadata, all managed components carry information about
the components and resources they were built against. The runtime uses
this information to ensure that your component or application has the
specified versions of everything it needs, which makes your code less likely
to break because of some unmet dependency.
The mechanism of Garbage Collection eliminates memory leaks and
common programming errors.
The CLR is physically represented by an assembly named mscoree.dll
(Microsoft Common Object Runtime Execution Engine). When an assembly
is referenced for use, mscoree.dll is loaded automatically, which in turn
loads the required assembly into memory.
In addition to loading your custom assemblies and creating your custom
types, the CLR will also interact with the types contained within the .NET
base class libraries.
Self Assessment Questions
3. CLR is the .NET equivalent of _________.
4. CLR is physically represented by an assembly named ________.
5. CLR supports ___________.

1.6 The Microsoft Intermediate Language ("MSIL" or "IL")


The Microsoft Intermediate Language (MSIL or IL) is a platform independent
language that gets compiled into platform dependent executable file or a
Dynamic Link Library (DLL). It means that the .NET compiler can generate
Sikkim Manipal University

Page No.: 8

C# Programming

Unit 1

the code written using any supported languages and finally convert it to the
required machine code depending on the target machine.
With MSIL, all the .Net applications would be compiled into a standard
common format and will interact with the .Net environment in a consistent
manner. The MSIL code is send to the CLR that converts the code to
machine language which is then run on the host machine. MSIL is similar to
Java Byte code.

1.7 The Common Type System (CTS)


The CTS defines how types are declared, used, and managed in the
runtime, and is also an important part of the runtime's support for crosslanguage integration.
The CTS performs the following functions:
Establishes a framework that helps enable cross-language integration,
type safety, and high performance code execution.
Provides an object-oriented model that supports the complete
implementation of many programming languages.
Defines rules that languages must follow, which helps ensure that
objects written in different languages can interact with each other.
That is all the language sharing the same data types. What this means is
that an int should mean the same in VB, VC++, C# and all other .NET
compliant languages. This is achieved through introduction of Common
Type System (CTS). CTS defines every data type as a class; this means
that only Object-Oriented (or Object-Based) languages can achieve .NET
compliance.

1.8 .NET Framework Base Classes


The .NET Framework class library is a library of classes, interfaces, and
value types that are included in the Windows Software Development Kit
(SDK). This library provides access to system functionality and is designed
to be the foundation on which .NET Framework applications, components,
and controls are built.
The .NET platform provides a base class library that is available to all .NET
programming languages. Not only does this base class library encapsulate
various primitives such as threads, file I/O, graphical rendering and
Sikkim Manipal University

Page No.: 9

C# Programming

Unit 1

interaction with various hardware devices, but it also provides support for a
number of services required by most real world applications.
Self Assessment Questions
6. Microsoft Intermediate Language (MSIL) is a platform independent
language that gets compiled into __________.
7. The common type system defines how ________ are declared, used,
and __________ in the runtime.
8. The .NET Framework class library is a library of classes, interfaces, and
_________ that are included in the Windows Software Development Kit
(SDK).

1.9 Web Services, Web Forms, and Windows Forms


We have seen in the previous units console applications to demonstrate
C# and the Common Language Runtime (CLR). Although console
applications can be implemented simply, it is time for us to learn about
building Windows and W eb applications.
In the early days of Windows computing, an application ran on a desktop, in
splendid isolation. Over time, many industries and developers found it
beneficial to spread their applications across a network, with the user
interface on one computer and a database on another.
This division of responsibilities or partitioning of an application came to be
called two-tier or client-server application development. Later three-tier or ntier approaches emerged as developers began to use web servers to host
business objects that could handle the database access on behalf of clients.

There is a clear distinction between Windows applications and web


applications. Windows applications ran on the desktop or a local area
network (LAN), and web applications ran on a distant server and were
accessed by a browser. This distinction is now being blurred as Windows
applications reach out to the Web for services.
The primary remaining distinction between a Windows application and a
web application might be this: who owns the user interface? Will your
application use a browser to display its user interface, or will the UI be built
into the executable running on the desktop?

Sikkim Manipal University

Page No.: 10

C# Programming

Unit 1

There are enormous advantages to web applications, starting with the


obvious: they can be accessed from any browser that can connect to the
server. In addition, updates can be made at the server, without the need to
distribute new dynamic link libraries (DLLs) to your customers.
On the other hand, if your application derives no benefit from being on the
Web, you might find that you can achieve greater control over the look and
feel of your application or that you can achieve better performance by
building a desktop application.
.NET offers closely related, but distinguishable, suites of tools for building
Windows or web applications. Both are based on forms, with the premise
that many applications have user interfaces centered on interacting with the
user through forms and controls, such as buttons, list boxes, text, and so
forth.
The tools for creating web applications are called Web Forms and the tools
for creating Windows applications are called Windows Forms.
Web Services: They promote software reusability in distributed systems
where applications execute across multiple computers on a network.
A Web Service is a class that allows its methods to be called by methods
on other machines via common data formats and protocols, such as XML
and HTTP.
In a .NET environment, the over-the-network method calls are commonly
implemented through the Simple Object Access Protocol (SOAP), an
XML-based protocol describing how to mark up requests and responses so
that they can be transferred via protocols such as HTTP.
Using SOAP, applications represent and transmit data in a standardized
XML-based format.
Web services represent the next step in object-oriented programming-rather
than developing software from a small number of class libraries provided at
one location, programmers can access Web service class libraries
distributed worldwide.
Web services are not the best solution for certain performance-intensive
applications, because applications that invoke Web services experience
network delays.
Sikkim Manipal University

Page No.: 11

C# Programming

Unit 1

Data transfers are typically larger because of the fact that data is
transmitted in text-based XML formats.
Web services facilitate collaboration and allow businesses to grow.
By purchasing Web services and using extensive free Web services that are
relevant to their businesses, companies can spend less time developing
new applications.
Web Forms
Similar to how the Win Forms provide a unified way of developing GUI for
desktop applications, the Web Forms provide a similar tool for web
applications. Web Forms has been introduced in .NET as a part of
ASP.NET. Web Forms are a forms engine that provide a browser-based
user interface.
Web Forms consists of two parts - a template, which contains HTML-based
layout information for all the GUI elements and a Component which contains
all the logic to be hooked to the controls or GUI elements. This provides a
neat presentation layer and application logic layer separation.
The GUI will be rendered on the client side, while the code that has been
hooked to the GUI elements will run on the server side.
Another good feature of Web Forms is that it can be built to have enough
intelligence to support a vast variety of browsers. The same ASP page
would render itself using DHTML, if the browser is IE 5.5. but if the browser
is Netscape the web page will be rendered using HTML tags; if the page is
being accessed through a WAP device the same page will render itself
using WML tags. With ASP.NET Microsoft has provided such presentationbusiness layer separation by introducing the concept of Web Forms:
Window Forms
Windows Forms (also called Win Forms) are used to create GUI for
Windows desktop applications. The idea of Win Form has been borrowed
from Windows Foundation Classes (WFC) which was used for Visual J++.
Win Forms provide an integrated and unified way of developing GUI. It has
a rich variety of Windows controls and user interface support.

Sikkim Manipal University

Page No.: 12

C# Programming

Unit 1

1.10 The .Net Languages


Out of ALL .NET languages, Visual Basic.NET (VB.NET) is one language
that has probably undergone the most changes. Now VB.NET may be
considered a complete Object-Oriented Language (as opposed to its
previous "Half Object Based and Half Object Oriented" status).
ASP, is another language that has been improved markedly. Microsoft has
tried to turned the tables by introducing ASP.NET. ASP.NET makes
extensive use of Web-Services. There are many other features that have
been introduced in ASP.NET, to make it an ideal distributed programming
tool and to measure up against JSP.
Visual C++ (VC++) , has undergone changes to incorporate .NET. VC++
also maintains its status as being a platform dependent programming. Many
new MFC classes have been added; a programmer can choose between
using MFC and compiling the program into a platform specific executable
file; or using .NET framework classes and compile into platform independent
MISL file.
Microsoft has also developed a brand new programming language C#
(C Sharp). This language makes full use of .NET. It is a pure object oriented
language. A Java programmer may find most aspects of this language to be
identical to Java. If you are a new comer to Microsoft Technologies this
language is the easiest way to get on the .NET band wagon.
C# is developed to make full use of all the intricacies of .NET. The learning
curve of C# for a Java programmer is minimal.
Microsoft has also developed J# (Java Sharp). C# may be similar to Java,
but it is not entirely identical. It is for this reason that Microsoft has
developed J# - the syntax of J# is identical to Visual J++.
Self Assessment Questions
9. Win Forms provide a unified way of developing GUI for ___________.
10. The _______ promote software reusability in distributed systems
where applications execute across multiple computers on a network.

Sikkim Manipal University

Page No.: 13

C# Programming

Unit 1

1.11 Summary
This unit has discussed the following topics:
1. The .NET Framework: The .NET Framework introduces a completely
new model for the programming and deployment of applications. .NET is
Microsoft's vision of "software as a Service", a development environment
in which you can build, create, and deploy your applications and the next
generation of components, the ability to use the Web rather than your
own computer for various services. This section discussed the basic
structure and usage of the .Net Framework with respect to C#
programming.
2. Components of .NET: The .NET framework can only be exploited by
languages that are compliant with .NET. Most of Microsoft languages
have been made to fully comply with .NET. .NET also introduces Web
Forms, Web Services and Windows Forms. This section described the

major components of a .Net Framework in detail.


3. The Common Language Specification (CLS): CLS is the collection of
the rules and constraints that define a subset of common types and
programming constructs that every language (that seeks to achieve
.NET compatibility) must follow. This section described the usage and
applications of CLS in a .net Framework.
4. The Common Language Runtime (CLR): The Common Language
Runtime or CLR is the .NET equivalent of Java Virtual Machine (JVM).
The primary goal of the CLR is to locate, load, and manage .NET types.
This section described the usage and applications of CLR in a .net
Framework.
5. The Microsoft Intermediate Language ("MSIL" or "IL"): The Microsoft
Intermediate Language (MSIL or IL) is a platform independent language
that gets compiled into platform dependent executable file or a Dynamic
Link Library (DLL). This section described the usage and applications of
MSIL in a .net Framework.
6. The Common Type System (CTS): The CTS defines how types are
declared, used, and managed in the runtime, and is also an important
part of the runtime's support for cross-language integration. This section
illustrates the application of the CTS in a .Net Framework.
Sikkim Manipal University

Page No.: 14

C# Programming

Unit 1

7. The .NET Framework Base Classes: The .NET Framework class


library is a library of classes, interfaces, and value types that are
included in the Windows Software Development Kit (SDK). This library
provides access to system functionality and is designed to be the
foundation on which .NET Framework applications, components, and
controls are built. This section discussed various base classes of

.Net Framework used in most of the C# programs.


8. Web Services, Web Forms, and Windows Forms: This section takes
a walkthrough of the concepts of Web Services, Web Forms and
Windows forms and their development using C# as a programming
language.
9. The .NET Languages: There are numerous languages used by and
compatible with the .Net Framework. This section discusses various .Net
languages and their applications.

1.12 Terminal Questions


1.
2.
3.
4.
5.

What is .Net Framework?


Describe the usage of MSIL in running programs using C#.
Explain the importance of CLR in C# application development.
List some of the services provided by the CLR
List the Languages supported by the .NET framework.

1.13 Answers
Self Assessment Questions
1. Re-usable, software
2. build, create, and deploy
3. Java Virtual Machine (JVM)
4. Mscoree.dll
5. Cross language interoperability
6. Dynamic Link Library (DLL)
7. types
8. value types
9. Windows Desktop Applications
10. Web Services

Sikkim Manipal University

Page No.: 15

C# Programming

Unit 1

Terminal Questions
1. NET is Microsoft's vision of "software as a service", a development
environment in which you can build, create, and deploy your applications
and the next generation of components, the ability to use the Web rather
than your own computer for various services. (Refer Section 1.1)
2. Microsoft Intermediate Language (MSIL) is a platform independent
language that gets compiled into platform dependent executable file or
dynamic link library. (Refer Section 1.6)
3. CLR is .NET equivalent of Java Virtual Machine (JVM).The primary goal
of the CLR is to locate, load, and manage. (Refer Section 1.5)
4. Some of the main services provided by the CLR are Cross-Language
integration, cross - language exception handling, enhanced security,
versioning and deployment support, a simplified model for component
interaction, and debugging and profiling services. (Refer Section 1.5)
5. APL, C++, C#, Cobol, etc (Refer Section 1.4)

Sikkim Manipal University

Page No.: 16

Vous aimerez peut-être aussi