Anda di halaman 1dari 25

VPL NOTES FOR Hasan Adil For midterm - Farukh Khalid

What is .NET?

.NET is an integral part of many applications running on


Windows and provides common functionality for those
applications to run. This download is for people who
need .NET to run an application on their computer. For
developers,
the
.NET
Framework
provides
a
comprehensive and consistent programming model for
building applications that have visually stunning user
experiences and seamless and secure communication.

The .NET Framework

The .NET Framework is a technology 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 Internetdistributed, 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 consistent across


widely varying types of applications, such as
Windows-based
applications
and
Web-based
applications.
To build all communication on industry standards to
ensure that code based on the .NET Framework can
integrate with any other code.

Microsoft .NET Framework is a software framework which


is installed in Microsoft Windows. Software which are
developed using .NET require .NET Framework to be
installed in your system to be able to run.
Once you install .NET Framework, it creates the required
software environment so that it can provide the
appropriate runtime requirements to the software.

The .NET Framework consists of the common language


runtime and the .NET Framework class library. The
common language runtime 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 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.

Common Language Infrastructure (CLI)


Common Language Infrastructure (CLI) is a Microsoft
specification for running high-level language program
applications in different computer systems without
changing the application code. CLI is based on the
Microsoft .NET concept that some high-level language
programs require modifications due to system hardware
and processing constraints. CLI compiles applications as
Intermediate Language (IL), which is automatically
compiled as native system code. This approach allows
applications to run without code rewrites in limited
systems.

Common Type System (CTS)


The Common Type System (CTS) is a standard for defining
and using data types in the .NET framework. CTS defines
a collection of data types, which are used and managed by
the run time to facilitate cross-language integration.

Common Language Specification (CLS)


The Common Language Specification (CLS) is a
fundamental set of language features supported by the
Common Language Runtime (CLR) of the .NET Framework.
CLS is a part of the specifications of the .NET Framework.
CLS was designed to support language constructs
commonly used by developers and to produce verifiable
code, which allows all CLS-compliant languages to ensure
the type safety of code. CLS includes features common to
many object-oriented programming languages. It forms a
subset of the functionality of common type system (CTS)
and has more rules than defined in CTS.

Virtual Execution System (VES)


Virtual Execution System, or VES, is a component of the
Common Language Runtime (CLR) portion of the .NET
Framework. Its function is to provide a platform for
executing the managed code of an application.
Common Intermediate Language
CIL is a low level language based on the Common
language Infrastructure specification document. CIL was
earlier referred as MSIL (Microsoft Intermediate
Language) but later changed to CIL to standardize
the name of the language which is based on the
Common Language Infrastructure specification.

CIL is CPU and platform-independent instructions that


can be executed in any environment supporting the
Common Language Infrastructure, such as the .NET run
time or the cross-platform Mono run time. These
instructions are later processed by run time compiler and
is then converted into native language. CIL is an objectoriented assembly language, and is entirelystack-based.

What is Common Language Runtime?


Common Language Runtime is the backbone of the .NET
framework. CLR takes care of a number of low-level
executions such as application hosting, thread handling,
memory management, security checks and application
performance. Its primary role is to locate, load, and
manage the .NET types (class, array, object etc.). The
term service refers to as a collection of services that are
required to execute a code. The beauty of CLR is that all
.NET-supported languages can be executed under this
single defined runtime layer. The spinal cord of CLR is
represented by a library refers to asmscoree.dll (common
object runtime execution engine). When an assembly is
referenced for use, mscoree.dll is automatically loaded.
The CLR first locates the referenced assembly, and then it
loads it into memory, compiles the associated IL code into
platform specific instructions, performs security related
checks, and finally executes the code.

In Depth, the .NET compilation process completes in two


stages; first, the C# code you write is compiled into an
Intermediate Code (MSIL). All .NET languages are
compiled into virtual identical IL code. The compiled file
with IL code is referred to as Assembly.
The second level of compilation happens just before the
application is executed. At this point, the IL code is
compiled into low-level native machine code. The Process
of Converting IL Code to native code will be done with
the help of JIT compiler (Just-In-Time Compiler). This
stage is known-as Just-In-Time (JIT) Compilation. Finally
.NET CLR produces the executable or library files and
executes the code.

The CLR (Common Language Runtime) is Microsoft's


implementation of the VES (Virtual Execution System).
The VES along with the CTS (Common Type System), the
CLS (common language specification) and the metadata
specification are all part of the CLI (Common Language
Infrastructure) specification.

The VES is a standardized virtual machine specification


that must be implemented in order to load and execute
CIL (Common Intermediate Language) modules (dll and
exe). A VES implementation also provide runtime services
such as garbage collection and security.

Common Language Runtime


As part of Microsoft's .NET Framework, the Common
Language Runtime (CLR) is programming that manages
the execution of programs written in any of several
supported languages, allowing them to share common
object-oriented classes written in any of the languages.
The Common Language Runtime is somewhat comparable
to the Java Virtual Machine that Sun Microsystems
furnishes for running programs compiled from the Java
language. Microsoft refers to its Common Language
Runtime as a "managed execution environment." A
program compiled for the CLR does not need a languagespecific execution environment and can easily be moved
to and run on any system with Windows 2000 or Windows
XP .

WHAT ARE LIB IN PROGRAMING


In computing, a library is a collection of similar objects that
are stored for occasional use - most frequently, programs

in source code or object code form, data files, scripts,


templates, fonts, and physical storage units such as tape
cartridges. Here are some common types of libraries.

1) A program library is a collection of (usually)


precompiled, reusable programming routines that a
programmer can "call" when writing code so that the
programmer doesn't have to write it. A dynamic link library
(DLL) is one type of program library. Another type of
program library is a class library, whose stored routines
are class definitions in object-oriented programming
(OOP). Graphical user interface (GUI) components such as
scroll bars, buttons, and windowing routines are generally
stored in a class library.

2) A storage library is a collection of physical storage


media such as tapes or disks and a way to access them. A
tape library, for example, contains tape cartridges and a
mechanism that moves them into and out of the drive(s)
where their content is read or updated.

3) A data library is the area of a data center (a centralized


area housing computer systems and equipment) where

storage media are archived. Online service providers also


sometimes refer to a directory on a server containing files
for downloading as a data library
Machine code: This is the most well-defined one. It is
code that uses the byte-code instructions which your
processor (the physical piece of metal that does the actual
work) understands and executes directly. All other code
must be translated or transformed into machine code
before your machine can execute it.
Native code: This term is sometimes used in places where
machine code (see above) is meant. However, it is also
sometimes used to mean unmanaged code (see below).
Unmanaged code and managed code: Unmanaged
code refers to code written in a programming language
such as C or C++, which is compiled directly into machine
code. It contrasts with managed code, which is written in
C#, VB.NET, Java, or similar, and executed in a virtual
environment (such as .NET or the JavaVM) which kind of
simulates a processor in software. The main difference is
that managed code manages the resources (mostly the
memory allocation) for you by employing garbage
collection and by keeping references to objects opaque.
Unmanaged code is the kind of code that requires you to
manually allocate and de-allocate memory, sometimes
causing memory leaks (when you forget to de-allocate)
and sometimes segmentation faults (when you de-allocate
too soon). Unmanaged also usually implies there are no

run-time checks for common errors such as null-pointer


dereferencing or array bounds overflow.
Strictly speaking, most dynamically-typed languages
such as Perl, Python, PHP and Ruby are alsomanaged
code. However, they are not commonly described as such,
which shows that managed code is actually somewhat of a
marketing term for the really big, serious, commercial
programming environments (.NET and Java).
Assembly code: This term generally refers to the kind of
source code people write when they really want to write
byte-code. An assembler is a program that turns this
source code into real byte-code. It is not acompiler
because the transformation is 1-to-1. However, the term
is ambiguous as to what kind of byte-code is used: it could
be managed or unmanaged. If it is unmanaged, the
resulting byte-code is machine code. If it is managed, it
results in the byte-code used behind-the-scenes by a
virtual environment such as .NET. Managed code (e.g. C#,
Java) is compiled into this special byte-code language,
which in the case of .NET is called Common Intermediate
Language (CIL) and in Java is called Java byte-code. There
is usually little need for the common programmer to access
this code or to write in this language directly, but when
people do, they often refer to it as assembly code because
they use an assembler to turn it into byte-code.

Namespaces in C#
Namespaces allow you to create a system to organize your
code. A good way to organize your namespaces is via a
hierarchical system.
Introduction
This article bring in you to C# Namespaces. In this article,
my objectives are as follows:

To give you the picture of what Namespace is?

Learn how to implement the "using" directive?


Learn to use "alias" directives.
Understand what are namespace members?
Need for Namespaces

Namespaces allow you to create a system to organize your


code. A good way to organize your namespaces is via a
hierarchical system. You put the more general names at
the top of the hierarchy and get more specific as you go
down. This hierarchical system can be represented by
nested namespaces. By placing code in different subnamespaces, you can keep your code organized.

The example:
namespace arun.CSharp.Namespaces
{
public class Hello
{
public string GetMessage()
{
return "Hello, world";
}
}
}

Namespaces
are
hierarchical,
and
the
name
arun.CSharp.Namespaces is actually shorthand for
defining a namespace named arun that contains a
namespace named CSharp that itself contains a
namespace named Namespaces, as in:

namespace arun

{
namespace CSharp
{
namespace Namespaces
{....}
}
}

What is an Assembly
An assembly is a file that is automatically generated by
the compiler upon successful compilation of every .NET
application. It can be either a Dynamic Link Library or an
executable file. It is generated only once for an
application and upon each subsequent compilation the
assembly gets updated. However, a basic knowledge
about this topic will help you to understand the
architecture behind a .NET application.

An Assembly contains Intermediate Language (IL) code,


which is similar to Java byte code. In the .NET language,
it consists of metadata. Metadata enumerates the
features of every "type" inside the assembly or the
binary. In addition to metadata, assemblies also have a
special file called Manifest. It contains information about
the current version of the assembly and other related
information.

In .NET, there are two kinds of assemblies, such as


Single file and Multi file. A single file assembly contains
all the required information (IL, Metadata, and Manifest)
in a single package. The majority of assemblies in .NET
are made up of single file assemblies. Multi file
assemblies are composed of numerous .NET binaries or
modules and are generated for larger applications. One of
the assemblies will contain a manifest and others will
have IL and Metadata instructions.

Global assembly cache

GAC, global assembly cache is an area of memory


reserved to store the assemblies of all .NET applications
that are running on a certain machine.

It shares assemblies among multiple .NET applications.


The assemblies must have a strong name and must be
publicly shared to be installed in the GAC.

Passing Value-Type Parameters


A value-type variable contains its data directly as opposed
to a reference-type variable, which contains a reference to
its data. Passing a value-type variable to a method by
value means passing a copy of the variable to the method.
Any changes to the parameter that take place inside the
method have no effect on the original data stored in the
argument variable. If you want the called method to
change the value of the parameter, you must pass it by
reference, using the ref or out keyword. For simplicity, the
following examples use ref.

INDEXER

An indexer allows an object to be indexed like an array.


When you define an indexer for a class, this class behaves
like a virtual array. You can then access the instance of
this class using the array access operator ([ ]).
Simplify the interface

Boxing/ UnBoxing
C# allows us to convert a Value Type to a Reference Type,
and back again to Value Types . The operation of
Converting a Value Type to a Reference Type is called
Boxing and the reverse operation is called Unboxing.

Boxing

1: int Val = 1;
2: Object Obj = Val; //Boxing

The first line we created a Value Type Val and assigned a


value to Val. The second line , we created an instance of
Object Obj and assign the value of Val to Obj. From the
above operation (Object Obj = i ) we saw converting a
value of a Value Type into a value of a corresponding
Reference Type . These types of operation is called Boxing.

UnBoxing

1: int Val = 1;
2: Object Obj = Val; //Boxing
3: int i = (int)Obj; //Unboxing

The first two line shows how to Box a Value Type . The
next line (int i = (int) Obj) shows extracts the Value Type
from the Object . That is converting a value of a Reference
Type into a value of a Value Type. This operation is called
UnBoxing.

Boxing and UnBoxing are computationally expensive


processes. When a value type is boxed, an entirely new
object must be allocated and constructed , also the cast
required for UnBoxing is also expensive computationally.

What Is DLL
A DLL file is a resource within the Windows operating
system that contains code and data related to the
functionality of one or more applications. These files,
which may have the file extension .dll or other file
extensions, have been a major building block for the
Windows operating system and Windows programs since
the early MS-DOS versions of Microsofts computer

technology. Successive versions of Windows have


illustrated certain problems with the use of DLL files for
many different programs.

Anonymous method
Anonymous method is an inline un-named method in the
code.
>> It is created using the delegate keyword end doesnt
require name end return type and
Hence we can consider an anonymous method has only
body without name end return type.
Syntax:
delegate ( [argnfo] )
{ //statement(s) }

What is Lambda Expression?


A Lambda Expression is an anonymous function that we
can use to create a delegate or expression tree types.
Create a local functions that passes as an argument
Syntax:
(Input parameter) => (expression)

ADO.NET Connected & Disconnected Architecture


ADO.NET is a middleware technology used to connect
front end and back end of .NET applications.
ASP.NET is used to create front end of applications using
a code behind language like C#.NET.
Most of the applications require database as back end to
store application information.
To connect front end and back end ADO.NET is used.
It contains two types of architectures.
1. Connected architecture.
2. Disconnected architecture.
Two namespaces required to work with ADO.NET. They
are
System.Data.SqlClient
System.Data
SqlConnection class is used to create connection object
containing server, database and authentication details in
connection string.
SqlCommand class is used to create command object
which stores sql query which can be executed.

ADO.NET Connected architecture


In ADO.NET Connected architecture connection to the
database has to be in opened state to access the
database.
In Connected architecture connection string is used to
connect to the database.
Connection should be opened and closed.
Commands are executed using methods like
Method
ExecuteNonQuery
ExecuteReader
ExecuteScalar

Commands
Return type
Insert,Update,Delete,... int
Select
SqlDataReader
Aggregate commands
Object

ExecuteNonQuery method is used to execute


commands and return number of rows effected.
ExecuteReader method is used to execute command
that retreives data and stores it in SqlDataReader which
is capable of reading data row by row. SqlDataReader is
read only and it reads in sequential order only.
Read( ) method of SqlDataReader is used to read data
row by row and it returns bool value.
ExecuteScalar method is used to execute sql command
that returns one value as its return type is object.

If ExecuteScalar is used to execute select query that


retreives table data. It can return only first row first
column cell value.

ADO.NET Disconnected architecture


In ADO.NET disconnected architecture data is retrieved
from database and stored in dataset.
Data in dataset can be accessed even when the
connection to the database is closed.
SqlDataAdapter is used to open the connection ,execute
the command, store data in dataset and close the
connection.
DataSet is capable of storing more than one table.
DataSet is used to store data on client side.
da.fill(ds);
The above line of code is used to execute the command
by opening the connection to store data in dataset and
closing the connection.

Optimistic locking
As the name suggests optimistic it assumes that
multiple transaction will work without affecting each
other. In other words no locks are enforced while doing
optimistic locking. The transaction just verifies that no
other transaction has modified the data. In case of
modification the transaction is rolled back.
How does optimistic lock work?
You can implement optimistic locking by numerous ways
but the fundamental to implement optimistic locking
remains same. Its a 5 step process as shown below: Record the current timestamp.
Start changing the values.
Before updating check whether anyone else has
changed the values by checking the old time stamp and
new time stamp.
If its not equal rollbacks or else commit.

What are the different solutions by which we can


implement optimistic locking?
There are 3 primary ways by which we can implement
optimistic locking in .NET: Datasets: - Dataset by default implement optimistic
locking. They do a check of old values and new values
before updating.
Timestamp Data type: - Create a timestamp data type
in your table and while updating check if old timestamp is
equal to new timestamp.

Check old and new value: - Fetch the values, do the


changes and while doing the final updates check if the old
value and current values in database are equal. If they
are not equal then rollback or else commits the values.
Pessimistic locking
We can do pessimistic locking by specifying IsolationLevel in
SQL Server stored procedures, ADO.NET level or by using
transaction scope object.
What kind of locks can be acquired by using pessimistic locking?
There are 4 kinds of locks you can acquire Shared, Exclusive,
Update and intent. The first two are actual locks while the other
two
are hybrid locks and marker.

When to use?

Reads Writes
Allowed Allowed

Shared lock

When you want only to read


and you do not want any other Yes
transactions to do update.

No

Exclusive

When you want to modify data


and you do not want anyone to
No
read the transaction, neither
you want anyone to update.

No

Update lock

This is a hybrid lock. This lock


is used when you want to do
update operation which passes
through multiple phases before
the actual update happens. It
first starts with shared lock in
the read phase and then on the
actual update it acquires an
exclusive lock.

Read phase

Yes

No

Manipulating phase

Yes

No

Update phas

No

No

Intent lock is for lock hierarchy.


This lock is used when you
want to lock resources down in
Intent Lock (
the hierarchy. For example a
Demand
NA
shared intent lock on a table
locks)
means shared locks are placed
on pages and rows with the
table.

NA

Schema locks

When you are changing table


structure.

No

Bulk update
locks

Used when you are doing bulk


updates

Table
Table
level No level No

No

Anda mungkin juga menyukai