Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
415 views

Unit-1 Intro To .Net Framework

- .NET is a development platform that allows developers to build all types of applications like web, mobile, desktop, and more using C#, F# or Visual Basic. - It provides a common language runtime, class libraries and language interoperability that allows applications built with different languages to work together. - The .NET Framework consists of the common language runtime, common type system and common language specification that enable this interoperability across languages, along with a comprehensive framework class library.

Uploaded by

Rahul Gupta
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
415 views

Unit-1 Intro To .Net Framework

- .NET is a development platform that allows developers to build all types of applications like web, mobile, desktop, and more using C#, F# or Visual Basic. - It provides a common language runtime, class libraries and language interoperability that allows applications built with different languages to work together. - The .NET Framework consists of the common language runtime, common type system and common language specification that enable this interoperability across languages, along with a comprehensive framework class library.

Uploaded by

Rahul Gupta
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 77

Introduction to .

NET Technology
What is .NET ?
Microsoft.NET is a Framework
– Microsoft .NET is a Framework which provides a common
platform to Execute or, Run the applications developed in
various programming languages.

– Microsoft announced the .NET initiative in July 2000.

– The main intention was to bridge the gap in


interoperability between services of various
programming languages.
What is .NET ?
• A developer platform for building all your apps: web, mobile,
desktop, gaming, IoT, and more

• .NET is an open source development platform which is just a


way of saying it's a collection of languages and libraries that
can all work together to build all kinds of apps!

• With .NET you can use multiple languages, editors, and


libraries to build for web, mobile, desktop, gaming, and IoT .
.NET Framework Objectives

• The .NET Framework is designed to fulfill the following


objectives:
– Provide object-oriented programming environment

– Provide environment for developing various types of


applications, such as Windows-based applications and
Web-based applications

– To ensure that code based on the .NET Framework can


integrate with any other code
.NET is multi-language
• .NET supports VB, C# (C-sharp), C++, J# (Java 1.2), Eiffel, etc.

code.vb code.cs code.cpp ...

Development Tools FCL

app.exe
Languages

• You can write .NET apps in C#, F#, or Visual Basic.


• C# is a simple, modern, object-oriented, and
type-safe programming language.
• F# is a cross-platform, open-source, functional
programming language for .NET. It also includes
object-oriented and imperative programming.
• Visual Basic is an approachable language with a
simple syntax for building type-safe,
object-oriented apps.
.NET is cross-platform
• Compiled .NET apps run on any supported
platform:
APP.exe

Win32
Win64 (XP,2K,98) WinCE
How is cross-platform achieved?
• Cross-platform execution realized in two
ways:
1. apps are written against Framework Class Library
(FCL), not underlying OS

2. compilers generate generic assembly language


which must be executed by the Common
Language Runtime (CLR)
(1) FCL
• Framework Class Library
– 1000's of predefined classes
– common subset across all platforms & languages
– networking, database access, XML processing, GUI,
Web, etc.

• Goal?
– FCL is a portable operating system
(2) CLR-based execution
• Common Language Runtime must be present to execute
code:
APP.exe

OS Process
other FCL
JIT Compiler
components

Core
obj code
FCL

CLR

Underlying OS and HW
Monolithic
• Monolithic app: all source code compiled into
one .EXE

APP.exe
Component-based
• Component-based app: .EXE + 1 or more .DLLs

compute.dll

GUI.exe
data.dll

– standard practice on Windows…


Why component-based?
• Many motivations:
– team programming
– multi-language development (I like VB, you like C#)
– code reuse (e.g. across different .EXEs)
– independent updating (update just component X)

• FCL ships as a set of components!


.NET implementations
• .NET Core is a cross-platform .NET
implementation for websites, servers, and
console apps on Windows, Linux, and macOS
• .NET Framework supports websites, services,
desktop apps, and more on Windows
• Xamarin/Mono is a .NET implementation for
running apps on all the major mobile
operating systems
One consistent API

• .NET Standard is a base set of APIs that are


common to all .NET implementations.
• Each implementation can also expose
additional APIs that are specific to the
operating systems it runs on. For example,
.NET Framework is a Windows-only .NET
implementation that includes APIs for
accessing the Windows Registry.
.NET Framework
• Programming model for .NET
• Platform for running .NET managed code in a
virtual machine
• Provides a very good environment to develop
networked applications and Web Services
• Provides programming API and unified
language-independent development
framework
The Core of .NET Framework: FCL & CLR

• Common Language Runtime


– Garbage collection
– Language integration
– Multiple versioning support
– Integrated security
• Framework Class Library
– Provides the core functionality:
ASP.NET, Web Services, ADO.NET, Windows
Forms, IO, XML, etc.
.NET Framework

C++ C# VB Perl J# …

Common Language Specification

Visual Studio .NET


ASP .NET Windows
Web Forms Web Services
Forms
Mobile Internet Toolkit

ADO .NET and XML

.NET Framework (Base Class Library)

Common Language Runtime

Operating System
• The .NET Framework consists of:
– The Common Language Specification (CLS)
It contains guidelines, that language should follow so that
they can communicate with other .NET languages. It is also
responsible for Type matching.
– The Framework Base Class Libraries (BCL)
A consistent, object-oriented library of prepackaged
functionality and Applications.
– The Common Language Runtime (CLR)
A language-neutral development & execution environment
that provides common runtime for application execution .
Common Language Specification
CLS performs the following functions:
• Establishes a framework that helps enable cross-language
integration, type safety, and high performance code
execution
• Provides an object-oriented model that supports the
complete implementation of many programming languages
• Defines rules that languages must follow, which helps
ensure that objects written in different languages can
interact with each other
Common Language Specification (CLS)

• Any language that conforms to the CLS is a


.NET language
• A language that conforms to the CLS has the
ability to take full advantage of the
Framework Class Library (FCL)
• CLS is standardized by ECMA
Common Type System (CTS)
• All .NET languages have the same primitive data
types. An int in C# is the same as an int in VB.NET
• When communicating between modules written in
any .NET language, the types are guaranteed to be
compatible on the binary level
• Types can be:
– Value types – passed by value, stored in the stack
– Reference types – passed by reference, stored in the
heap
• Strings are a primitive data type now
.NET Framework Base Class Library
• The Class Library is a comprehensive, object-oriented collection
of reusable types
• These class library can be used to develop applications that
include:
– Traditional command-line applications
– Graphical user interface (GUI) applications
– Applications based on the latest innovations provided by ASP.NET
• Web Forms
• XML Web services
Common Language Runtime
• Manages running code – like a virtual
machine
– Threading
– Memory management
– No interpreter: JIT-compiler produces native
code – during the program installation or at run
time
• Fine-grained evidence-based security
– Code access security
• Code can be verified to guarantee type safety
• No unsafe casts, no un-initialized variables and
no out-of-bounds array indexing
Common Language Runtime (CLR)
• CLR ensures:
– A common runtime environment for all .NET languages
– Uses Common Type System (strict-type & code-verification)
– Memory allocation and garbage collection
– Intermediate Language (IL) to native code compiler. Which
Compiles MSIL code into native executable code
– Security and interoperability of the code with other
languages
• Over 36 languages supported today
– C#, VB, Jscript, Visual C++ from Microsoft
– Perl, Python, Smalltalk, Cobol, Haskell, Mercury, Eiffel, Oberon, Oz, Pascal, APL,
CAML, Scheme, etc.
Main components of CLR
• Common Language Specification (CLS)
• Common Type System (CTS)
• Garbage Collection (GC)
• Just In – Time Compiler (JIT)
CLS
• It is responsible for converting the different .NET programming language
syntactical rules and regulations into CLR understandable format.
• 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
• Managed Code
– The MSIL code which is managed by the CLR is known as the
Managed Code.
• Unmanaged Code
– Before .NET development the programming language like
.COM Components & Win32 API do not generate the MSIL
code.
– So these are not managed by CLR rather managed by
Operating System.
Common Type System (CTS)
• Every programming language has its own data type system, so CTS
is responsible for understanding all the data type systems of .NET
programming languages and converting them into CLR
understandable format which will be a common format.
• There are 2 Types of CTS that every .NET programming language
have :
• Value Types: Value Types will store the value directly into the
memory location. These types work with stack mechanism only.
CLR allows memory for these at Compile Time.
• 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 allots memory for these at Runtime.
• Garbage Collector:
JIT(Just In Time Compiler)
• It is responsible for converting the
CIL(Common Intermediate Language) into
machine code or native code using the
Common Language Runtime environment.
Automatic Memory Management
• The CLR manages memory for managed code
– All allocations of objects and buffers made from a
Managed Heap
– Unused objects and buffers are cleaned up automatically
through Garbage Collection
• Some of the worst bugs in software development are
not possible with managed code
– Leaked memory or objects
– References to freed or non-existent objects
– Reading of uninitialised variables
• Pointerless environment
Garbage Collection
• Automatic memory management is made possible by Garbage Collection in .NET
Framework.
• When a class object is created at runtime, certain memory space is allocated to it
in the heap memory.
• After all the actions related to the object are completed in the program, the
memory space allocated to it is a waste as it cannot be used.
• In this case, garbage collection is very useful as it automatically releases the
memory space after it is no longer required.
• Garbage collection will always work on Managed Heap and internally it has an
Engine which is known as the Optimization Engine.
• Garbage Collection occurs if at least one of multiple conditions is satisfied.
– 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.
Phases in Garbage Collection
Phases in Garbage Collection
Marking Phase
• A list of all the live objects is created during
the marking phase.
• This is done by following the references from
all the root objects.
• All of the objects that are not on the list of live
objects are potentially deleted from the heap
memory.
Phases in Garbage Collection
Relocating Phase
• The references of all the objects that were on
the list of all the live objects are updated in
the relocating phase so that they point to the
new location where the objects will be
relocated to in the compacting phase.
Phases in Garbage Collection
Compacting Phase
• The heap gets compacted in the compacting
phase as the space occupied by the dead
objects is released and the live objects
remaining are moved.
• All the live objects that remain after the
garbage collection are moved towards the
older end of the heap memory in their original
order.
Heap Generations in Garbage
Collection
• The heap memory is organized into 3 generations
so that various objects with different lifetimes can
be handled appropriately during garbage
collection.
• The memory to each Generation will be given by
the Common Language Runtime(CLR) depending
on the project size.
• Internally, Optimization Engine will call
the Collection Means Method to select which
objects will go into Gneration 1 or Generation 2.
Heap Generations in Garbage
Collection
Heap Generations in Garbage
Collection
Generation 0
• All the short-lived objects such as temporary
variables are contained in the generation 0 of the
heap memory.
• All the newly allocated objects are also
generation 0 objects implicitly unless they are
large objects.
• In general, the frequency of garbage collection is
the highest in generation 0.
Heap Generations in Garbage
Collection
Generation 1
• If space occupied by some generation 0
objects that are not released in a garbage
collection run, then these objects get moved
to generation 1.
• The objects in this generation are a sort of
buffer between the short-lived objects in
generation 0 and the long-lived objects in
generation 2.
Heap Generations in Garbage
Collection
Generation 2
• If space occupied by some generation 1
objects that are not released in the next
garbage collection run, then these objects get
moved to generation 2.
• The objects in generation 2 are long lived such
as static objects as they remain in the heap
memory for the whole process duration.
Managed Code
• Code that targets the CLR is referred to as
managed code
• All managed code has the features of the CLR
– Object-oriented
– Type-safe
– Cross-language integration
– Cross language exception handling
– Multiple version support
• Managed code is represented in special
Intermediate Language (IL)
Execution in CLR
Source
VB C# C++
code
Compiler Compiler Compiler

Managed Assembly Assembly Assembly


code IL Code IL Code IL Code

Common Language Runtime


JIT Compiler

Native Code

Operating System Services


Benefits of CLR
• It improves the performance by providing a rich interact between programs at run
time.
• Enhance portability by removing the need of recompiling a program on any
operating system that supports it.
• Security also increases as it analyzes the MSIL instructions whether they are safe
or unsafe. Also, the use of delegates in place of function pointers enhance the type
safety and security.
• Support automatic memory management with the help of Garbage Collector.
• Provides cross-language integration because CTS inside CLR provides a common
standard that activates the different languages to extend and share each other’s
libraries.
• Provides support to use the components that developed in other .NET
programming languages.
• Provide language, platform, and architecture independence.
• It allows easy creation of scalable and multithreaded applications, as the
developer has no need to think about the memory management and security
issues.
Multiple Language Support
◆ IL (MSIL or CIL) – Intermediate Language
❖ It is low-level (machine) language, like Assembler,
but is Object-oriented
◆ CTS is a rich type system built into the CLR
❖ Implements various types (int, float, string, …)
❖ And operations on those types
◆ CLS is a set of specifications that all languages and
libraries need to follow
❖ This will ensure interoperability between languages
Intermediate Language
• .NET languages are compiled to an Intermediate
Language (IL)
• IL is also known as MSIL or CIL
• CLR compiles IL in just-in-time (JIT) manner –
each function is compiled just before execution
• The JIT code stays in memory for subsequent calls
• Recompilations of assemblies are also possible
Example of MSIL Code
.method private hidebysig static void Main()
cil managed
{
.entrypoint
// Code size 11 (0xb)
.maxstack 8
IL_0000: ldstr "Hello, world!"
IL_0005: call void
[mscorlib]System.Console::WriteLine(string)
IL_000a: ret
} // end of method HelloWorld::Main
Visual Studio IDE

Microsoft has introduced Visual Studio.NET,


which is a tool (also called Integrated Development
Environment) for developing .NET applications by
using programming languages such as VB, C#,
VC++ and VJ#. etc.
Visual Studio .NET
• Development tool that contains a rich set of
productivity and debugging features
– Supports managed and unmanaged applications
– Supports C#, C++, VB.NET, …
– Many useful tools and wizards
– Windows Forms Designer
– ASP.NET Web Forms Designer
– Web Services support
– SQL Server integration with ADO.NET and XML
• VS.NET is not part of the .NET Framework
– Not necessary to build or run managed code
– The .NET Framework SDK includes command line compilers
VS.NET – Single Development
Environment & Skill Set
• From Visual Studio.NET you can:
– Write code
– Design user interface
– Study documentation
– Debug
– Test
– Deploy
• Same tools for all languages
• Same tools for all platforms
Visual Studio .NET
.NET Languages
• Languages provided by Microsoft
– C++, C#, J#, VB.NET, JScript
• Third-parties languages
– Perl, Python, Pascal, APL, COBOL, Eiffel, Haskell,
ML, Oberon, Scheme, Smalltalk…
• Advanced multi-language features
– Cross-language inheritance and exceptions
handling
• Object system is built in, not bolted on
– No additional rules or API to learn
C# Language
• Mixture between C++, Java and Delphi
• Component-oriented
– Properties, Methods, Events
– Attributes, XML documentation
– All in one place, no header files, IDL, etc.
– Can be embedded in ASP+ pages
• Everything really is an object
– Primitive types aren’t magic
– Unified type system == Deep simplicity
– Improved extensibility and reusability
C# (C Sharp)
• Microsoft C# (pronounced C Sharp) developed by Microsoft
Corporation, USA

• New programming language that runs on the .NET Framework

• C# is simple, modern, type safe, and object oriented

• C# code is compiled as managed code

• Combines the best features of Visual Basic, C++ and Java


C# Features
• Simple
• Modern
• Object-Oriented
• Type-safe
• Versionable
• Compatible
• Secure
C# Language – Example
using System;
class HelloWorld
{
public static void main()
{
Console.WriteLine(“Hello, world!”);
}
}
Code Compilation and Execution
Compilation
Also called
Source Language Code Assembly
Code Compiler MSIL
Metadat (.EXE or
a .DLL file)

Before
installation or
the first time
Execution each method is
called
Native JIT
Code Compiler
Assemblies
• Logical unit that contains compiled code targeted at the
.NET Framework
• Assembly is really a collection of types and resource
information that are built to work together and form a
logical unit of functionality
• DLL or EXE file
• Smallest deployable unit of .NET applications
• Difference between an assembly and a native .EXE or
.DLL- assemblies include metadata
• Any .net application- compile- assembly(.exe or .dll)
• Assembly contains IL and manifest
Assembly features
• Self- describing
• Versions dependencies are recorded inside an assembly
manifest
• Loaded side by side
• Assemblies are only loaded into memory if they are required.
If they aren't used, they aren't loaded. This means that
assemblies can be an efficient way to manage resources in
larger projects
Assembly structure
Assembly structure
• An assembly consists of
– assembly metadata describing the complete assembly
– type metadata describing the exported types and methods
– MSIL code
– resources
• All these parts can be inside one file or spread across several
files
Assembly Manifests
• Every assembly has an assembly manifest file.
• Assembly manifest contains:
– Identity – name, version, culture, and public key
– List of files - files that make up the assembly, such as other
assemblies you created that your .exe or .dll file relies on,
bitmap files, or Readme files.
– An assembly reference list, which is a list of all external
dependencies, such as .dlls or other files. Assembly
references contain references to both global and private
objects. This reference information includes the version
number and public key to uniquely identify assemblies.
Assembly Manifests
• A set of permissions requests- needed to run this assembly
• Exported types
– these are include if they are defined within a module and
the module is referenced from the assembly
– otherwise they are not part of manifest
– A module is a unit of reuse
– Type description is stored as metadata inside the assembly
• Some information in manifest can be modified using
attributes
Assembly version

• Specifies the assembly version


• Default- 1.0.0.0.
• Four parts
– Major version
– Minor version
– Build number
– Revision number
ildasm-IL Disassembler

• Use this tool


– to peek at assembly manifest and IL
– to export manifest and IL to text file
• Path for ildasm
– C:\program files\microsoft sdks\windows\v7.0a\bin
• Syntax
– ildasm.exe <filename>
ilasm-IL Assembler

• Use this tool


– To reconstruct an assembly from a text file that contains assembly
manifest and IL
• Path for ilasm
– C:\windows\microsoft.net\framework\v4.0.50727
• Syntax
– ilasm.exe <filename>
Assembly types
Private Assemblies

• Private Assemblies are designed to be used by one application


• must reside in that application's directory or subdirectory
• No naming conflicts with other classes
• No versioning problems
• During build process referenced assemblies are copied to the
application directory
• Useful when applications and components are built within the
same company
• Default assembly
• Simplest types
Assembly types
Shared Assemblies

• Intended to be common libraries that any other


application can use
• Mostly used when a large application is split into
subprojects
• Shared assembly is installed in a global assembly
cache(GAC)
• Given a name based on private key cryptography(strong
name)
• Strong name is guaranteed to be unique
• Version information is specified in the assembly manifest
Assembly types
Satellite Assembly

• A satellite Assembly is defined as an assembly with resources


only, no executable code
• This is extremely useful for localization
• Because an assembly has a culture associated with it, the
resource manager looks for satellite assemblies containing
the resources of a specific culture
Metadata in Assembly
Type Descriptions
Classes
Base classes
Implemented interfaces
Data members
Methods
Assembly Description
Name
Version
Culture
Other assemblies
Security Permissions
Exported Types
Applications
• One or more assemblies
• Assemblies conflict resolution
– Using metadata
• Local (preferred)
• Global Assembly Cache (GAC)
• Different applications may use different
versions of an assembly
– Easier software updates
– Easier software removal
GAC
• The Global Assembly Cache (GAC) is a folder in Windows
directory to store the .NET assemblies that are specifically
designated to be shared by all applications executed on a
system.
• The concept of GAC is the result of the .NET architecture
whose design addresses the issue of "DLL hell" that existed in
COM (Component Object Model).
• Unlike in COM, there is no need for the assembly in GAC to be
registered before its use.
• Each assembly is accessed globally without any conflict by
identifying its name, version, architecture, culture and public
key.
GAC
• For .net 2.0-3.5 assemblies
– C:\windows\assembly
• For .net 4.0 assemblies
– c:\windows\microsoft.net\assembly
Strong naming assembly
• To generate key pair use strong naming tool
– sn.exe –k c:\KeyFile.snk
• To strong name an assembly
– [assembly: AssemblyKeyFile(“KeyFile.snk”)]
• Strong named assembly should have:
– Assembly name
– Version number
– Private/public key pair
Deploying assembly
• There are two ways to deploy an assembly into the Global
Assembly Cache:
– Use an installer designed to work with the Global Assembly Cache.
This is the preferred option for installing assemblies into the Global
Assembly Cache
– Use a developer tool called the Global Assembly Cache tool
(Gacutil.exe), provided by the Windows SDK
Global Assembly Cache tool
• The Global Assembly Cache tool allows you to view and
manipulate the contents of the global assembly cache and
download cache.
• This tool is automatically installed with Visual Studio. To run
the tool, use the Developer Command Prompt for Visual
Studio (or the Visual Studio Command Prompt in Windows 7).
• At the command prompt, type the following:
• Syntax
• gacutil [options] [assemblyName | assemblyPath |
assemblyListFile]
• You must have administrator privileges to use Gacutil.exe.
Example
• The following command installs the
assembly mydll.dll into the global assembly cache
– gacutil -i mydll.dll
• The following command removes the
assembly hello from the global assembly cache as long
as no reference counts exist for the assembly
– gacutil -u hello
• To uninstall a specific assembly using GAC utility
– gacutil –u
claaslibrary,version=1.0.0.0,publickeytoken=eeaabf36d7783129

You might also like