Dot Net Unit - 1
Dot Net Unit - 1
.Net Framework is a software development platform developed by Microsoft for building and running Windows
applications. The .Net framework consists of developer tools, programming languages, and libraries to build
desktop and web applications. It is also used to build websites, web services, and games.
The .Net framework was meant to create applications, which would run on the Windows Platform. The first version
of the .Net framework was released in the year 2002. The version was called .Net framework 1.0. The Microsoft
.Net framework has come a long way since then, and the current version is .Net Framework 4.7.2.
The Microsoft .Net framework can be used to create both - Form-based and Web-based applications. Web
services can also be developed using the .Net framework.
The framework also supports various programming languages such as Visual Basic and C#. So developers can
choose and select the language to develop the required application.
.Net Framework Architecture is a programming model for the .Net platform that provides an execution
environment and integration with various programming languages for simple development and deployment of
various Windows and desktop applications. It consists of class libraries and reusable components.
.NET Components
The "Common Language Infrastructure" or CLI is a platform in .Net architecture on which the .Net programs are
executed.
• Exception Handling - Exceptions are errors which occur when the application is executed.
o If an application tries to open a file on the local machine, but the file is not present.
o If the application tries to fetch some records from a database, but the connection to the database is
not valid.
• Garbage Collection - Garbage collection is the process of removing unwanted resources when they are no
longer required.
o A File handle which is no longer required. If the application has finished all operations on a file, then
the file handle may no longer be required.
o The database connection is no longer required. If the application has finished all operations on a
database, then the database connection may no longer be required.
• Working with Various programming languages –
1. Language - The first level is the programming language itself, the most common ones are VB.Net and C#.
2. Compiler – There is a compiler which will be separate for each programming language. So underlying the
VB.Net language, there will be a separate VB.Net compiler. Similarly, for C#, you will have another
compiler.
3. Common Language Interpreter – This is the final layer in .Net which would be used to run a .net program
developed in any programming language. So the subsequent compiler will send the program to the CLI
layer to run the .Net application.
2. Class Library
The .NET Framework includes a set of standard class libraries. A class library is a collection of methods and
functions that can be used for the core purpose.
For example, there is a class library with methods to handle all file-level operations. So there is a method which can
be used to read the text from a file. Similarly, there is a method to write text to a file.
Most of the methods are split into either the System.* or Microsoft.* namespaces. (The asterisk * just means a
reference to all of the methods that fall under the System or Microsoft namespace)
A namespace is a logical separation of methods. We will learn these namespaces more in detail in the subsequent
chapters.
3. Languages
The types of applications that can be built in the .Net framework is classified broadly into the following categories.
• WinForms – This is used for developing Forms-based applications, which would run on an end user
machine. Notepad is an example of a client-based application.
• ASP.Net – This is used for developing web-based applications, which are made to run on any browser such
as Internet Explorer, Chrome or Firefox.
o The Web application would be processed on a server, which would have Internet Information
Services Installed.
o Internet Information Services or IIS is a Microsoft component which is used to execute
an Asp.Net application.
o The result of the execution is then sent to the client machines, and the output is shown in the
browser.
• ADO.Net – This technology is used to develop applications to interact with Databases such as Oracle or
Microsoft SQL Server.
Microsoft always ensures that .Net frameworks are in compliance with all the supported Windows operating
systems.
Now in this .Net Architecture tutorial, we will learn the design priciples of .Net framework. The following design
principles of the .Net framework is what makes it very relevant to create .Net based applications.
1. Interoperability - The .Net framework provides a lot of backward support. Suppose if you had an
application built on an older version of the .Net framework, say 2.0. And if you tried to run the same
application on a machine which had the higher version of the .Net framework, say 3.5. The application
would still work. This is because with every release, Microsoft ensures that older framework versions gel
well with the latest version.
2. Portability- Applications built on the .Net framework can be made to work on any Windows platform. And
now in recent times, Microsoft is also envisioning to make Microsoft products work on other platforms,
such as iOS and Linux.
3. Security - The .NET Framework has a good security mechanism. The inbuilt security mechanism helps in
both validation and verification of applications. Every application can explicitly define their security
mechanism. Each security mechanism is used to grant the user access to the code or to the running program.
4. Memory management - The Common Language runtime does all the work or memory management. The
.Net framework has all the capability to see those resources, which are not used by a running program. It
would then release those resources accordingly. This is done via a program called the "Garbage Collector"
which runs as part of the .Net framework.
The garbage collector runs at regular intervals and keeps on checking which system resources are not
utilized, and frees them accordingly.
5. Simplified deployment - The .Net framework also have tools, which can be used to package applications
built on the .Net framework. These packages can then be distributed to client machines. The packages
would then automatically install the application.
Important Points:
• Visual Studio is the development tool that is used to design and develop the .NET applications. For using
Visual Studio, the user has to first install the .NET framework on the system.
• Windows 8, 8.1 or 10 do not provide a pre-installed version 3.5 or later of .NET Framework. Therefore, a
version higher than 3.5 must be installed either from a Windows installation media or from the Internet on
demand. Windows update will give recommendations to install the .NET framework.
CLR is the basic and Virtual Machine component of the .NET Framework. It is the run-time enviornment in the
.NET Framework that runs the codes and helps in making the development process easier by providing the various
services. Basically, it is responsible for managing the execution of .NET programs regardless of
any .NET programming language. Internally, CLR implements the VES(Virtual Execution System) which is defined
in the Microsoft’s implementation of the CLI(Common Language Infrastructure).
The code that runs under the Common Language Runtime is termed as the Managed Code. In other words, you can
say that CLR provides a managed execution enviornment for the .NET programs by improving the security,
including the cross language integration and a rich set of class libraries etc.
Role of CLR in the execution of a C# program
• Suppose you have written a C# program and save it in a file which is known as the Source Code.
• Language specific compiler compiles the source code into the MSIL(Microsoft Intermediate Language) which
is also know as the CIL(Common Intermediate Language) or IL(Intermediate Language) along with its
metadata. Metadata includes the all the types, actual implementation of each function of the program. MSIL is
machine independent code.
• Now CLR comes into existence. CLR provides the services and runtime environment to the MSIL code.
Internally CLR includes the JIT(Just-In-Time) compiler which converts the MSIL code to machine code which
further executed by CPU. CLR also uses the .NET Framework class libraries. Metadata provides information
about the programming language, environment, version, and class libraries to the CLR by which CLR handles
the MSIL code. As CLR is common so it allows an instance of a class that written in a different language to call
a method of the class which written in another language.
As the word specify, Common means CLR provides a common runtime or execution environment as there are more
than 60 .NET programming languages.
Main components of CLR:
• Common Language Specification (CLS)
• Common Type System (CTS)
• Garbage Collection (GC)
• Just In – Time Compiler (JIT)
Common Language Specification (CLS):
It is responsible for converting the different .NET programming language syntactical rules and regulations into
CLR understandable format. Basically, it provides the Language Interoperability. Language Interoperability means
to provide the execution support to other programming languages also in .NET framework.
Language Interoperability can be achieved in two ways :
i. Managed Code: The MSIL code which is managed by the CLR is known as the Managed Code. For managed
code CLR provides three .NET facilities:
Benefits of CLR:
• It improves the performance by providing a rich interact between programs at run time.
• Enhance portability by removing the need of recompiling a program on any operating system that supports it.
• Security also increases as it analyzes the MSIL instructions whether they are safe or unsafe. Also, the use of
delegates in place of function pointers enhance the type safety and security.
• Support automatic memory management with the help of Garbage Collector.
• Provides cross-language integration because CTS inside CLR provides a common standard that activates the
different languages to extend and share each other’s libraries.
• Provides support to use the components that developed in other .NET programming languages.
• Provide language, platform, and architecture independence.
• It allows easy creation of scalable and multithreaded applications, as the developer has no need to think about
the memory management and security issues.
2. Runtime process.
1. The Common Language Runtime (CLR) includes a JIT compiler for converting MSIL to native code.
2. The JIT Compiler in CLR converts the MSIL code into native machine code that is then executed by the OS.
3. During the runtime of a program, the "Just in Time" (JIT) compiler of the Common Language Runtime
(CLR) uses the Metadata and converts Microsoft Intermediate Language (MSIL) into native code.
BYTE CODE (MSIL + META DATA) ----- Just-In-Time (JIT) compiler------> NATIVE CODE
Notes:
When you compile a C# application or any application written in a CLS-compliant language, the application is
compiled into MSIL. This MSIL is then further compiled into native CPU instructions when the application is
executed for the first time by the CLR. (Actually, only the called functions are compiled the first time they are
invoked.)
6. Cross-language compatibility:-
Cross-language compatibility means .NET components can interact with each other, irrespective of the languages
that they are written in. An application written in VB.NET can reference a DLL file written in C# or a C#
application can refer to a resource written in VC++ etc. This allows the reusability of code and improves the
efficiency of the development process.
8. Cross-platform support:-
Any windows platform that supports CLR can execute the .NET application, i.e., the .NET application enables
interoperability between multiple windows operating system.
1. Security is comparatively lesser and many familiar things can reveal as reverse engineering of codes is easy.
2. Newer versions of frameworks are not at all pre-installed in any version of windows.
3. In the older versions of windows, the framework may not be pre-installed. Hence it should be checked first
and if not, you should follow the guidelines in the user manual.
4. Time consumption may be more in some cases due to regularly occurring garbage collection for reclaiming
memory.
5. Limited object-relational (OR) support as it comes only with entity framework.
6. Does not come with multi-platform support from Microsoft, and is not available right after installing visual
studio.
7. Involves a vendor lock-in, and future development solely dependent on Microsoft.
8. Migrating applications to .NET can be expensive.
// =============================================================
In the above MSIL, there are opcodes that are one or two bytes long. The base class declarations from which all
other classes are inherited are contained in the mscorlib.dll. In the method Main(), the instruction ldstr loads the
string “AKSU” on the stack. Then the static System.Console.Writeline function is called and the string is popped
from the stack. Finally, the ret instruction signals the end of the function call.
Then the .ctor() statement implies a default constructor without parameters for the class Demo. This constructor is
automatically created by the compiler for the non-static class Demo. The call instruction passes the base object
constructor and the ret instruction signals the end of the function call.
.Net Assembly
The .NET assembly is the standard for components developed with the Microsoft.NET. Dot NET assemblies may
or may not be executable, i.e., they might exist as the executable (.exe) file or dynamic link library (DLL) file. All
the .NET assemblies contain the definition of types, versioning information for the type, meta-data, and manifest.
The designers of .NET have worked a lot on the component (assembly) resolution.
An Assembly is a basic building block of .Net Framework applications. It is basically a compiled code that can be
executed by the CLR. An assembly is a collection of types and resources that are built to work together and form a
logical unit of functionality. An Assembly can be a DLL or exe depending upon the project that we choose.
1. Private Assembly
2. Shared Assembly
1. Private Assembly
It is an assembly that is being used by a single application only. Suppose we have a project in which we refer to a
DLL so when we build that project that DLL will be copied to the bin folder of our project. That DLL becomes a
private assembly within our project. Generally, the DLLs that are meant for a specific project are private
assemblies.
2. Shared Assembly
Assemblies that can be used in more than one project are known to be a shared assembly. Shared assemblies are
generally installed in the GAC. Assemblies that are installed in the GAC are made available to all the .Net
applications on that machine.
.NET Framework Class Library (FCL)
The Framework Class Library or FCL provides the system functionality in the .NET Framework as it has various
classes, data types, interfaces, etc. to perform multiple functions and build different types of applications such as
desktop applications, web applications, mobile applications, etc. The Framework Class Library is integrated with
the Common Language Runtime (CLR) of the .NET framework and is used by all the .NET languages such as C#,
F#, Visual Basic .NET, etc.
Categories in the Framework Class Library
The functionality of the Framework Class Library can be broadly divided into three categories i.e utility features
written in .NET, wrappers around the OS functionality and frameworks. These categories are not rigidly defined
and there are many classes that may fit into more than one category.
The Accessibility namespace is a part of the managed wrapper for the COM
Accessibility accessibility interface.
The System namespace has base classes for definition of interfaces, data types,
System events, event handlers, attributes, processing exceptions etc.
The System.Data namespace accesses and manages data from various sources
System.Data using different classes.
The System.IO namespaces support IO like data read/write into streams, data
System.IO compression, communicate using named pipes etc. using various types.
The System.Media namespace handles sound files and accessing the sounds
System.Media provided by the system using various classes.
The System.Security namespace has the .NET security system and permissions.
System.Security Child namespaces provide authentication, crytographic services etc.
XamlGeneratedNames The XamlGeneratedNamespace has compiler-generated types that are not used
pace directly from the code.
1. Marking Phase: A list of all the live objects is created during the
marking phase. This is done by following the references from all the
root objects. All of the objects that are not on the list of live objects
are potentially deleted from the heap memory.
2. Relocating Phase: The references of all the objects that were on the list of all the live objects are updated in the
relocating phase so that they point to the new location where the objects will be relocated to in the compacting
phase.
3. Compacting Phase: The heap gets compacted in the compacting phase as the space occupied by the dead
objects is released and the live objects remaining are moved. All the live objects that remain after the garbage
collection are moved towards the older end of the heap memory in their original order.
Metadata in .NET
Metadata is information about the assemblies, modules, and types that constitute .NET programs.
In .NET, metadata is a common mechanism that the .NET runtime, compilers, and tools all can use. Microsoft
.NET uses metadata to describe all types that are used and exposed by the particular .NET assembly.
In this sense, metadata describes an assembly in detail, including descriptions of its identity, the types that it
references, the type that it exports, and the security requirements for execution. Much richer than a type library,
metadata includes descriptions of an assembly and modules, classes, interfaces, methods, properties, fields, events,
global methods, and so forth.
Metadata provides enough information for any runtime, tool, or program to find out literally everything that is
needed for component integration.
Users of metadata in .NET
Following are the users that make intelligent use of metadata in .NET
1) CLR
The CLR uses metadata for verification, security enforcement, memory layout, and execution. The CLR relief
heavily on metadata to support these runtime features.
2) Classloader
A component of the CLR, the class loader uses metadata to find load .NET classes. This is because metadata
records detailed information for a specific class and where the class is located, whether it is in the same assembly,
within or outside of a specific namespace, or in a dependent assembly somewhere on the network.
4) Tools
Tools use metadata to support integration. Tools such as debuggers, profilers, and object browser can use metadata
to provide richer development support.
Namespaces
Namespaces are used to organize the classes. It helps to control the scope of methods and classes in
larger .Net programming projects. In simpler words you can say that it provides a way to keep one set of
names(like class names) different from other sets of names. The biggest advantage of using namespace is that the
class names which are declared in one namespace will not clash with the same class names declared in another
namespace. It is also referred as named group of classes having common features. The members of a namespace
can be namespaces, interfaces, structures, and delegates.
Defining a Namespace
To define a namespace in C#, we will use the namespace keyword followed by the name of the namespace and
curly braces containing the body of the namespace as follows:
Syntax:
namespace name_of_namespace {
}
Example:
// defining the namespace name1
namespace name1
{
// C1 is the class in the namespace name1
class C1
{
// class code
}
}
Accessing the Members of Namespace
The members of a namespace are accessed by using dot(.) operator. A class in C# is fully known by its respective
namespace.
Syntax:
[namespace_name].[member_name]
Note:
• Two classes with the same name can be created inside 2 different namespaces in a single program.
• Inside a namespace, no two classes can have the same name.
• In C#, the full name of the class starts from its namespace name followed by dot(.) operator and the class
name, which is termed as the fully qualified name of the class.
Example:
// C# program to illustrate the
// use of namespaces
// namespace declaration
namespace first {
}
}
class Geeks_1
{
} */
}
}
Output:
Hello Geeks!
In the above example:
• In System.Console.WriteLine()” “System” is a namespace in which we have a class named “Console” whose
method is “WriteLine()“.
• It is not necessary to keep each class in C# within Namespace but we do it to organize our code well.
• Here “.” is the delimiter used to separate the class name from the namespace and function name from the
classname.
The using keyword
It is not actually practical to call the function or class(or you can say members of a namespace) every time by
using its fully qualified name. In the above example, System.Console.WriteLine(“Hello
Geeks!”); and first.Geeks_1.display(); are the fully qualified name. So C# provides a keyword “using” which
help the user to avoid writing fully qualified names again and again. The user just has to mention the namespace
name at the starting of the program and then he can easily avoid the use of fully qualified names.
Syntax:
using [namespace_name][.][sub-namespace_name];
In the above syntax, dot(.) is used to include subnamespace names in the program.
Example:
// predefined namespace name
using System;
// user-defined namespace name
using name1
// namespace declaration
namespace first {
}
}
// Class declaration
class Geeks_2
{
// Main Method
public static void Main(String []args)
{
}
}
Output:
Hello Geeks!
So here is a more or less full list of various types of application that we can develop on .NET.
•ASP.Net Web applications are programs that used to run inside some web server to fulfill the user requests over
the http. ASP.NET Web applications can range from simple Web sites that consist of HTML pages to advanced
enterprise applications that run on local and remote networks. These enterprise applications also provide
components for exchanging data using XML. This type includes dynamic and data driven browser based
applications. (Ex: Hotmail and Google).
•Web services are “web callable” functionality available via industry standards like HTTP, XML and SOAP.
•Windows applications are form based standard Windows desktop applications for common day to day tasks. (Ex:
Microsoft word). Run only under Windows environment. These applications consume the services provided by the
Windows operating system.
•Windows services are long-running executable applications that run on the system as a background process. These
applications do not interfere with the working of the other processes that run on the same computer. Windows
services execute within separate Windows sessions created specifically for each Windows service. These services
do not have a graphic user interface and are ideal for running on the server. Windows services were earlier called
NT services.
•Console applications are light weight programs run inside the command prompt (DOS) window. They are
commonly used for test applications.
•Mobile applications can run on multiple mobile devices, such as Pocket PCs, mobile phones, or personal digital
assistants. These applications provide ubiquitous access to data from mobile devices. The .NET Framework
automatically makes changes to these applications to enable them to run on multiple browsers, depending on the
mobile device.
•Class libraries are components that you create once and reuse a number of times in multiple applications. Class
libraries allow you to define several classes, along with their methods and interfaces, in one file. These libraries
compile to .dll files and facilitate rapid development of new applications because of reusability of code. To access
the functionality of the classes in a class library from your application, you need to include a reference to that
library in your program.
All types of .NET applications use one or more .NET compliant languages for their design and development. The
.NET Framework includes various technologies, such as ASP.NET, VB.NET, VC++.NET, and ADO.NET. You use
ASP.NET to build Web applications and services, VB.NET and VC++.NET to create Windows applications, and
ADO.NET for flexible access to databases.