Chapter 1 Introduction To .NET Framework
Chapter 1 Introduction To .NET Framework
NET Framework
.NET framework Architecture
.NET is a software framework which is designed and developed by Microsoft.
.NET is tiered, modular, and hierarchal. Each tier of the .NET Framework is a layer of
abstraction. .NET languages are the top tier and the most abstracted level. The common language
runtime is the bottom tier, the least abstracted, and closest to the native environment. This is
important since the common language runtime works closely with the operating environment to
manage .NET applications. The .NET Framework is partitioned into modules, each with its own
distinct responsibility. Finally, since higher tiers request services only from the lower tiers, .NET
is hierarchal.
It is a tool for developing Windows applications, Web applications and Web services. It provides
environment for developing, deploying and running applications.
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 convert 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.
The execution process that includes the creation of the MSIL and the conversion of the MSIL
into machine code by the JIT compiler is given as follows:
The source code is converted into the MSIL by a language-specific compiler in the compile time
of the CLR. Also, along with the MSIL, metadata is also produced in the compilation. The
metadata contains information such as the definition and signature of the types in the code,
runtime information, etc.
A Common Language Infrastructure (CLI) assembly is created by assembling the MSIL. This
assembly is basically a compiled code library that is used for security, deployment, versioning,
etc. and it is of two types i.e. process assembly (EXE) and library assembly (DLL).
The JIT compiler then converts the Microsoft Intermediate Language (MSIL) into the machine
code that is specific to the computer environment that the JIT compiler runs on. The MSIL is
converted into the machine code on a requirement basis i.e. the JIT compiler compiles the MSIL
as required rather than the whole of it.
The machine code obtained using the JIT compiler is then executed by the processor of the
computer.
CLR internally include the JIT(Just -In-Time) compiler which converts the MSIL code(managed
code) to machine code which further executed by CPU. The CLR also provides additional
services including garbage collection.
Common means CLR provides a common runtime or execution environment as there are more
than 60 .NET programming languages.
The CLR is somewhat comparable to the JVM. But the JVM supports only Java, not multiple
programming languages
Microsoft has defined some specifications that each .Net language has to follow. For e.g. no
pointer, no multiple inheritance etc.
CLS defines a set of rules and restrictions that every language must follow which runs under the
.NET framework
C# has an int data type and VB.NET has Integer data type.
Hence a variable declared as an int in C# and Integer in VB.NET, finally after compilation, uses
the same structure Int32 from CTS.
1. Value Types
These types directly contain their data, and instances of value types are allocated on the stack
which is faster but limited in size.
Value types can be built-in, user-defined, or enumerations.
2. Reference Types
These types store a reference of memory address at which values are stored, and they are
allocated on the heap which is slower but larger. Reference types can be self-describing types,
pointer types or interface types.
Conversion of value type to reference type is called boxing and conversion of reference type to
value type is called unboxing.
int a=10;
object o = (object) a; //boxing
int b = (int) o; //unboxing
Namespaces
A namespace is designed for providing a way to keep one set of names separate from another.
The class names declared in one namespace does not conflict with the same class names declared
in another. The namespace concept is similar to package in java.
Declaring a Namespace
Namespaces are declared using the namespace keyword. The basic syntax for creating a
namespace is very simple, requiring only a name for the new grouping and a code block
surrounded by braces:
namespace namespace-name {}
System.Console.WriteLine("Hello World!");
System is a namespace and Console is a class in that namespace. The using keyword can be used
so that the complete name is not required, as in the following example:
using System;
Console.WriteLine("Hello World!");
In .Net languages every program is created with a default Namespaces. Programmers can also
create their own Namespaces in .Net languages. The .NET framework provides the default
namespace, which is used automatically for any code that is written outside of any namespace
declaration. Namespace are considered as a container which consists of other namespaces,
classes, etc.
Classes
Interfaces
Structures
Delegates
namespace GTU
class Student
class Subject
{
{
Console.WriteLine("This is Subject class");
}
}
using GTU;
using System;
class Result
{
Subject s;
}
Alias for namespace:
Example:
Nested Namespace
Example:
namespace Parent
{
namespace Child
{
namespace Grandchild
{
class Test
{
public void ShowMessage()
{
Console.WriteLine ("This is a nested namespace!");
}
}
}
}
}
Major Namespaces in .NET
System -> The System namespace contains fundamental classes and base classes that define
commonly-used value and reference data types, events and event handlers, interfaces, attributes,
and processing exceptions.
It also contains the types that we felt to be the most fundamental and frequently use
System.Data -> This Namespace is used when we are using Database connectivity
in Ado.NET Application. If any Namespace use "Data" attribute then it will used
for Database connectivity in .NET.
System.Data.sqlClient -> This also used for database connectivity in Ado.NET When we are
use sql query for data connection.
System.Collections -> This Namespace is used when we are using collections (Generic and Non
Generic).
System.Drawing -> This Namespace is used for Graphical Primitive data type such
as size,fonts and printing services etc.
System.Reflection -> This Namespace is used Run type discovery and dynamic creation of
types.
System.web -> This Namespace is used when we are making a web Application in
.NET. Ex. asp.net,XML web services etc.
System.Windows.Forms -> when we are making any GUI based application in .NET then we
use this Namespace otherwise compiler gives error message.
System.xml -> When we are using XML Language in our programs then we will use this
Namespace in our programs.
System.Configuration-> When we configure some .net controls and use it in our .net
application. Then we use this Namespace.Ex. Configuration of Sql Data Source or Sql Data
Adapter and use it for connection.
System.Diagnostics-> Includes classes that allow to debug our application and to step through
our code.
System.Web.Services-> Includes classes that let us build and use Web Services.
Assemblies
MSIL code is stored in a package called Assembly.
Assembly can include any file types like image files, text files etc. along with DLLs or EXEs.
When you compile your source code, the EXE or DLL is generated and it is actually an
assembly.
One assembly may contain one or more files.
Assembly contains four major parts:
(1) Manifest: - Additional information regarding assembly.
(2) Type metadata:-Data about data or structure of classes.
(3) MSIL code: - Intermediate version of our program.
(4) Set of Resource: - Information or resource of other assembly.
What is assembly manifest?
Every assembly file contains information about itself. This information is called as Assembly
Manifest.
Assembly manifest is a data structure which stores information about an assembly.
The information includes version information, list of files packed, and definition of types,
security permissions, version control and metadata.
In the common language runtime (CLR), the garbage collector serves as an automatic memory
manager and runs in low-priority thread. Garbage collection is not deterministic which means it
is not guaranteed when the garbage collector will be called, it will be called when CLR decides
that is needed.
In .Net languages there is a facility that we can call Garbage Collector (GC) explicitly in the
program by calling System.GC.Collect.
Following are the advantage of using Garbage Collector.
Allow us to develop an application without having worry to free memory.
Allocates memory for objects efficiently on the managed heap.
Reclaims the memory for no longer used objects and keeps the free memory for future
allocations.
Provides memory safety by making sure that an object cannot use the content of another
object.
End to DLL Hell – Managed Execution
When we install an application then dll of that application get stored in the registry, then if we
install other application that has same name .dll that means previously installed .dll get overwrite
by the same name new .dll. This is ok for newly installed application but previously installed
application cannot get execute further. This is big problem in context of version of same
application. This is DLL Hell problem.
OR
DLL Hell refers to a set of problems caused when multiple applications attempt to share a
common component like a dynamic link library (DLL). The reason for this issue was that the
version information about the different components of an application was not recorded by the
system.
Solution for END to DLL Hell
This problem of dynamic link library (.dll) is resolved through versioning.
We can do versioning only with shared assembly Applications must be self-describing:
Reflection API
When you compile a C# program, part of the EXE or DLL you generate contains metadata that
tells you the classes that you are EXE or DLL contains. It also tells you about the members of
each class: the fields, properties, methods, delegates, events etc. This information is always
available to your program and other programs, and can be reached through a mechanism known
as 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.
The System.Reflection namespace and System.Type class play an important role in .NET
Reflection. These two works together and allow you to reflect over many other aspects of a type.