Unit 1 C#
Unit 1 C#
➢ .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.
➢ 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.
➢ 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:
Assemblies:
➢ 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.
➢ .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.
➢ 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.
➢ CTS defines how these types are declared, used and managed in the runtime.
➢ 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.
• 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]);
What is AJAX?
➢ 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