VB Dot Net Material Unit I Part1.Docx
VB Dot Net Material Unit I Part1.Docx
Net Programming
Unit-I
Overview
.NET is a software framework which is designed and developed by Microsoft. The first version of the
.Net framework was 1.0 which came in the year 2002. In easy words, it is a virtual machine for
compiling and executing programs written in different languages like C#, VB.Net etc.
It is used to develop Form-based applications, Web-based applications, and Web services. There is a
variety of programming languages available on the .Net platform, VB.Net and C# being the most
common ones. It is used to build applications for Windows, phone, web, etc. It provides a lot of
functionalities and also supports industry standards.
NET Framework is a technology that supports building and running Windows apps and web services.
.NET Framework is designed to fulfil the following objectives:
This framework contains a large number of class libraries known as Framework Class Library (FCL).
The software programs written in .NET are executed in the execution environment, which is called
CLR (Common Language Runtime). These are the core and essential parts of the .NET framework.
This framework provides various services like memory management, networking, security, and
type-safety.
The .Net Framework supports more than 60 programming languages such as C#, F#, VB.NET, J#,
VC++, JScript.NET, APL, COBOL, Perl, Oberon, ML, Pascal, Eiffel, Smalltalk, Python, Cobra, ADA, etc.
Following is the .NET framework Stack that shows the modules and components of the Framework.
The .NET Framework is composed of four main components:
It is a program execution engine that loads and executes the program. It converts the program into
native code. It acts as an interface between the framework and operating system. It does exception
handling, memory management, and garbage collection. Moreover, it provides security, type-safety,
interoperability, and portability. A list of CLR components are given below:
Common Type System (CTS)
In Microsoft's .NET Framework, the Common Type System (CTS) is a standard that specifies how type
definitions and specific values of types are represented in computer memory. It is intended to allow
programs written in different programming languages to easily share information.
Role of CTS:
Reference types' objects are represented by a reference to the object's actual value; a reference here
is similar to a pointer in C/C++. It simply refers to a memory location where the objects' values are.
This has a profound impact on how these types are used. If you assign a reference type to a variable
and then pass that variable into a method, for instance, any changes to the object will be reflected
on the main object; there is no copying.
Value types are the opposite, where the objects are represented by their values. If you assign a value
type to a variable, you are essentially copying a value of the object.
To enable full interoperability scenarios, all objects that are created in code must rely on some
commonality in the languages that are consuming them (are their callers). Since there are numerous
different languages, .NET has specified those commonalities in something called the Common
Language Specification (CLS). CLS defines a set of features that are needed by many common
applications. It also provides a sort of recipe for any language that is implemented on top of .NET on
what it needs to support.
CLS is a subset of the CTS. This means that all of the rules in the CTS also apply to the CLS, unless the
CLS rules are more strict. If a component is built using only the rules in the CLS, that is, it exposes
only the CLS features in its API, it is said to be CLS-compliant. For instance, the <framework-librares>
are CLS-compliant precisely because they need to work across all of the languages that are supported
on .NET.
It is a standard library that is a collection of thousands of classes and used to build an application.
The BCL (Base Class Library) is the core of the FCL and provides basic functionalities.
WinForms
Windows Forms is a smart client technology for the .NET Framework, a set of managed libraries that
simplify common application tasks such as reading and writing to the file system.
ASP.NET
ASP.NET is a web framework designed and developed by Microsoft. It is used to develop websites,
web applications, and web services. It provides a fantastic integration of HTML, CSS, and JavaScript. It
was first released in January 2002.
ADO.NET
Windows Presentation Foundation (WPF) is a graphical subsystem by Microsoft for rendering user
interfaces in Windows-based applications. WPF, previously known as "Avalon", was initially released
as part of .NET Framework 3.0 in 2006. WPF uses DirectX.
It is a framework for building service-oriented applications. Using WCF, you can send data as
asynchronous messages from one service endpoint to another.
WF (Workflow Foundation)
Windows Workflow Foundation (WF) is a Microsoft technology that provides an API, an in-process
workflow engine, and a rehostable designer to implement long-running processes as workflows
within .NET applications.
It is a query language, introduced in .NET 3.5 framework. It is used to make the query for data
sources with C# or Visual Basics programming languages.
Entity Framework
It is an ORM based open source framework which is used to work with a database using .NET objects.
It eliminates a lot of developer’s effort to handle the database. It is Microsoft's recommended
technology to deal with the database.
Parallel LINQ
Parallel LINQ or PLINQ is a parallel implementation of LINQ to objects. It combines the simplicity and
readability of LINQ and provides the power of parallel programming.
Microsoft Intermediate Language (MSIL)
MSIL stands for Microsoft Intermediate Language. We can call it as Intermediate Language (IL) or
Common Intermediate Language (CIL). During the compile time, the compiler converts the source
code into Microsoft Intermediate Language (MSIL). Microsoft Intermediate Language (MSIL) is a
CPU-independent set of instructions that can be efficiently converted to the native code. During the
runtime the Common Language Runtime (CLR)'s Just In Time (JIT) compiler converts the Microsoft
Intermediate Language (MSIL) code into native code to the Operating System.
When a compiler produces Microsoft Intermediate Language (MSIL), it also produces Metadata. The
Microsoft Intermediate Language (MSIL) and Metadata are contained in a portable executable (PE)
file. Microsoft Intermediate Language (MSIL) includes instructions for loading, storing, initializing,
and calling methods on objects, as well as instructions for arithmetic and logical operations, control
flow, direct memory access, exception handling, and other operations
The Portable Executable (PE) format is a file format for executables, object code, and DLLs, used in
32-bit and 64-bit versions of Windows operating systems.
The .Net languages, which is conforms to the Common Language Specification (CLS), uses its
corresponding runtime to run the application on different Operating Systems. During the code
execution time, the Managed Code compiled only when it is needed, that is it converts the
appropriate instructions to the native code for execution just before when each function is called.
This process is called Just In Time (JIT) compilation, also known as Dynamic Translation.
Metadata format
Manifest
Managed Code in Microsoft .Net Framework, is the code that has executed by the Common
Language Runtime (CLR) environment. On the other hand, Unmanaged Code is directly executed by
the computer's CPU. Data types, error-handling mechanisms, creation and destruction rules, and
design guidelines vary between managed and unmanaged object models.
The benefits of Managed Code include programmer’s convenience and enhanced security. Managed
code is designed to be more reliable and robust than unmanaged code, examples are Garbage
Collection, Type Safety etc. The Managed Code running in a Common Language Runtime (CLR)
cannot be accessed outside the runtime environment as well as cannot call directly from outside the
runtime environment.
● It improves the security of the application like when you use runtime environment, it
automatically checks the memory buffers to guard against buffer overflow.
● It implements the garbage collection automatically.
● It also provides runtime type checking/dynamic type checking.
● It also provides reference checking which means it checks whether the reference point to the
valid object or not and also check they are not duplicate.
The main disadvantage of managed language is that you are not allowed to allocate memory directly,
or you cannot get the low-level access of the CPU architecture.
Memory Management
The Microsoft .NET common language runtime requires that all resources be allocated from the
managed heap. Objects are automatically freed when they are no longer needed by the application.
When a process is initialized, the runtime reserves a contiguous region of address space that initially
has no storage allocated for it. This address space region is the managed heap. The heap also
maintains a pointer. This pointer indicates where the next object is to be allocated within the heap.
Initially, the pointer is set to the base address of the reserved address space region.
An application creates an object using the new operator. This operator first ensures that the bytes
required by the new object fit in the reserved region (committing storage if necessary). If the object
fits, then the pointer points to the object in the heap, this object's constructor is called, and the new
operator returns the address of the object.
Example
The preceding figure shows a managed heap consisting of three objects: A, B, and C. The next object
to be allocated will be placed where NextObjPtr points (immediately after object C).
When an application calls the new operator to create an object, there may not be enough address
space left in the region to allocate to the object. The heap detects this by adding the size of the new
object to NextObjPtr. If NextObjPtr is beyond the end of the address space region, then the heap is
full and a collection must be performed.
The .Net Framework provides a new mechanism for releasing unreferenced objects from the
memory (that is we no longer needed that objects in the program), this process is called Garbage
Collection (GC). When a program creates an Object, the Object takes up the memory. Later when the
program has no more references to that Object, the Object's memory becomes unreachable, but it is
not immediately freed.
Garbage collector generally doesn’t take an object as Garbage if it implements Finalize method.
During the process of garbage collection, it first looks for the object finalization from metadata. If the
object has implemented Finalize (), garbage collector doesn’t make this object as unreachable, but it
is assigned to as Reachable and a reference of it is placed to the Finalization queue.
.Net Assembly
The .NET assembly is the standard for components developed with the Microsoft.NET. Dot NET
assemblies may or may not be executable.
An assembly can be a single file or it may consist of the multiple files. In case of multi-file, there is
one master module containing the manifest while other assemblies exist as non-manifest modules. A
module in .NET is a sub part of a multi-file .NET assembly. Assembly is one of the most interesting
and extremely useful areas of .NET architecture along with reflections and attributes.
.Net Assembly is a logical unit of code, that contains code which the Common Language Runtime
(CLR) executes. It is the smallest unit of deployment of a .net application and it can be a .dll or an
.exe . Assembly is really a collection of types and resource information that are built to work together
and form a logical unit of functionality. It includes both executable application files that you can run
directly from Windows without the need for any other programs (.exe files), and libraries (.dll files)
for use by other applications.
Types of Assemblies
1. Private Assembly
2. Shared Assembly
3. Satellite Assembly
Private Assembly
Private assembly requires us to copy separately in all application folders where we want to use that
assembly’s functionalities; without copying we cannot access the private assembly features and
power. Private assembly means every time we have one, we exclusively copy into the BIN folder of
each application folder.
Public Assembly
Public assembly is not required to copy separately into all application folders. Public assembly is also
called Shared Assembly. Only one copy is required in system level, there is no need to copy assembly
into application folder. Public assembly should install in GAC.
Shared assemblies (also called strong named assemblies) are copied to a single location (usually the
Global assembly cache). Each shared assembly has a four-part name including its face name, version,
public key token and culture information. The public key token and version information makes it
almost impossible for two different assemblies with the same name or for two similar assemblies
with different version to mix with each other.
When the assembly is required for more than one project or application, we need to make the
assembly with a strong name and keep it in GAC or in Assembly folder by installing the assembly with
the GACUtil command.
Satellite Assembly
Satellite assemblies are used for deploying language and culture-specific resources for an application
Strong name consists of an Assembly’s identity, that means the Assemblies can be assigned a
cryptographic signature. The strong name guarantees the integrity of the assembly which prevents
someone from taking over the name of the assembly.
Strong Name includes the name of the .net assembly, version number, culture identity, and a public
key token. It is generated from an assembly file using the corresponding private key. Strong names
guarantee name uniqueness by relying on unique key pairs.
The Strong Name tool (Sn.exe) helps sign assemblies with strong names. Sn.exe provides options for
key management, signature generation, and signature verification.
Assembly versioning
The Assembly Version attribute assigns the version number of the assembly, and this is embedded in
the manifest. Version information for an assembly consists of the following four values:
● a major
● a minor version number,
● and two further optional build and revision numbers.
ILDASM
The IL Disassembler is a companion tool to the IL Assembler (Ilasm.exe). Ildasm.exe takes a portable
executable (PE) file that contains intermediate language (IL) code and creates a text file suitable as
input to Ilasm.exe.
1. Firstly open the Visual Studio 2010 Command Prompt and write the command like this.
2. Then a ILDasm window will open. In it select the File menu and click on open menu option.
3. Then go to the location of the assembly and select the assembly to open it.
4. A window will be opened like this which shows the information of the assembly.
In the above window we can see that whatever assembly or DLL we have chosen the ILDASM utility is
showing all the information of the DLL. It is showing what kind of method a DLL is containing, what
kind of data field it is containing, what kind of constructor it is containing and it is also showing the
manifest of the assembly.
.NET Reflection
.NET Framework's Reflection API allows you to fetch Type (Assembly) information at runtime
or programmatically. We can also implement late binding using .NET Reflection. At runtime,
Reflection uses the PE file to read the metadata about an assembly. Reflection enables you
to use code that was not available at compile time. .NET Reflection allows application to
collect information about itself and also manipulate on itself. It can be used effectively to
find all the types in an assembly and/or dynamically invoke methods in an assembly. This
includes information about the type, properties, methods and events of an object. With
reflection, we can dynamically create an instance of a type, bind the type to an existing
object, or get the type from an existing object and invoke its methods or access its fields and
properties. We can also access attributes using Reflection. In short, Reflection can be very
useful if you don't know much about an assembly.
Using reflection, you can get the kind of information that you will see in the Class Viewer,
Object Explorer, or a Class Explorer. You can see all the types in an assembly, their members,
their types, and metadata. Here is an example of the Class View in Visual Studio.
Security in .NET
The common language runtime and the .NET provide many useful classes and services that enable
developers to easily write secure code and enable system administrators to customize the
permissions granted to code so that it can access protected resources. In addition, the runtime and
the .NET provide useful classes and services that facilitate the use of cryptography and role-based
security.
Type-safe code accesses only the memory locations it is authorized to access. For example, type-safe
code cannot read values from another object's private fields. It accesses types only in well-defined,
allowable ways.
Role-Based Security
Roles are often used in financial or business applications to enforce policy. For example, an
application might impose limits on the size of the transaction being processed depending on whether
the user making the request is a member of a specified role.
It supports authorization by making information about the principal, which is constructed from an
associated identity, available to the current thread.
It supports that is flexible and extensible enough to meet the needs of a wide spectrum of
applications.
Cryptography Model
The .NET Framework security system implements an extensible pattern of derived class inheritance.
The hierarchy is as follows:
● Algorithm type class, such as SymmetricAlgorithm, AsymmetricAlgorithm or HashAlgorithm.
This level is abstract.
● Algorithm class that inherits from an algorithm type class; for example, Aes, RC2, or
ECDiffieHellman. This level is abstract.
● Implementation of an algorithm class that inherits from an algorithm class; for example,
AesManaged, RC2CryptoServiceProvider, or ECDiffieHellmanCng. This level is fully
implemented.
Using this pattern of derived classes, it is easy to add a new algorithm or a new implementation of an
existing algorithm.
Different implementations available for an algorithm, consider symmetric algorithms. The base for all
symmetric algorithms is SymmetricAlgorithm, which is inherited by the following algorithms:
● Aes
● DES
● RC2
● Rijndael
● TripleDES