Unit I
Unit I
Net Framework
Introduction:
The .NET is a very large framework and sometimes is hard to decide whether to code something or use an existing component of the
framework. Sometimes is faster to code, but when production workload and reliability comes into play, the perspective is different. An
overview of what .NET offers is a fundamental knowledge. It is hard to keep in track because .NET evolves very fast. This article
discusses essentials which is the .NET Framework built from.
.NET Framework (pronounced dot net) is a software framework developed by Microsoft that runs primarily on Microsoft Windows. It
includes a large class library known as Framework Class Library (FCL) and provides language interoperability(each language can use
code written in other languages) across several programming languages. Programs written for .NET Framework execute in
a software environment (as contrasted to hardware environment), known as Common Language Runtime (CLR), an application virtual
machine that provides services such as security, memory management, and exception handling. FCL and CLR together constitute
.NET Framework.
FCL provides user interface, data access, database connectivity, cryptography, web application development, numeric alagorithms,
and network communications. Programmers produce software by combining their own source code with .NET Framework and other
libraries. .NET Framework is intended to be used by most new applications created for the Windows platform. Microsoft also
produces an integrated development environment largely for .NET software called Visual Studio.
.NET Framework started out as a proprietary framework, although the company worked to standardize the software stack almost
immediately, even before its first release. Despite the standardization efforts, developers—particularly those in the free and open-
source software communities—expressed their uneasiness with the selected terms and the prospects of any free and open-source
implementation, especially with regard to software patents. Since then, Microsoft has changed .NET development to more closely
follow a contemporary model of a community-developed software project, including issuing an update to its patent that promises to
address the concerns.
The .NET Framework is a new and revolutionary platform created by Microsoft for developing applications.
It is a platform for application developers.
It is a Framework that supports Multiple Language and Cross language integration.
IT has IDE (Integrated Development Environment).
Framework is a set of utilities or can say building blocks of your application system.
.NET Framework provides GUI in a GUI manner.
.NET is a platform independent but with help of Mono Compilation System (MCS). MCS is a middle level interface.
.NET Framework provides interoperability between languages i.e. Common Type System (CTS)
.NET Framework also includes the .NET Common Language Runtime (CLR), which is responsible for maintaining the execution of
all applications developed using the .NET library.
The .NET Framework consists primarily of a gigantic library of code.
Overview
C# is a modern, general-purpose, object-oriented programming language developed by Microsoft and approved by European
Computer Manufacturers Association (ECMA) and International Standards Organization (ISO).
C# was developed by Anders Hejlsberg and his team during the development of .Net Framework.
C# is designed for Common Language Infrastructure (CLI), which consists of the executable code and runtime environment that
allows use of various high-level languages on different computer platforms and architectures.
The following reasons make C# a widely used professional language −
It is a modern, general-purpose programming language
It is object oriented.
It is component oriented.
It is easy to learn.
It is a structured language.
It produces efficient programs.
It can be compiled on a variety of computer platforms.
It is a part of .Net Framework.
In the above diagram we could see that all the languages supports the CTS minimally and some extra types. And diagram also shows
all the languages obey the rules defined in the CLS. We achieve the interoperability through this CTS. Since all the .Net languages are
expected to support CTS it is guaranteed that types inside this CTS are fully interoperable.
What is CTS
CTS define all of the basic types that can be used in the .NET Framework and the operations performer on those type.
All this time we have been talking about language interoperability, and .NET Class Framework. None of this is possible without all
the language sharing the same data types. What this means is that an int should mean the same in VB, VC++, C# and all other .NET
compliant languages. This is achieved through introduction of Common Type System (CTS).
WHAT IS A CLR?
CLR is .NET equivalent of Java Virtual Machine (JVM). It is the runtime that converts a MSIL code into the host machine language
code, which is then executed appropriately. The CLR is the execution engine for .NET Framework applications. It provides a number
of services, including:
Code management (loading and execution)
Application memory isolation
Verification of type safety
Conversion of IL to native code.
Access to metadata (enhanced type information)
Managing memory for managed objects
Enforcement of code access security
Exception handling, including cross-language exceptions
Full form of CLR is Common Language Runtime and it forms the heart of the .NET framework
Many different languages and platforms provide a runtime, and the .NET Framework is no exception. You will find, however,
that this runtime is quite different from most.
The Common Language Runtime (CLR) in the .NET Framework manages the execution of the code and provides access to a
variety of services that will make the development process easier.
The CLR has been developed to be far superior to previous runtimes, such as the VB runtime, by attaining the following:
_ Cross-language integration
_ Code access security
_ Object lifetime management
_ Debugging and profiling support
Code that is compiled and targeted to the CLR is known as managed code. Managed code
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
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.
Introduction to Metadata in C#
C# in metadata is defined as the binary information which is describing our program and this information is stored either in common
language runtime portable executable file or in memory. If you compile the code from portable executable file then metadata is
inserted in one more area portion of the file and all this code will now be converted into MSIL format (Microsoft Intermediate
Language) then code moved into another partition portion of the file. All data types and data members that is defined and referenced
in the assembly is put within the metadata. While we are executing the C# code at the runtime, it loads the metadata from the memory.
The main purpose of the C# metadata is to know the information about the class, data members, inheritance, and data types etc. of the
class. Metadata in the file consists of table and heap data structures.
Uses of Metadata
Given below are the uses of Metadata:
It provides description about assembly data types like name, visibility, base class and interfaces etc.
It provides data members like methods, fields, properties, events and nested types.
It also provides additional description of the elements that modify types and members.
It have identity like name, version, public key etc.
It is a key to simple programming model and it will eliminate the necessity for IDL (Interface Definition Language) files, header files.
Difference between managed and unmanaged code
What is Managed Code -
Managed code is the code that is written to target the services of the managed runtime execution environment such as Common
Language Runtime in .Net Technology.
The Managed Code running under a Common Language Runtime cannot be accessed outside the runtime environment as well as
cannot call directly from outside the runtime environment. It refers to a contract of cooperation between natively executing code and
the runtime. It offers services like garbage collection, run-time type checking, reference checking etc. By using managed code you can
avoid many typical programming mistakes that lead to security holes and unstable applications, also, many unproductive programming
tasks are automatically taken care of, such as type safety checking, memory management, destruction of unused Objects etc.
Class Library
Class libraries are those that provide functionality similar to Active X and DLL by creating classes that access other applications.
Windows Control Library
This allows to create our own windows controls. Also called as User Controls, where you group some controls, add it to the toolbox
and make it available to other projects.
Windows Service
These run continuously regardless of the user interaction. They are designed for special purpose and once written, will keep running
and come to an end only when the system is shut down.This template is to develop other kinds of applications like enterprise
applications,database applications
Properties Window
The properties window allows us to set properties for various objects at design time. For example, if you want to change the font, font
size, backcolor, name, text that appears on a button, textbox etc, you can do that in this window. Below is the image of properties
window. You can view the properties window by selecting
View->Properties Window from the main menu or by pressing F4 on the keyboard.
Command Window
The command window in the image below is a useful window. Using this window we can add new item to the project, add new
project and so on. You can view the command window by selecting
View->Other Windows->Command Window from the main menu. The command window in the image displays all possible
commands with File.
Output Window
The output window as you can see in the image below displays the results of building and running applications.
Object Explorer Window
The object explorer window allows us to view all the members of an object at once. It lists all the objects in our code and gives us
access to them. The image below displays an object explorer window. You can view the object explorer window by selecting View-
>Other Windows-> Object Browser from the main menu.
Toolbox Window
The toolbox window is the window that gives us access to all controls, components, etc. As you can see from the image below, the
toolbox uses tabs to divide it's contents into categories (Data, Components, Windows Forms and General). The Data tab displays tools
for creating datasets and making data connections, the Windows Forms tab displays tools for adding controls to forms, the General tab
is left empty by default, the Clipboard Ring tab displays recent items stored in the clipboard and allows us to select from them
Compilation and Execution Process in C#
C# is a general-purpose, strongly typed, lexically scoped, functional, object-oriented, and component-oriented programming language.
In this article, we are going to learn how C# code gets compiled and executed.
Step-by-step process of C# code compilation
Step 3: Now compiler checks if the code contains an error or not. If no error is found then the compiler move to the next step. Or if
the compiler found errors, then it will immediately tell the developer that an error is found in the given line, so that the developer can
correct them. After correcting the error when you again run the code the compiler again check for the errors, if no error found then it
will move to the next step or if an error is found then the compiler gives a message to the developer. In C# there are two types of
errors:
Compiler error
Errors that occur when the developer violates the rules of writing syntax are known as Compile-Time errors. This compiler error
indicates something that must be fixed before the code can be compiled. All these errors are generally detected by the compiler and
thus are known as compile-time errors. For example, missing semicolon, missing parenthesis, etc.
Runtime error
Errors that occur during program execution(run-time) after successful compilation is called run-time errors. One of the most common
run-time errors is division by zero also known as Division error. These types of errors are hard to find as the compiler doesn’t point to
the line at which the error occurs.
Step 4: Languages such as Java or C# are not directly converted or compiled into machine-level language or machine instructions.
These languages need to be converted to an intermediate code first, which is a partially half compiled code. For C#, the source code is
converted to an intermediate code which is known as Common Intermediate Language (CIL) or Intermediate Language Code
(ILC or IL code). This CIL or IL Code can run on any operating system because C# is a Platform Independent Language.
Step 5: After converting the C# source code to Common Intermediate Language (CIL) or Intermediate Language Code (ILC or IL
code, the intermediate code needs to be converted to machine understandable code. C# uses the .NET Framework and as part of this
.NET Framework, the Virtual Machine component manages the execution of programs written in any language that uses the . NET
Framework. This virtual machine component is known as Common Language Runtime (CLR) which translates the CIL or IL code
to native code or machine understandable code or machine instructions. This process is called the Just-In-Time (JIT)
Compilation or Dynamic Compilation which is the way of compiling code during the execution of a program at run time only.
Step 6: Once the C# programs are compiled, they’re physically packaged into Assemblies. An assembly is a file that contains one or
more namespaces and classes. As the number of classes and namespaces in program grows, it is physically separated by related
namespaces into separate assemblies. Assemblies typically have the file extension .exe or .dll, depending on whether they implement
applications or libraries respectively, where EXE stands for Executable and DLL stands for Dynamic Link Library. An EXE
(Executable) file represents a program that can be executed and a DLL (Dynamic Link Library) file includes code (Eg: Library) that
can be reused across different programs.
Step 7: Now, the C# compiler returns the output of the given c# code.
C# Namespaces
Namespaces in C# are used to organize too many classes so that it can be easy to handle the application.
In a simple C# program, we use System.Console where System is the namespace and Console is the class. To access the class of a
namespace, we need to use namespacename.classname. We can use using keyword so that we don't have to use complete name all the
time.
In C#, global namespace is the root namespace. The global::System will always refer to the namespace "System" of .Net Framework.
C# namespace example
Let's see a simple example of namespace which contains one class "Program".
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello Namespace!");
}
}
}
Output:
Hello Namespace!
C# namespace example:
Let's see another example of namespace in C# where one namespace program accesses another namespace program.
using System;
namespace First
{
public class Hello
{
public void sayHello()
{
Console.WriteLine("Hello First Namespace"); }
}
}
namespace Second
{
public class Hello
{
public void sayHello()
{
Console.WriteLine("Hello Second Namespace"); }
}
}
public class TestNamespace
{
public static void Main()
{
First.Hello h1 = new First.Hello();
Second.Hello h2 = new Second.Hello();
h1.sayHello();
h2.sayHello();
}
}
Output:
Hello First Namespace
Hello Second Namespace