Unit 1 1
Unit 1 1
The main objective of this framework is to develop an application that can run on
the windows platform. The current version of the .Net framework is 4.8.
Note: The .NET Framework is not only a language, but it is also a software and language
neutral platform.
Components of .NET Framework
There are following components of .NET Framework:
6. .NET Assemblies
8. Window Services
It is an important part of a .NET framework that works like a virtual component of the
.NET Framework to executes the different languages program like c#, Visual Basic, etc.
A CLR also helps to convert a source code into the byte code, and this byte code is
known as CIL (Common Intermediate Language) or MSIL (Microsoft Intermediate
Language). After converting into a byte code, a CLR uses a JIT compiler at run time
that helps to convert a CIL or MSIL code into the machine or native code.
It specifies a standard that represent what type of data and value can be defined and
managed in computer memory at runtime. A CTS ensures that programming data
defined in various languages should beinteract with each other to share information.
For example, in C# we define data type as int, while in VB.NET we define integer as a
data type.
The base class library has a rich collection of libraries features and functions that help
to implement many programming languages in the .NET Framework, such as C #, F #,
Visual C ++, and more. Furthermore, BCL divides into two parts:
4. In DLL file, there is no main method, whereas exe file has main
method.
It is a subset of common type system (CTS) that defines a set of rules and regulations
which should be followed by every language that comes under the .net framework. In
other words, a CLS language should be cross-language integration or interoperability.
For example, in C# and VB.NET language, the C# language terminate each statement
with semicolon, whereas in VB.NET it is not end with semicolon, and when these
statements execute in .NET Framework, it provides a common platform to interact and
share information with each other.
A .NET assembly is the main building block of the .NET Framework. It is a small unit of
code that contains a logical compiled code in the Common Language infrastructure
(CLI), which is used for deployment, security and versioning. It defines in two parts
(process) DLL and library (exe) assemblies. When the .NET program is compiled, it
generates a metadata with Microsoft Intermediate Language, which is stored in a file
called Assembly.
It provides the various system functionality in the .NET Framework, that includes
classes, interfaces and data types, etc. to create multiple functions and different types
of application such as desktop, web, mobile application, etc. In other words, it can be
defined as, it provides a base on which various applications, controls and components
are built in .NET Framework.
1. Object type
4. Garbage collection
4. Application domains
8. LINQ
10. Interoperability
2. The second version 2.0 of .net framework was launched on 22 January 2006.
11. .Net framework 4.6.2 version was announced on March 30, 2016
13. .Net framework 4.7.1 version was announced on October 17, 2017
15. And currently we are using .Net framework version 4.8 that was released on 18
April 2019
What is Just-In-Time(JIT) Compiler in .NET
Last Updated : 14 Nov, 2019
Just-In-Time compiler(JIT) is a part of Common Language Runtime (CLR) in .NET which is responsible for
managing the execution of .NET programs regardless of any .NET programming language. A language-specific
compiler converts the source code to the intermediate language. This intermediate language is then converted
into the machine code by the Just-In-Time (JIT) compiler. This machine code is specific to the computer
environment that the JIT compiler runs on.
Working of JIT Compiler: The JIT compiler is required to speed up the code execution and provide support
for multiple platforms. Its working is given as follows:
The JIT compiler converts the Microsoft Intermediate Language(MSIL) or Common Intermediate
Language(CIL) into the machine code. This is done before the MSIL or CIL can be executed. The MSIL is
converted into machine code on a requirement basis i.e. the JIT compiler compiles the MSIL or CIL as
required rather than the whole of it. The compiled MSIL or CIL is stored so that it is available for subsequent
calls if required.
Types of Just-In-Time Compiler: There are 3 types of JIT compilers which are as follows:
1. Pre-JIT Compiler: All the source code is compiled into the machine code at the same time in a single
compilation cycle using the Pre-JIT Compiler. This compilation process is performed at application
deployment time. And this compiler is always implemented in the Ngen.exe (Native Image Generator).
2. Normal JIT Compiler: The source code methods that are required at run-time are compiled into machine
code the first time they are called by the Normal JIT Compiler. After that, they are stored in the cache and
used whenever they are called again.
3. Econo JIT Compiler: The source code methods that are required at run-time are compiled into machine
code by the Econo JIT Compiler. After these methods are not required anymore, they are removed.
Advantages of JIT Compiler:
The JIT compiler requires less memory usage as only the methods that are required at run-time are
compiled into machine code by the JIT Compiler.
Page faults are reduced by using the JIT compiler as the methods required together are most probably in the
same memory page.
Code optimization based on statistical analysis can be performed by the JIT compiler while the code is
running.
Disadvantages of JIT compiler:
The JIT compiler requires more startup time while the application is executed initially.
The cache memory is heavily used by the JIT compiler to store the source code methods that are required at
run-time.
1.0 1.0
1.1 1.1
2.0 2.0
2.0 3.0
2.0 3.5
4 4
Below diagram illustrate how CLR is associated with the operating system/hardware along with the class
libraries. Here, the runtime is actually CLR.
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.
Main Components of CLR
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:
The inherits attribute identifies the class created in the code-behind file
from which this .aspx file will derive. One major point of Code Behind is
that the code for all the Web pages is compiled into a DLL file that allows
the web pages to be hosted free from any Inline Server Code.
Abstraction - The abstraction is simplifying complex reality by modeling classes appropriate to the problem.
Polymorphism - The polymorphism is the process of using an operator or function in different ways for
different data input.
Encapsulation - The encapsulation hides the implementation details of a class from other objects.
Inheritance - The inheritance is a way to form new classes using classes that have already been defined.
1. Class
A class is a collection of objects and represents description of objects that share same attributes and actions.
A class describes in abstract all of the characteristics and behaviour of a type of object.
Once instantiated, an object is generated that has all of the methods, properties and other behaviour defined within
the class.
A class should not be confused with an object. The class is the abstract concept for an object that is created at design -
time by the programmer. The objects based upon the class are the concrete instances of the class that occur at run-
time. For example, the Car class will define that all cars have a make, model and colour.
using System;
namespace oops
{
//class definition
public class SimpleHelloWorld
{
//Entry point of the program
static void Main(string[] args)
{
//print Hello world"
Console.WriteLine("Hello World!");
}
}
}
2. Objects
Objects are basic building blocks of a C# OOP program. An object is a combination of data and methods. The data
and the methods are called members of an object. In an OOP program, we create objects. These objects communicate
together through methods. Each object can receive messages, send messages and process data.
There are two steps in creating an object. First, we define a class. A class is a template for an object. It is a blueprint
which describes the state and behavior that the objects of the class all share. A class can be used to create many
objects. Objects created at runtime from a class are called instances of that particular class.
For example, A “Bike” usually has common elements such as bike color, engine, mileage etc. In OOP terminology
these would be called as a Class Properties or Attributes of a Bike object. Here is the example of Object:
3. Method
Methods are functions defined inside the body of a class. They are used to perform operations with the attributes of
our objects. Methods bring modularity to our programs.
Methods are essential in the encapsulation concept of the OOP paradigm. For example, we might have a Connect()
method in our AccessDatabase class. We need not to be informed how exactly the method Connect() connects to the
database. We only have to know that it is used to connect to a database. This is essential in dividing responsibilities
in programming, especially in large applications.
using System;
namespace oops
{
class customer
{
// Member Variables
public int CustID;
public string Name;
public string Address;
Now, to access cutomer class, we need create the object of the cutomer class and then access it Methods and objects.
class customer
{
// class members code
//Entry point
static void Main(string[] args)
{
// object instantiation
customer obj = new customer();
//Method calling
obj.displayData();
//fields calling
Console.WriteLine(obj.CustID);
Console.WriteLine(obj.Name);
Console.WriteLine(obj.Address);
}
4. Encapsulation
Encapsulation is implemented
class. the process ofby
keeping
using access
or enclosing
specifiers.
one orAnmore
access
items
specifier
withindefines
a singlethe
physical
scope and
or logical
visibility
package.
of a In
Encapsulation is the mechanism that binds together the code and the data it manipulates,
and keeps both safe from outside interference and misuse. In OOP, code and data may be
combined in such a way that a self-contained box is created. When code and data are
linked together in this way, an object is created and encapsulation exists.
Within an object, code, data or both may be private or public to that object. Private code is
known to and accessible only by another part of the object, that is private code or data may
not be accessible by a piece of the program that exists outside the object. When the code
and data is public, other portions of your program may access it even though it is defined
within an object.
return formula;
Example – [Encapsulation]
public class Bike
{
return mileage;
return color;
}
}
{
Bike objBike = new Bike();
Output: https://dotnetfiddle.net/scAgvm
Bike mileage is : 65
Bike color is : Black
5. Abstraction
C# allows both classes and functions to be declared abstract using the abstract keyword.
You can't create an instance of an abstract class. An abstract member has a signature but
no function body and they must be overridden in any non-abstract derived class. Abstract
classes exist primarily for inheritance. Member functions, properties and indexers can be
abstract. A class with one or more abstract members must be abstract as well. Static
members can't be abstract.
In this example, we are declaring an abstract class Employess with a method displayData()
that does not have an implementation. Then we are implementing the displayData() body
in the derived class. One point to be noted here is that we have to prefixe the abstract
method with the override keyword in the derived class.
using System;
namespace oops
{
//abstract class
public abstract class Employess
{
//abstract method with no implementation
public abstract void displayData();
}
//derived class
public class test : Employess
{
//abstract class method implementation
public override void displayData()
{
Console.WriteLine("Abstract class method");
}
}
class abstractClass
{
static void Main(string[] args)
{
// class instance
new test().displayData();
}
}
}
6. Inheritance
The inheritance is a way to form new classes using classes that have already been defined. The newly formed classes
are called derived classes, the classes that we derive from are called base classes. Important benefits of inheritance
are code reuse and reduction of complexity of a program. The derived classes (descendants) override or extend the
functionality of the base classes (ancestors).
using System;
namespace oops
{
//Base Class
public class Father
{
public void FatherMethod()
{
Console.WriteLine("this property belong to Father");
}
}
//Derived class
public class Child : Father
{
public void ChildMethod()
{
Console.WriteLine("this property belong to Child");
}
}
class Inheritance
{
//Entry point
static void Main(string[] args)
{
7. Polymorphism
Polymorphism is a Greek word, meaning "one name many forms". In other words, one object has many forms or has one
name with multiple functionalities. "Poly" means many and "morph" means forms. Polymorphism provides the ability to a
class to have multiple implementations with the same name. It is one of the core principles of Object Oriented
Programming after encapsulation and inheritance. In this article, you'll learn what polymorphism is, how it works, and how
to implement polymorphism in C#.
Types of Polymorphism
There are two types of polymorphism in C#:
Here C# compiler checks the number of parameters passed and the type of parameter and make the decision of which
method to call and it throw an error if no matching method is found.
In the following example, a class has two methods with the same name "Add" but with different input parameters (the first
method has three parameters and the second method has two parameters).
Method overriding can be done using inheritance. With method overriding it is possible for the base class and derived
class to have the same method name and same something. The compiler would not be aware of the method available for
overriding the functionality, so the compiler does not throw an error at compile time. The compiler will decide which
method to call at runtime and if no method is found then it throws an error.
The compiler requires an Area() method and it compiles successfully but the right version of the Area() method is not
being determined at compile time but determined at runtime. Finally the overriding methods must have the same name
and signature (number of parameters and type), as the virtual or abstract method defined in the base class method and
that it is overriding in the derived class.
1.7 event-driven programming
After you design the look of your ASP.NET page using ASP.NET controls,
you need to add the application logic. ASP.NET introduces the concept
of an event-driven programming. It enables you to write code that
executes in response to events raised by particular user actions. Events
can be related to the page or the controls on the page. For example,
you can write code to make your application do something when the
page loads or a button is clicked.
RadioButtonList