Common Language Runtime (CLR)

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 8

Introducing the .

NET Framework
The .NET Framework, introduced by Microsoft, aims at integrating various programming
languages and services. The .NET Framework is designed to make significant improvements in
code reuse, code specialization, resource management, Multilanguage development, security,
deployment, and administration. The .NET Framework consists of all the technologies that help
in creating and running robust, scalable, and distributed applications.

• The .NET suite consists of:

• .NET Products

• .NET Services

• .NET Framework

.NET Products: - .NET products aim at allowing developers to create applications, which are
capable of interacting seamlessly with each other. All .NET products use eXtensible Markup
Language (XML) for describing and exchanging data between applications. An example of a
.NET product is Visual Studio .NET.

.NET Services: - .NET helps us 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). We can
identify the service by a Uniform Resource Locator (URL).

The .NET Framework: - It is the foundation for designing, developing, and deploying
applications. It is the core of the .NET infrastructure because it exists as a layer between the
.NET applications and the underlying operating system.

The various components of the .NET Framework are:

• Common Language Runtime

• The .NET Framework Class Library

• User and Program Interfaces

Common Language Runtime (CLR):


CLR is the environment where all programs in .NET are executed. CLR provides services such
as code compilation, memory allocation, and garbage collection. 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 JIT compiler. During JIT compilation, code is also checked for type
safety. Type safety ensures that objects are always accessed in a compatible way.

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

While executing the program, CLR:

• Identifies the process of compilation

• Identifies the process of code execution

The .NET Framework Class Library:


The .NET Framework class library works with any .NET language, such as VB.NET, VC++
.NET, and VC#. The .NET Framework class 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.

The .NET Framework class library comprises of:

• Namespaces.

• Assemblies

User and Program Interfaces:


At the presentation layer, .NET provides three types of user interfaces:

• Windows Forms

• Web Forms

• Console Applica

.NET provides a program interface, Web Services, to communicate with remote components.
The Visual Studio .NET
The Visual Studio .NET IDE provides we with a common interface for developing various kinds
of projects for the .NET Framework. The IDE also provides we with a centralized location for
designing the user interface for an application, writing code, and compiling and debugging the
application. 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:

• Project: It typically contains items that make up the application. These items are
interrelated.

• Solution: It usually acts as a container for one or more projects.

To create a console application in Visual Studio, we need to create a project. To create a project,
we 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.
3. In the New Project dialog box, select Visual C# from the Project Types
pane and Console Application from the Templates pane.
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. We can use the Browse button to browse to the folder in
which the new project is to be created.
6. Click the OK button.

To compile and execute the application, we 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.

3. Select DebugàStart without Debugging or press Ctrl + F5 to execute the


application.
Introducing C#
C#, also known as C-Sharp, is a programming language introduced by Microsoft. C# is specially
designed to work with the Microsoft’s .NET platform. C# (pronounced "see sharp") is a multi-
paradigm programming language encompassing imperative, declarative, functional, generic,
object-oriented (class-based), and component-oriented programming disciplines. It was
developed by Microsoft within the .NET initiative and later approved as a standard by Ecma
(ECMA-334) and ISO (ISO/IEC 23270). C# is one of the programming languages designed for
the Common Language Infrastructure.

By design, C# is the programming language that most directly reflects the underlying Common
Language Infrastructure (CLI). Most of its intrinsic types correspond to value-types implemented
by the CLI framework. However, the language specification does not state the code generation
requirements of the compiler: that is, it does not state that a C# compiler must target a Common
Language Runtime, or generate Common Intermediate Language (CIL), or generate any other
specific format. Theoretically, a C# compiler could generate machine code like traditional
compilers of C++ or Fortran.

The Common Language Infrastructure (CLI) is an open specification developed by Microsoft


that describes the executable code and runtime environment that form the core of the
Microsoft .NET Framework and the free and open source implementations Mono and
Portable.NET. The specification defines an environment that allows multiple high-level
languages to be used on different computer platforms without being rewritten for specific
architectures.

The CLI specification describes the following four aspects:-

• The Common Type System (CTS)


A set of data types and operations that are shared by all CTS-compliant programming languages.
• Metadata
Information about program structure is language-agnostic, so that it can be referenced between languages
and tools, making it easy to work with code written in a language we are not using.
• Common Language Specification (CLS)
A set of base rules to which any language targeting the CLI should conform in order to
interoperate with other CLS-compliant languages. The CLS rules define a subset of the Common Type
System.
• Virtual Execution System (VES)
The VES loads and executes CLI-compatible programs, using the metadata to combine separately
generated pieces of code at runtime.
FEATURES OF C#
1. There are no global variables or functions. All methods and members must be declared
within classes. Static members of public classes can substitute for global variables and
functions.

2. C# supports a strict Boolean datatype, bool. Statements that take conditions, such as
while and if, require an expression of a type that implements the true operator, such as the
Boolean type.

3. In C#, memory address pointers can only be used within blocks specifically marked as
unsafe, and programs with unsafe code need appropriate permissions to run. Most object
access is done through safe object references, which always either point to a "live" object
or have the well-defined null value; it is impossible to obtain a reference to a "dead"
object (one which has been garbage collected), or to a random block of memory. An
unsafe pointer can point to an instance of a value-type, array, string, or a block of
memory allocated on a stack.

4. Managed memory cannot be explicitly freed; instead, it is automatically garbage


collected. Garbage collection addresses the problem of memory leaks by freeing the
programmer of responsibility for releasing memory which is no longer needed.

5. In addition to the try...catch construct to handle exceptions, C# has a try...finally


construct to guarantee execution of the code in the finally block.

6. Multiple inheritance is not supported, although a class can implement any number of
interfaces.

7. C# is more type safe than C++. The only implicit conversions by default are those which
are considered safe, such as widening of integers. This is enforced at compile-time,
during JIT, and, in some cases, at runtime. Any user-defined conversion must be
explicitly marked as explicit or implicit, unlike C++ copy constructors and conversion
operators, which are both implicit by default.

8. Enumeration members are placed in their own scope.

9. C# provides properties as syntactic sugar for a common pattern in which a pair of


methods, accessor (getter) and mutator (setter) encapsulate operations on a single
attribute of a class.C# currently (as of version 4.0) has 77 reserved words.

10. Checked exceptions are not present in C# (in contrast to Java). This has been a conscious
decision based on the issues of scalability and versionability.
CTS separates datatypes into two categories

Value types:-Value types are plain aggregations of data. Instances of value types do not have
referential identity or referential comparison semantics - equality and inequality comparisons for
value types compare the actual data values within the instances, unless the corresponding
operators are overloaded. Value types are derived from System.ValueType, always have a
default value, and can always be created and copied. Some other limitations on value types are
that they cannot derive from each other (but can implement interfaces) and cannot have an
explicit default (parameterless) constructor. Examples of value types are some primitive types,
such as int (a signed 32-bit integer), float (a 32-bit IEEE floating-point number), char (a 16-bit
Unicode code unit), and System.DateTime (identifies a specific point in time with nanosecond
precision). Other examples are enum (enumerations) and struct (user defined structures).

Reference types: - Reference types have the notion of referential identity - each instance of a
reference type is inherently distinct from every other instance, even if the data within both
instances is the same. This is reflected in default equality and inequality comparisons for
reference types, which test for referential rather than structural equality, unless the corresponding
operators are overloaded (such as the case for System.String). Examples of reference types are
object (the ultimate base class for all other C# classes), System.String (a string of Unicode
characters), and System.Array (a base class for all C# arrays).

Both type categories are extensible with user-defined types.

Compiling and Executing C# Program

• After writing the program in a Notepad, we need to compile and execute it to get the
desired output.
• The compiler converts the source code that we write into the machine code, which the
computer can understand.
• The following steps are needed to compile and execute a C# program.
1. Save the code written in the Notepad with an extension .cs.
2. To compile the code, we need to go to the Visual Studio 2005 Command
Prompt window. Select StartàAll ProgramsàMicrosoft Visual Studio
2005àVisual Studio ToolsàVisual Studio 2005 Command Prompt. The Visual
Studio 2005 Command Prompt window is displayed to compile the program.
3. In the Visual Studio 2005 Command Prompt window, move to the location
where the programs file is saved.
4. Compile the program file by using the following command:
csc myexample.cs
5. To execute the code, type the following in the command prompt:
myexample.exe

ADO.NET
ADO.NET is a model used by .NET applications to communicate with a database for retrieving,
accessing, and updating data. It uses a structured process flow to interact with a database.It is a
part of the base class library that is included with the Microsoft .NET Framework. It is
commonly used by programmers to access and modify data stored in relational database systems,
though it can also access data in non-relational sources. ADO.NET is sometimes considered an
evolution of ActiveX Data Objects (ADO) technology, but was changed so extensively that it
can be considered an entirely new product.

Features of ADO.NET
 Disconnected data architecture — Applications connect to the database only while
retrieving and updating data.
 Data cached in datasets — ADO.NET is based on a disconnected data structure.
Therefore, the data is retrieved and stored in datasets.
 Data transfer in XML format — ADO.NET uses XML for transferring information from
a database into a dataset and from the dataset to another component.
 Interaction with the database is done through data commands.

ADO.NET Object Model


 Data from a database can be assessed through:

 A dataset

 A DataReader object

Key Components of the ADO.NET Model


 Data Provider

 Is used for connecting to a database, retrieving data, and storing the data.

 Is of two types:

 OLE DB data provider

 SQL Server data provider


Components of a Data Provider
 Connection

 Used to establish a connection with a data source

 Some commonly used properties and methods:

 ConnectionString property

 Open()method

 Close()method

 Data adapter

 Creates a dataset and updates the database.


 Handles data transfer between the database and the dataset through its properties
and methods.
 Displays the data through the process of table mapping.
 Are of two types:
 SqlDataAdapter
 OleDbDataAdapter .

 Data command

 Is a SQL statement or a stored procedure that is used to retrieve, insert, delete, or


modify data from a data source.
 Is an object of the OleDbCommand or SQLCommand class.

 Data reader

 Is used to retrieve data from a data source in a read-only and forward-only mode.
 Stores a single row at a time in the memory.
 Commonly used methods:
 Read()
 Close()
Dataset
 Is a disconnected, cached set of records that are retrieved from a database.
 Is present as a DataSet class in the System.Data namespace.

You might also like