0% found this document useful (0 votes)
5 views

c#module1

.NET Framework is a software development framework by Microsoft that provides a runtime environment and libraries for building applications across various platforms. It supports multiple programming languages, allowing developers to write code that is portable and efficient through the use of a common class library and the Common Language Runtime (CLR). The framework includes features like automatic memory management, a robust set of libraries, and tools for developing desktop, web, and mobile applications.

Uploaded by

Shashank S
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

c#module1

.NET Framework is a software development framework by Microsoft that provides a runtime environment and libraries for building applications across various platforms. It supports multiple programming languages, allowing developers to write code that is portable and efficient through the use of a common class library and the Common Language Runtime (CLR). The framework includes features like automatic memory management, a robust set of libraries, and tools for developing desktop, web, and mobile applications.

Uploaded by

Shashank S
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

22ISE451 / C# and .

NET/Module1

Introduction to .NET Framework

The C# Environment:
.NET Framework – An Overview,
Components of .NET ,
Common Language Specification (CLS),
Common Language Runtime (CLR)
Microsoft Intermediate Language ("MSIL" or "IL"),
The Common Type System (CTS),
.NET Framework Base Classes
Object-Oriented Programming concepts: Encapsulation, Polymorphism, Inheritance
1.Introduction to .NET Framework

The .NET Framework is a software development framework developed by


Microsoft that provides a runtime environment and a set of libraries and tools for building
and running applications on Windows operating systems. The framework includes a variety
of programming languages, such as C#, F#, and Visual Basic, and supports a range of
application types, including desktop, web, mobile, and gaming applications.
.NET technology was introduced by Microsoft, to catch the market from the SUN's
Java. Few years back, Microsoft had only VC++ and VB to compete with Java, but Java was
catching the market very fast. With the world depending more and more on the Internet/Web
and java related tools becoming the best choice for the web applications, Microsoft seemed to
be loosing the battle. Thousands of programmers moved to java from VC++ and VB. To
recover the market, Microsoft announced .NET
.NET framework comes with a single class library. And that’s all programmers need to
learn!! Whether they write the code in C# or VB.NET or J#, it doesn't matter, you just use the
.NET class library. There is no classes specific to any language. There is nothing more you can
do in a language, which you can't do in any other .NET language. You can write code in C# or
VB.NET with the same number of lines of code, same performance and same efficiency,
because everyone uses same .NET class library.

1.1 Features of .NET


• It is a platform neutral framework.

• It is a layer between the operating system and the programming language.

PREPARED BY K. THAMARAI SELVI AP/ISE 1


22ISE451 / C# and .NET/Module1

• Provides Multi Language Support (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.)

• .NET provides a common set of class libraries, which can be accessed from any .NET based
programming language. There will not be separate set of classes and libraries for each
language. If you know any one .NET language, you can write code in any .NET language.

• Helps in creating Applications by Integrating Different Languages

1.2 Application development in .NET

1.3 Compilation Process in .NET

Non-DOT NET Program Execution Process:

We know that computers only understand machine-level code. The Machine-level code is also
known as native code or binary code. So when we compile a C, VB6, or C++ program, the
language compiler compiles the language source code. It generates the native machine code
(also called binary code), which can be understood by the underlying operating system and the
system hardware. The above process is shown in the below image.

PREPARED BY K. THAMARAI SELVI AP/ISE 2


22ISE451 / C# and .NET/Module1

The Native code or machine code that is generated by the respective language compiler is
specific to the operating system on which it is generated. If we take this compiled native code
and try to run it on another operating system, then it will fail. So, the problem with this style
of program execution is that it is not portable from one platform to another. That means it is
platform-dependent.

.NET Program Execution Process:

Now, let us understand the .NET Program Execution Process in detail. Using .NET, we
can create different types of applications such as Console, Windows, Web, and Mobile
Applications. Irrespective of the type of application, when we execute any .NET application,
the following things happen in order
The .NET application Source Code gets compiled into Microsoft Intermediate
Language (MSIL), which is also called Intermediate language (IL) or Common Intermediate
language (CIL) code. Both .NET and Non-DOTNET applications generate an assembly when
we compile the application. Generally, the assemblies have an extension of. DLL or .EXE
based on the type of application we compiled. For example, if we compile a Window or
Console application in .NET, we get an assembly of type .EXE, whereas when we compile a
Web or Class Library Project in .NET, we get an assembly of type .DLL.
The difference between a .NET and NON-DOTNET assembly is that .NET Assembly
is an Intermediate Language format, whereas NON-.NET assembly is in native code format.
The NON-.NET applications can run directly on top of the operating system as the
NON-DOTNET assembly contains the native code, whereas .NET applications run on top of a
virtual environment called Common Language Runtime (CLR). CLR contains a component
called Just-In-Time Compiler (JIT) which will convert the Intermediate language into native
code, which can be understood by the underlying operating system.

In .NET, the application execution consists of 2 steps. They are as follows:

In the step1 the respective language compiler compiles the Source Code into Intermediate
Language (IL).

PREPARED BY K. THAMARAI SELVI AP/ISE 3


22ISE451 / C# and .NET/Module1

In the 2nd step, the JIT compiler of CLR will convert the Intermediate Language (IL) code into
native code (Machine Code or Binary Code), which the underlying operating system can then
execute. The above process is shown in the image below.

As the .NET assembly is in Intermediate Language (IL) format and not in native or
machine code, the .NET assemblies are portable to any platform as long as the target platform
has the Common Language Runtime (CLR). The target platform’s CLR converts the
Intermediate Language code into native or machine code that the underlying operating system
can understand.

Intermediate Language code is also called managed code. This is because CLR manages
the code that runs inside it. For example, in a VB6 program, the developer is responsible for
de-allocating the memory consumed by an object. If a programmer forgets to de-allocate
memory, it may get out-of-memory exceptions. On the other hand, a .NET programmer does
not need to worry about de-allocating the memory consumed by an object. Automatic memory
management also known as garbage collection is provided by CLR. Apart from garbage
collection, the CLR provides several other benefits, which we will discuss in a later session.
Since CLR manages and executes the Intermediate Language, it (IL) is also called the managed
code.

.NET supports different programming languages like C#, VB, J#, and C++. C#, VB,
and J# can only generate managed code (IL), whereas C++ can generate both managed code
(IL) and unmanaged code (Native code).

PREPARED BY K. THAMARAI SELVI AP/ISE 4


22ISE451 / C# and .NET/Module1

The native code is not stored permanently anywhere after we close the program the
native code is thrown away. When we execute the program again, the native code is generated
again. The .NET program is similar to the Java program’s execution. In Java, we have
bytecodes and JVM (Java Virtual Machine) whereas in .NET we have Intermediate Language
and CLR (Common Language Runtime).

In the .NET framework, the Code is Compiled Twice.


1. In the 1st compilation, the source code is compiled by the respective language compiler
and generates the intermediate code which is known as MSIL (Microsoft
Intermediate Language) or IL (Intermediate language code) Or Managed Code.
2. In the 2nd compilation, MSIL code is converted into Native code (native code means
code specific to the Operating system so that the code is executed by the Operating
System) and this is done by CLR.
Always 1st compilation is slow and 2nd compilation is fast.

Code Execution Process in .NET

The Code Execution Process involves the following two stages:

1. Compiler time process.


2. Runtime process.
1. Compiler time process
1. The .Net framework has one or more language compilers, such as Visual Basic, C#,
Visual C++, JScript, or one of many third-party compilers such as an Eiffel, Perl, or
COBOL compiler.
2. Anyone of the compilers translates your source code into Microsoft Intermediate
Language (MSIL) code.

PREPARED BY K. THAMARAI SELVI AP/ISE 5


22ISE451 / C# and .NET/Module1

3. For example, if you are using the C# programming language to develop an application,
when you compile the application, the C# language compiler will convert your source
code into Microsoft Intermediate Language (MSIL) code.
4. In short, VB.NET, C#, and other language compilers generate MSIL code. (In other
words, compiling translates your source code into MSIL and generates the required
metadata.)
5. Currently "Microsoft Intermediate Language" (MSIL) code is also known as the
"Intermediate Language" (IL) Code or "Common Intermediate Language" (CIL) Code.

SOURCE CODE -----.NET COMLIPER------> BYTE CODE (MSIL + META DATA)

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

PREPARED BY K. THAMARAI SELVI AP/ISE 6


22ISE451 / C# and .NET/Module1

1.4 Basic Architecture and Component Stack of .NET Framework

The first three components from the bottom are considered as the basic architecture of .Net
framework which came in the year 2005 and after this more components were added by
Microsoft in the .Net Framework as following
1. CLR (Common Language Runtime): It is a run-time environment that executes the code
written in any .NET programming language. .Net framework provides support for many
languages like c#, C++, F#, Cobra, Jscript.Net, VB.Net, Oxygene, etc.
2. FCL (Framework Class Library): A large number of class libraries are present in this
framework which is known as FCL.
3. Types of Applications: Mainly the applications that are built in the .Net framework are
divided into the following three categories:
• WinForms: Form–Based applications are considered under this category. In simple
terms, we can say client-based applications that read and write the file system come under
this category.

PREPARED BY K. THAMARAI SELVI AP/ISE 7


22ISE451 / C# and .NET/Module1

• ASP .NET: Web-based applications come under this category. ASP.Net is a framework
for the web and it provides an awesome integration of HTML, CSS, and JavaScript which
makes it useful to develop web applications, websites, and web services. Web services
were added in .Net Framework 2.0 and considered as a part of ASP.NET web
applications.
• ADO .NET: It includes the applications that are developed to communicate with the
database like MS SQL Server, Oracle, etc. It mainly consists of classes that can be used
to connect, retrieve, insert, and delete data.
4. WPF (Windows Presentation Foundation): Windows Presentation Foundation (WPF)
is a graphical subsystem given by Microsoft that uses DirectX and is used in Windows-
based applications for rendering UI (User Interface).
5. WCF (Windows Communication Foundation): It is a framework for building
connected and service-oriented applications used to transmit data asynchronously from
one service endpoint to another service point.
6. WF (Windows Workflow Foundation): It is a technology given by Microsoft that
provides a platform for building workflows within .Net applications.
7. Card Space: It is a Microsoft .NET Framework software client that is designed to let
users provide their digital identity to online services in a secure, simple, and trusted way.
8. LINQ (Language Integrated Query): It is introduced in .Net framework version 3.5.
Basically, it is a query language used to make the query for data sources with VB or
c# programming languages.
9. Entity Framework: It is an open–source ORM (Object Relational Mapping) based
framework that comes into .Net Framework version 3.5. It enables the .Net developer to
work with a database using .Net objects. Before the entity framework, .Net developers
performed a lot of things related to databases. Like to open a connection to the database,
developers have to create a Data Set to fetch or submit the data to the database, convert
data from the Data Set to .NET objects, or vice-versa. It creates difficulties for developers
and also it was an error-prone process, then “Entity Framework” comes to automate all
these database-related activities for the application. So, Entity Framework allows the
developers to work at a higher level of abstraction.
10. Parallel LINQ (Language Integrated Query): It comes in .Net Framework version 4.0
and is also termed PLINQ. It provides a concurrent query execution engine for LINQ. It
executes the LINQ in parallel such that it tries to use as much processing power the
system on which it is executing.
11. TPL (Task Parallel Library): It is a set of public types and APIs. It allows the
developers to be more productive by simplifying the process of adding concurrency and
parallelism to .Net applications.
12. .NET API For Store/UWP Apps: In 2012, Microsoft added some APIs for
creating UWP(Universal Windows Platform) apps for Windows using C# or VB.
13. Task-Based Asynchronous Model: It is a model used to describe the asynchronous
operations and tasks in the .Net Framework.

PREPARED BY K. THAMARAI SELVI AP/ISE 8


22ISE451 / C# and .NET/Module1

1.5 Common Language Runtime (CLR)


Whenever we create any EXE (i.e. Console Application, Windows Application, Class
Library Project, etc.) or any DLL i.e. Web Application (i.e. ASP.NET MVC, ASP.NET Web
API, ASP.NET, etc.) in .NET Framework using Visual Studio and using any .NET supported
Programming Language such as C#, VB, F#, J#, etc., then these applications are run completely
under the control of CLR (Common Language Runtime).
CLR is the Runtime Engine of .NET Applications

CLR (Common Language Runtime) is a heart of Dot Net Framework.


It is a core runtime environment of .NET Framework for executing applications. The main
function of Common Language Runtime (CLR) is to convert the Managed Code into native
code and then execute the Program. It acts as a layer between Operating Systems and the
applications written in .Net languages

It is a core runtime environment of .NET Framework for executing applications. The


main function of Common Language Runtime (CLR) is to convert the Managed Code into
native code and then execute the Program. It acts as a layer between Operating Systems and
the applications written in .Net languages.

PREPARED BY K. THAMARAI SELVI AP/ISE 9


22ISE451 / C# and .NET/Module1

CLR handles the execution of code and provides useful services for the implementation of the
program. In addition to executing code, CLR provides services such as memory management,
thread management, security management, code verification, compilation, and other system
services.
BASE CLASS LIBRARY SUPPORT

It provides class libraries supports to an application when needed. The Common Language
Runtime provides support for the base class library. The BCL contains multiple libraries that
provide various features such as Collections, I/O, XML, Datatype definitions, etc. for the
multiple .NET programming languages.

CODE MANAGER

• Manager component of CLR in the .NET Framework allocates the necessary memory for
the variables and objects that are to be used by the application.
• CLR manages code. When we compile a .NET application you don't generate code that can
actually execute on your machine. You actually generate Microsoft Intermediate Language
(MSIL or just IL). All .NET code is IL code. IL code is also called Managed Code, because
the .NET Common Language Runtime manages it.

Managed and Unmanaged Code in .NET Framework

The codes which run under the complete control of CLR are called Managed Code in
.NET Framework. These kinds of code (Managed code) are run by the .NET Runtime
Environment i.e. CLR. If the .NET Framework is not installed or if the .NET Runtime
Environment i.e. CLR is not available, then these kinds of codes are not going to be executed.
Skype, PowerPoint, and Microsoft Excel do not require dot net runtime. They run under
their own environment. So, in short, the code (EXE, Web App) that is not run under the control
of CLR is called unmanaged code. CLR will not provide any facilities and features of .NET to
the unmanaged code in C# execution like Language Interoperability, Automatic memory
management, Exception handling mechanism, code access security, etc.

PREPARED BY K. THAMARAI SELVI AP/ISE 10


22ISE451 / C# and .NET/Module1

Note: Managed code is the code that is managed by the CLR (Common Language Runtime) in
.NET Framework. Whereas Unmanaged code is the code that is directly executed by the
operating system.
TYPE CHECKER
Type checker will verify types used in the application with CTS or CLS standards supported
by CLR, this provides type safety.
DEBUG ENGINE:
CLR allows us to perform debugging an application during runtime. Find and remove bugs in
an application
CLASS LOADER
Various modules, resources, assemblies, etc. are loaded by the CLR loader. Also, this loader
loads the modules on demand if they are actually required so that the program initialization
time is faster and the resources consumed are lesser. It is used to load all classes at run time.

EXCEPTION MANAGER
It handles the exception at runtime to avoid application failure. The exception manager in the
CLR handles the exceptions regardless of the .NET Language that created them. For a
particular application, the catch block of the exceptions is executed in case they occur and if
there is no catch block then the application is terminated.

THREAD SUPPORT
The CLR provides thread support for managing the parallel execution of multiple threads.
The System. Threading class is used as the base class for this.
Threading means parallel code execution. Threads are basically light weight processes
responsible for multi-tasking within a single application.
SECURITY ENGINE

PREPARED BY K. THAMARAI SELVI AP/ISE 11


22ISE451 / C# and .NET/Module1

It enforces security permissions at code level security, folder level security, and machine level
security using Dot Net Framework setting and tools provided by Dot Net.
Restrict access to system resources like memory (DB secure)
COM MARSHALLER:
Communication with the COM (Component Object Model) component in the .NET
application is provided using the COM marshaller. This provides the COM interoperability
support

GARBAGE COLLECTOR
Garbage Collector handles automatic memory management and it will release memory of
unused objects in an application, this provides automatic memory management. It releases the
unused memory and allocates it to a new application.
Garbage Collection in .NET Framework
When a dot net application runs, lots of objects are created. At a given point in time, it is
possible that the application does not use some of those objects. The Garbage Collector in the
.NET Framework is nothing but a Small Routine, or you can say it’s a Background Process
Thread that runs periodically and tries to identify what objects are not being used currently by
the application and de-allocates the memory of those objects.
So, Garbage Collector is nothing but a feature provided by CLR that helps us clean or destroy
unused managed objects. Cleaning or destroying those unused managed objects basically
reclaims the memory.
Garbage Collection (GC) in the .NET Framework is an automatic memory management system
that helps manage the allocation and release of memory in your applications. In .NET, when
we create an object using the new keyword, it automatically allocates memory on the managed
heap. You don’t need to allocate or deallocate memory explicitly as you might have to in
languages like C or C++.

• If the system has low physical memory, then garbage collection is necessary.
• If the memory allocated to various objects in the heap memory exceeds a pre-set
threshold, then garbage collection occurs.
• If the GC.Collect method is called, then garbage collection occurs. However, this method
is only called under unusual situations as normally garbage collector runs automatically.

Note: The Garbage Collector will destroy only the unused managed objects. It does not
clean unmanaged objects.

PREPARED BY K. THAMARAI SELVI AP/ISE 12


22ISE451 / C# and .NET/Module1

JUST-IN-TIME COMPILER (JIT)


The JIT compiler in the CLR converts the Microsoft Intermediate Language (MSIL)
into the machine code that is specific to the computer environment that the JIT compiler runs
on. The compiled MSIL is stored so that it is available for subsequent calls if required
.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:
• 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).

• 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.

• 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.

PREPARED BY K. THAMARAI SELVI AP/ISE 13


22ISE451 / C# and .NET/Module1

• 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. This JIT compiler is obsolete starting from dotnet
2.0

Conclusion : The CLR provides a number of services that include:

• Loading and execution of codes

• Memory isolation for application

• Verification of type safety

• Compilation of IL into native executable code

• Providing metadata

• Automatic garbage collection

• Enforcement of Security

• Interoperability with other systems

• Managing exceptions and errors

PREPARED BY K. THAMARAI SELVI AP/ISE 14


22ISE451 / C# and .NET/Module1

• Provide support for debugging and profiling

1.6 COMMON TYPE SYSTEM (CTS)


The .NET Framework supports many programming languages such as C#, VB.NET, J#, F#,
etc. Every programming language has its own data type. One programming language data type
cannot be understood by other programming languages. But, there can be situations where we
need to communicate between two different programming languages.
For example, we need to write code in the VB.NET language and that code may be
called from C# language. In order to ensure smooth communication between these languages,
the most important thing is that they should have a Common Type System (CTS) which ensures
that data types defined in two different languages get compiled to a common data type.
CLR in .NET Framework will execute all programming language’s data types. This is
possible because CLR has its own data types which are common to all programming languages.
At the time of compilation, all language-specific data types are converted into CLR’s data type.
This data type system of CLR is common to all .NET Supported Programming languages and
this is known as the Common Type System (CTS) in .NET Framework.

Example to Understand Common Type System in .NET Framework

Let us understand Common Type System (CTS) in .NET Framework with an example. What
we are going to do is, we will create two applications. One Application uses the C# Language
and the other one is using VB.NET Language. And then we will try to see the IL code of both
of these applications and then we will try to see what the CTS looks like.
Here we are going to create two class library projects. One class library project uses the
C#.NET language and the other class library project uses the VB.NET language

Creating C# Class Library Project:


Create a class library project with the name CsharpClassLibrary and use
the C# programming language. Once you create the C# class library project then add a class
file with the name Calculator.cs and then copy and paste the following code into it.

PREPARED BY K. THAMARAI SELVI AP/ISE 15


22ISE451 / C# and .NET/Module1

namespace CsharpClassLibrary
{
public class Calculator
{
public int Calculate()
{
int a = 10, b = 20;
int c = a + b;
return c;
}
}
}

Creating VB Class Library Project:


To the same CsharpClassLibrary solution, let us add another class library project with the name
as VBClassLibrary but using VB as the programming language. Once you created the VB
Class library project then add a class file with the name Calculator.cs to this project and copy
and paste the following code into it.

Public Class Calculator


Public Function Calculate() As Integer
Dim a As Integer = 10
Dim b As Integer = 10
Dim c As Integer
c=a+b
Return c
End Function
End Class

Now, let us open the IL code of the Calculate method of both the class library project as shown
in the below image. Please have a look at the integer variable in the IL code which is int32 in
this case. In the C# class library project, we use int as the data type to declare variables a, b,
and c whereas in the VB class library project, we use Integer as the data type to declare the
variables a, b, and c. At the end of the day, both the data types are compiled to a common data
type i.e. int32.

PREPARED BY K. THAMARAI SELVI AP/ISE 16


22ISE451 / C# and .NET/Module1

So, whether we write the code in VB.NET or in C#.NET, if it follows the dot net rules
or specifications, at the end of the day it is compiled into a Common Type System (CTS) in
.NET Framework which is common for all .NET Supported Programming Languages. This
Common Type System is Provided by CLR and is the same for all .NET Supported
programming languages. While compiling the source code by the respective language
compiler, they will convert the language-specific data type to CLR-specific data type. For
example, int in C# and Integer in VB is converted to Int32 after compilation and Int32 is the
data type which is understood by CLR. Or you can say Int32 is the CLR-specific data type.
Applications of CTS:

• Language Integration
• Code of one language can be inherited by code of other language
• Better performance
• Type safe

There are 2 Types of CTS that every .NET programming language have:
1. Value Types:
Value is passed. Value Types will store the value directly into the memory location. These
types work with stack mechanisms only. CLR allows memory for these at Compile Time.
2. Reference Types: Reference Types will contain a memory address of value because the
reference types won’t store the variable value directly in memory. These types work with
Heap mechanism. CLR allot memory for these at Runtime.

1.7 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 Language
Interoperability. Language Interoperability means providing execution support to other

PREPARED BY K. THAMARAI SELVI AP/ISE 17


22ISE451 / C# and .NET/Module1

programming languages also in .NET framework. CLS enables cross-language integration


or Interoperability.
CLS (Common Language Specification) is a part of CLR in the .NET Framework. The
.NET Framework supports many programming languages such as C#, VB.NET, J#, F#, etc.
Every programming language has its own syntactical rules for writing the code which is known
as a language specification.
One programming language’s syntactical rules (language specification) cannot be
understood by other programming languages. But there can be situations where we need to
communicate between two different programming languages. In order to ensure smooth
communication between different .NET Supported Programming Languages, the most
important thing is that they should have Common Language Specifications which ensures that
language specifications defined in two different languages get compiled into a Common
Language Specification

CLR in .NET Framework will execute all programming language’s code. This is possible
because CLR has its own language specification (syntactical rules) which are common to all
.NET Supported Programming Languages. At the time of compilation, every language
compiler should follow this language specification of CLR and generate the MSIL code. This
language specification of CLR is common for all programming languages and this is known as
Common Language Specifications (CLS).

Example to understand CLS in .NET Framework:


As we know C# is a case-sensitive language whereas VB is not a case-sensitive language.
That means in C#, you can declare the same member name multiple times with case
differences but it is not possible in VB. So, let us create two class library projects. One uses
C# language and the other one uses the VB Programming Language. Then we will try to
consume the C# class library project in the VB class library project.
Creating C# Class Library Project:
Create a class library project with the name CsharpClassLibrary using the C# programming
language. Once you create the C# class library project then add a class file with the
name Calculator.cs and then copy and paste the following code into it. As you can see we
have created two methods with the same name but with case differences. One method starts
with the capital C while the other one starts with a small c.

namespace CsharpClassLibrary
{
public class Calculator
{
public int Calculate()
{
int a = 10, b = 20;

PREPARED BY K. THAMARAI SELVI AP/ISE 18


22ISE451 / C# and .NET/Module1

int c = a + b;
return c;
}
public int calculate()
{
int a = 10, b = 20;
int c = a + b;
return c;
}
}
}

Creating VB Class Library Project:


To the same CsharpClassLibrary solution, let us add another class library project with the
name as VBClassLibrary but using VB as the programming language. Here, in this project we
want to use the C# class library project, so first add a reference to
the CsharpClassLibrary project. Then create a class with the name TestClass.cs and copy-
paste the following code into it.
Imports CsharpClassLibrary
Public Class TestClass
Public Sub TestMethod()
Dim obj As New Calculator()
obj.Calculate()
End Sub
End Class

Now, when you try to build the VB Class Library project, you will get the below error. This
is because VB is not case-sensitive and it found two methods with the same name. That
means we are violating the Common Language Specifications in the C# code.
CLS Compliant Attribute
Before starting with any concrete program/example on CLS, I will explain a useful attribute
that needs to be included while writing CLS Compliant Code.

Syntax : [assembly: CLSCompliant(true)]

If you set CLSCompliant attribute to true, it gives a compile time warning if you write any
public or protected member which is not CLSCompliant. It will give a warning for only
public and protected members, not for private, as private members can be accessed only
within that class.

PREPARED BY K. THAMARAI SELVI AP/ISE 19


22ISE451 / C# and .NET/Module1

1.8 BASE CLASS LIBRARY (BCL)/ FRAMEWORK CLASS LIBRARY


(FCL)

The terms “Base Class Library” (BCL) and “Framework Class Library” (FCL) are often used
in the context of software development, particularly in the context of .NET programming. They
refer to collections of reusable classes, types, and components that provide common
functionality for developing applications.

Base Class Library (BCL): The Base Class Library is a set of fundamental classes and types
that are part of a programming platform. These classes provide essential functionality such as
basic data types, exception handling, input/output operations, string manipulation, and more. In
the context of .NET, the BCL includes core namespaces
like System, System.Collections, System.IO, System.Text, and so on. These classes form the
foundation upon which more specialized functionality can be built.

Base Class Libraries The Base class library in the .NET Framework is huge. It covers areas
such as:
1. Collection: The System. Collections namespaes provides numerous collection classes.
2. Thread Support: The System. Threading namespace provides support for creating fast,
efficient, multi-threaded appliction.
3. Code Generation: The System.CodeDOM namespace provides classes for generating
source files in numerous language. ASP.NET uses these classes when converting
ASP.NET pages into classes, which are subsequently compiled.
4. IO: The System.IO provides extensive support for working with files and all other
stream types.
5. Reflection: The System. Reflection namespace provides support for load assemblies,
examining the type with in assemblies, creating instances of types, etc.
6. Security: The System.Security namespace provides support for services such as
authentication, authorization, permission sets, policies, and cryptography. These base
services are used by application development technologies like ASP.NET to build their
security infrastructure.

Framework Class Library (FCL):

The Framework Class Library is an extended collection of classes, types, and components
built on top of the Base Class Library. It provides higher-level functionality that is specific
to certain application domains or industries. The FCL includes classes for GUI (Graphical
User Interface) development, web development, database access, networking, and more.

PREPARED BY K. THAMARAI SELVI AP/ISE 20


22ISE451 / C# and .NET/Module1

Namespaces in the Framework Class Library are a group of related classes and interfaces
that can be used by all the .NET framework languages. Some of the namespaces in the FCL
along with their description is given as follows:

Namespaces Description
System It includes all common datatypes, string
values, arrays and methods for data
conversion.
System.Data, System.Data.Common, These are used to access a database, perform
System.Data.OleDb, System.Data.SqlClient, commands on a database and retrieve
System.Data.SqlTypes database.
System.IO, System.DirectoryServices, These are used to access, read and write files.
System.IO.IsolatedStorage
System.Diagnostics It is used to debug and trace the execution of
an application.
System.Net, System.Net.Sockets These are used to communicate over the
Internet when creating peer-to-peer
applications.
System.Windows.Forms, These namespaces are used to create
System.Windows.Forms.Design Windows-based applications using Windows
user interface components.
System.Web, System.WebCaching, These are used to create ASP. NET Web
System.Web.UI, System.Web.UI.Design, applications that run over the web.
System.Web.UI.WebControls

PREPARED BY K. THAMARAI SELVI AP/ISE 21


22ISE451 / C# and .NET/Module1

System.Security, These are used for authentication,


System.Security.Permissions, authorization, and encryption purpose
System.Security.Policy,
System.WebSecurity,
System.Security.Cryptography

1.9 CIL OR MSIL | MICROSOFT INTERMEDIATE LANGUAGE OR


COMMON INTERMEDIATE LANGUAGE

The Microsoft Intermediate Language (MSIL), also known as the Common Intermediate
Language (CIL) is a set of instructions that are platform independent and are generated by
the language-specific compiler from the source code. The MSIL is platform independent and
consequently, it can be executed on any of the Common Language Infrastructure supported
environments such as the Windows .NET runtime.

The MSIL is converted into a particular computer environment specific machine code by the
JLT compiler. This is done before the MSIL can be executed. Also, 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

Execution process in Common Language Runtime (CLR): 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:

PREPARED BY K. THAMARAI SELVI AP/ISE 22


22ISE451 / C# and .NET/Module1

• 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.

Evaluation Stack
Evaluation stack is used to hold the local variable or the method argument before they are
evaluated. Instructions that copy values from memory to the evaluation stack are called Load,
and instructions that copy values from stack back to memory are called Store. All the Opcodes
starting with ld are used for loading the item on the stack, and the Opcodes starting with st are
used for storing the item in memory.

PREPARED BY K. THAMARAI SELVI AP/ISE 23


22ISE451 / C# and .NET/Module1

At the beginning of the function, it is required to provide the maximum items that would be
present on that stack at any particular time. This is done using the .maxstack directive. If not
provided, it will be default to 8.

Example: The MSIL is generated by the language specific compiler from the source code
given below. To understand the MSIL in detail, simple C# source code with the class Demo
that prints “Welcome” is given as follows:
using System;

public class Demo {


public static void Main()
{
Console.WriteLine("Welcome");
}
}
The MSIL that is created by the C# compiler for the code provided above is given as
follows:

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 “Welcome” 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.

PREPARED BY K. THAMARAI SELVI AP/ISE 24


22ISE451 / C# and .NET/Module1

• CIL Directives, Tokens, and Attributes


In the above code, we notice some names (CIL Tokens) with the .. prefix,
e.g. .assembly, .namespace, .class, .method, .ctor, .override. These are called CIL
Directives. The tokens that are used along CIL Directive and describe how the CIL Directive
should be processed are called CIL Attributes.
• CIL Opcodes
Operation codes are tokens that are used to build the type's implementation logic. This is the
area where we are going to focus in our remaining article.
CIL Opcodes are actually binary codes but have corresponding friendly mnemonic (e.g. the
friendly name for 0x01 is "Break") to assist developer in understanding, debugging, and
writing code directly in intermediate language. Below are examples of some Opcodes:
OpcodeInstruction
0x00 Nop
0x01 Break
0x02 ldarg.0
0x73 newobj
• CIL Code Labels
The tokens like IL_000, IL_001, etc. are called CIL Code Labels. These are just optional
labels that can be replaced with any text of your choice.

• The nop instruction is simply a debug build artifact and are used to allow to put breakpoint on
the curly braces.
• The ldstr instruction load the string on the stack. In this case, it's the "Hello World!" string
value.
• Next, the call opcode calls the base class constructor.
• Finally, the ret opcode exits a method and return a value to the caller (if any).

PREPARED BY K. THAMARAI SELVI AP/ISE 25

You might also like