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

Unit 1 C#

The document discusses the .NET Common Language Runtime (CLR). CLR is a runtime environment that manages and executes code written in any .NET language. It compiles code into an intermediate language called MSIL. A just-in-time compiler then converts MSIL into native machine code for execution.

Uploaded by

chauhanjeel57
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)
22 views

Unit 1 C#

The document discusses the .NET Common Language Runtime (CLR). CLR is a runtime environment that manages and executes code written in any .NET language. It compiles code into an intermediate language called MSIL. A just-in-time compiler then converts MSIL into native machine code for execution.

Uploaded by

chauhanjeel57
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/ 11

 .

NET Common Language Runtime (CLR)

➢ .NET CLR is a runtime environment that manages and executes the code written in
any .NET programming language.
➢ CLR is the virtual machine component of the .NET framework. That language's
compiler compiles the source code of applications developed using .NET compliant
languages into CLR's intermediate language called MSIL, i.e., Microsoft
intermediate language code.
➢ This code is platform-independent. It is comparable to byte code in java. Metadata
is also generated during compilation and MSIL code and stored in a file known as
the Manifest file.
➢ This metadata is generally about members and types required by CLR to execute
MSIL code.
➢ A just-in-time compiler component of CLR converts MSIL code into native code of
the machine.
➢ This code is platform-dependent. CLR manages memory, threads, exceptions, code
execution, code safety, verification, and compilation.

• The main components of CLR are:


o Common type system Metadata and Assemblies
o Common language speciation
o Garbage Collector
o Just in Time Compiler

 Common type system:

➢ CTS provides guidelines for declaring, using, and managing data types at runtime.
It offers cross-language communication. For example, VB.NET has an integer data
type, and C# has an int data type for managing integers. After compilation, Int32
is used by both data types. So, CTS provides the data types using managed code.
A common type system helps in writing language-independent code.

 Common Language Specification (CLS):

➢ Common Language Specification (CLS) contains a set of rules to be followed by all


NET-supported languages. The common rules make it easy to implement language
integration and help in cross-language inheritance and debugging. Each language
supported by NET Framework has its own syntax rules. But CLS ensures
interoperability among applications developed using NET languages.
 Garbage Collection:

➢ Garbage Collector is a component of CLR that works as an automatic memory


manager. It helps manage memory by automatically allocating memory according
to the requirement. It allocates heap memory to objects. When objects are not in
use, it reclaims the memory allocated to them for future use. It also ensures the
safety of objects by not allowing one object to use the content of another object.

 Just in Time (JIT) Compiler:

➢ JIT Compiler is an important component of CLR. It converts the MSIL code into
native code (i.e., machine-specific code). The .NET program is compiled either
explicitly or implicitly. The developer or programmer calls a particular compiler to
compile the program in the explicit compilation. In implicit compilation, the
program is compiled twice. The source code is compiled into Microsoft
Intermediate Language (MSIL) during the first compilation process. The MSIL code
is converted into native code in the second compilation process. This process is
called JIT compilation. There are three types of JIT compilers -Pre, Econo, and
Normal. Pre JIT Compiler compiles entire MSIL code into native code before
execution. Econo JIT Compiler compiles only those parts of MSIL code required
during execution and removes those parts that are not required anymore. Normal
JIT Compiler also compiles only those parts of MSIL code required during execution
but places them in cache for future use. It does not require recompilations of
already used parts as they have been placed in cache memory.

 Metadata:

➢ A Metadata is a binary information about the program, either stored in a CLR


Portable Executable file (PE) along with MSIL code or in the memory. During the
execution of MSIL, metadata is also loaded into memory for proper interpretation
of classes and related. Information used in code. So, metadata helps implement
code in a language-neutral manner or achieve language interoperability.

 Assemblies:

➢ An assembly is a fundamental unit of physical code grouping. It consists of the


assembly manifest, metadata, MSIL code, and a set of resources like image files. It
is also considered a basic deployment unit, version control, reuse, security
permissions, etc.
 .NET CLR Functions
o It converts the program into native code.
o Handles Exceptions
o Provides type-safety
o Memory management
o Provides security
o Improved performance
o Language independent
o Platform independent
o Garbage collection
o Provides language features such as inheritance, interfaces, and overloading for object-
oriented programs.

➢ The code that runs with CLR is called managed code, whereas the code outside the
CLR is called unmanaged code.
➢ The CLR also provides an Interoperability layer, which allows both the managed
and unmanaged codes to interoperate.
• .NET Framework
➢ .NET is a framework to develop software applications. It is designed and developed
by Microsoft and the first beta version released in 2000.
➢ It is used to develop applications for web, Windows, phone. Moreover, it provides
a broad range of functionalities and support.
➢ This framework contains a large number of class libraries known as Framework
Class Library (FCL). The software programs written in .NET are executed in the
execution environment, which is called CLR (Common Language Runtime). These
are the core and essential parts of the .NET framework.
➢ This framework provides various services like memory management, networking,
security, memory management, and type-safety.
➢ The .Net Framework supports more than 60 programming languages such as C#,
F#, VB.NET, J#, VC++, JScript.NET, APL, COBOL, Perl, Oberon, ML, Pascal, Eiffel,
Smalltalk, Python, Cobra, ADA, etc.
➢ Following is the .NET framework Stack that shows the modules and components
of the Framework.

• The .NET Framework is composed of four main components:

1. Common Language Runtime (CLR)


2. Framework Class Library (FCL),
3. Core Languages (WinForms, ASP.NET, and ADO.NET), and
4. Other Modules (WCF, WPF, WF, Card Space, LINQ, Entity Framework, Parallel LINQ,
Task Parallel Library, etc.)
 .NET Framework Class Library (FCL)

➢ .NET Framework Class Library is the collection of classes, namespaces, interfaces


and value types that are used for .NET applications.
➢ It contains thousands of classes that supports the following functions.

o Base and user-defined data types


o Support for exceptions handling
o input/output and stream operations
o Communications with the underlying system
o Access to data
o Ability to create Windows-based GUI applications
o Ability to create web-client and server applications
o Support for creating web services

 .NET Framework Base Class Library

➢ .NET Base Class Library is the sub part of the Framework that provides library
support to Common Language Runtime to work properly. It includes the System
namespace and core types of the .NET framework.
 Language Integrated Query (LINQ)

➢ LINQ is known as Language Integrated Query, and it is presented in .NET 3.5 and
Visual Studio 2008.
➢ The excellence of LINQ is it gives the capacity to .NET languages(like C#, VB.NET,
and so forth) to create inquiries to recover information from the information
source.
➢ It attaches one, more power to the C# or .NET languages to generate a
query for any LINQ compatible data source.
➢ And the best part is the syntax used to create a query is the same no
matter which type of data source is used means the syntax of creating a
query data in a relational database is same as that used to create query
data stored in an array there is no need to use SQL or any other non-
.NET language mechanism.
➢ You can also use LINQ with SQL, with XML files, with ADO.NET, with web
services, and with any other database.

• LINQ is present in System.Linq namespace. It provides different type of


classes and methods which supports LINQ queries. In this namespace:
➢ Enumerable class holds standard query operators that operate on object
which executes IEnumerable<T>.
➢ Queryable class holds standard query operators that operate on object
which executes IQueryable<T>.
 Common Language Specification CLS

➢ CLS stands for Common Language Specification and it is a subset of CTS.

➢ It defines a set of rules and restrictions that every language must follow
which runs under the .NET framework.

➢ The languages which follow these set of rules are said to be CLS Compliant.
In simple words, CLS enables cross-language integration or Interoperability.

 Common Type System (CTS)


➢ Common Type System (CTS) describes the datatypes that can be used by
managed code.

➢ CTS defines how these types are declared, used and managed in the runtime.

➢ It facilitates cross-language integration, type safety, and high-performance


code execution.

➢ The rules defined in CTS can be used to define your own classes and values.
 Microsoft Intermediate Language (MSIL)
➢ The Microsoft Intermediate Language (MSIL), also known as the Common
Intermediate Language (CIL)
➢ It 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 JIT compiler.
➢ This is done before the MSIL can be executed. Also, the MSIL is converted
into the machine code on a requirement basis
 Just In Compiler (JIT)
➢ JIT Compiler is an important component of CLR.
➢ It converts the MSIL code into native code (i.e., machine-specific code).
➢ The .NET program is compiled either explicitly or implicitly.
➢ The developer or programmer calls a particular compiler to compile the
program in the explicit compilation.
➢ In implicit compilation, the program is compiled twice.
➢ The source code is compiled into Microsoft Intermediate Language (MSIL)
during the first compilation process.
➢ The MSIL code is converted into native code in the second compilation
process. This process is called JIT compilation.

✓ Types of Just-In-Time Compiler

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

➢ 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.
➢ The cache memory is heavily used by the JIT compiler to store the source
code methods that are required at run-time.
# Delegates
➢ In C#, delegate is a reference to the method. It works like function pointer in C and
C++. But it is objected-oriented, secured and type-safe than function pointer.
➢ For static method, delegate encapsulates method only. But for instance method, it
encapsulates method and instance both.
➢ The best use of delegate is to use as event.
➢ Internally a delegate declaration defines a class which is the derived class
of System.Delegate.

using System;
delegate int Calculator(int n); //declaring delegate
public class DelegateExample
{
static int number = 100;
public static int add(int n)
{
number = number + n;
return number;
}
public static int mul(int n)
{
number = number * n;
return number;
}
public static int getNumber()
{
return number;
}
public static void Main(string[] args)
{
Calculator c1 = new Calculator(add);//instantiating delegate
Calculator c2 = new Calculator(mul);
c1(20);//calling method using delegate
Console.WriteLine("After c1 delegate, Number is: " + getNumber());
c2(3);
Console.WriteLine("After c2 delegate, Number is: " + getNumber());
}
}
➢ Provides a good way to encapsulate the methods.
➢ Delegates are the library class in System namespace.
➢ These are the type-safe pointer of any method.
➢ Delegates are mainly used in implementing the call-back methods and
events.
➢ Delegates can be chained together as two or more methods can be
called on a single event.
➢ Delegate type can be declared using the delegate keyword. Once a
delegate is declared, delegate instance will refer and call those methods
whose return type and parameter-list matches with the delegate
declaration.

Syntax:
[modifier] delegate [return_type] [delegate_name] ([parameter_list]);

• modifier: It is the required modifier which defines the access of delegate


and it is optional to use.
• delegate: It is the keyword which is used to define the delegate.
• return_type: It is the type of value returned by the methods which the
delegate will be going to call. It can be void. A method must have the
same return type as the delegate.
• delegate_name: It is the user-defined name or identifier for the delegate.
• parameter_list: This contains the parameters which are required by the
method when called through the delegate.

 What is AJAX?

➢ AJAX = Asynchronous JavaScript and XML.AJAX is a technique for


creating fast and dynamic web pages.

➢ This is a cross platform technology which speeds up response time. The AJAX server
controls add script to the page which is executed and processed by the browser.
➢ AJAX just uses a combination of: A browser built-in XMLHttpRequest object JavaScript
and HTML DOM
➢ Its ability to improve the performance and usability of web applications.
➢ Ajax in ASP.NET is majorly used when the user is having dynamic content and has to be
reloaded multiple times after the changes have been done

You might also like