The Philosophy of .NET
Lecturer: Vahid Farahmandian
https://www.linkedin.com/in/vfarahmandian
CLR, CTS & CLS
• We can refer to .Net as a Runtime Environment and a
comprehensive Base Class Library
• The Runtime layer
 Common Language Runtime (CLR)
 Common Type System (CTS)
 Common Language Specification (CLS)
CLR Roles
1. Locate, Load & Manage .Net objects
2. Take care of:
Memory Management
App Hosting
Coordinating Threads
Performing basic security checks, etc
CTS Roles
1. Describes all possible data types and programming
constructs supported by runtime
2. Specifies how these entities interacts with each
other
3. How they are represented in .Net metadata
CLS Roles
•Essentially no all .Net aware languages might support
every feature defined by CTS.
•CLS defines a subset of common types and
programming constructs that all .Net languages agree
on.
CTS & CLS
Base Class Library(BCL)
What C# Brings to the Table
•Members of C family like Java, C, Objective C, C++
•Supports some features traditionally found in
Functional Languages ex: LISP, Haskell (Lambda
Expr)
•Syntactically clean as Java
•As simple as VB
•As much power and flexibility as C++
Managed vs. Unmanaged Code
• C# only can be used to build apps hosted under the .Net runtime
• Code targeting the .Net runtime  Managed Code
• Binary unit that contains managed code  Assembly
• Code that cannot be directly hosted by .Net Unmanaged Code
An Overview of .NET Assemblies
An Overview of .NET Assemblies
• .Net binaries do not contain platform-specific instruction but rather
platform-agnostic IL and type metadata
IL == MSIL == CIL
•Assembly contains CIL code  is not compiled to platform-
specific instruction until absolutely necessary
•Absolute necessary  referenced for use by the .Net runtime
An Overview of .NET Assemblies
• Assembly also contains metadata(Type Metadata)
• Metadata describes in vivid detail the characteristics of every type
within binary
Ex: Who is the base class? Which interfaces are implemented inside
the given class, etc.
• .Net metadata is always present within assembly & generated by
.Net-aware language compiler
An Overview of .NET Assemblies
•Assemblies themselves are also described using metadata
which is officially termed a manifest(Assembly Manifest).
•Manifest contains information about the current version of
assembly, culture information(used for localizing string etc)
and a list of all externally referenced assemblies
The role of CIL
Ildasm.exe: C:Program Files (x86)Microsoft SDKsWindowsv10.0AbinNETFX XXX Tools
Compiling CIL to Platform-Specific
Instructions
• JIT compiler(Jitter), compiles CIL code into meaningful CPU
instructions(Machine Code)
• Jitter cache the CIL compilation result in memory suited to
the target OS
• Ngen.exe is a command-line tool that perform a pre-JIT of an
assembly in order to improve the startup time(Mostly used
for graphically intensive apps)
The Role of .NET Type Metadata
• Used by intelliSense
• Used by object-browsing utilities
• Used by Debugging tools
• Used by compiler itself
• Used by WCF, Reflection, late binding, serialization etc
• Type Metadata is the backbone of numerous .Net
technologies
The Role of the Assembly Manifest
• Documents all external assemblies
• Assembly version number
• Copyright information, etc
• Like Type metadata, it’s the job of the compiler to generate
the assembly’s manifest
Understanding the CTS
• Type, is a general term used to refer to a member from
the set {class, interface, structure, enumeration,
delegate}
• CTS is a formal specification that documents how
types must be defined in order to be hosted by the
CLR
CTS Class Types
• Every .Net-aware language supports at least the notion
of a class type
• Class is the cornerstone of OOP
• A class may be composed of any number of
members(ctor, properties, methods, events etc)
CTS Interface Types
• Interfaces are nothing more than a named collection of
abstract definitions
• Interfaces are usually declared as public, to allow
types in other assemblies to implement their behavior
CTS Structure Types
• Structure can be thought of as a lightweight class type
having value-based semantics
• Typically are best suited for modeling geometric and
mathematical data
CTS Enumeration Types
• Enumerations allow you to group name-value pairs
• By default storage used to hold each item in
enumeration is a 32 bit integer, however you can alter
this
• Enumerations derive from common base class called
System.Enum which allows you to work better with
enumerations
CTS Delegate Types
• Delegates are critical when you want to provide a way
for one object to forward a call to another
• Delegates provide the foundation for .Net event
architecture
CTS Type Members
• Most types take any number of members
• Type member is constrainted by the set {ctor,
finalizer,static ctor, nested type, operator, method,
property, indexer, field, readonly field, constant event}
• CTS defines various concepts that may be associated
with a given member(ex member’s visibility)
Intrinsic CTS Data Types
• All .Net language keywords ultimately resolve to the
same CTS type defined in as assembly named
mscorlib.dll
Understanding the CLS
CTS is a set of rules that describe in vivid detail the
minimal and complete set of features a given .Net-
aware compiler must support to produce code that can
be hosted by the CLR
Understanding the CLS
CTS is a set of rules that describe in vivid detail the
minimal and complete set of features a given .Net-
aware compiler must support to produce code that can
be hosted by the CLR
[assembly: CLSCompliant(true)] tells the compiler to
check for CLS compliance
Understanding the CLR
•CLR is physically represented by a library named
mscoree.dll
•CLR might interact with the BCL
Asm referenced for usemscoree
loadedmscoree loads required assemblies
Understanding the CLR
Mscoree tasks:
1. Resolving the location for an assembly
2. Finding the requested type within assembly by
reading metadata
3. Lays out the type in memory
4. Compiles the associated CIL into machine code
5. Perform necessary security checks
6. Execute the code
Understanding the CLR
The Assembly/Namespace/Type Distinction
• Namespace is a grouping of semantically related types contained
in an assembly or possibly spread across multiple assemblies
• While defining a type using the fully qualified name provides
greater readability
• using keyword is simply a shorthand notation for specifying a
type’s fully qualified name, and either approach results in the
same underlying CIL (given that CIL code always uses fully
qualified names) and has no effect on performance or the size of
the assembly.
Referencing External Assemblies
• A vast majority of the .NET assemblies are located
under a specific directory termed the global assembly
cache (GAC)
• GAC by default is in : C:WindowsAssemblyGAC
• On a non-windows machine the location of the GAC
depends on the .Net distribution
The Platform-Independent Nature of .NET
The Platform-Independent Nature of .NET

The Philosophy of .Net

  • 1.
    The Philosophy of.NET Lecturer: Vahid Farahmandian https://www.linkedin.com/in/vfarahmandian
  • 2.
    CLR, CTS &CLS • We can refer to .Net as a Runtime Environment and a comprehensive Base Class Library • The Runtime layer  Common Language Runtime (CLR)  Common Type System (CTS)  Common Language Specification (CLS)
  • 3.
    CLR Roles 1. Locate,Load & Manage .Net objects 2. Take care of: Memory Management App Hosting Coordinating Threads Performing basic security checks, etc
  • 4.
    CTS Roles 1. Describesall possible data types and programming constructs supported by runtime 2. Specifies how these entities interacts with each other 3. How they are represented in .Net metadata
  • 5.
    CLS Roles •Essentially noall .Net aware languages might support every feature defined by CTS. •CLS defines a subset of common types and programming constructs that all .Net languages agree on.
  • 6.
  • 7.
  • 8.
    What C# Bringsto the Table •Members of C family like Java, C, Objective C, C++ •Supports some features traditionally found in Functional Languages ex: LISP, Haskell (Lambda Expr) •Syntactically clean as Java •As simple as VB •As much power and flexibility as C++
  • 9.
    Managed vs. UnmanagedCode • C# only can be used to build apps hosted under the .Net runtime • Code targeting the .Net runtime  Managed Code • Binary unit that contains managed code  Assembly • Code that cannot be directly hosted by .Net Unmanaged Code
  • 10.
    An Overview of.NET Assemblies
  • 11.
    An Overview of.NET Assemblies • .Net binaries do not contain platform-specific instruction but rather platform-agnostic IL and type metadata IL == MSIL == CIL •Assembly contains CIL code  is not compiled to platform- specific instruction until absolutely necessary •Absolute necessary  referenced for use by the .Net runtime
  • 12.
    An Overview of.NET Assemblies • Assembly also contains metadata(Type Metadata) • Metadata describes in vivid detail the characteristics of every type within binary Ex: Who is the base class? Which interfaces are implemented inside the given class, etc. • .Net metadata is always present within assembly & generated by .Net-aware language compiler
  • 13.
    An Overview of.NET Assemblies •Assemblies themselves are also described using metadata which is officially termed a manifest(Assembly Manifest). •Manifest contains information about the current version of assembly, culture information(used for localizing string etc) and a list of all externally referenced assemblies
  • 14.
    The role ofCIL Ildasm.exe: C:Program Files (x86)Microsoft SDKsWindowsv10.0AbinNETFX XXX Tools
  • 15.
    Compiling CIL toPlatform-Specific Instructions • JIT compiler(Jitter), compiles CIL code into meaningful CPU instructions(Machine Code) • Jitter cache the CIL compilation result in memory suited to the target OS • Ngen.exe is a command-line tool that perform a pre-JIT of an assembly in order to improve the startup time(Mostly used for graphically intensive apps)
  • 16.
    The Role of.NET Type Metadata • Used by intelliSense • Used by object-browsing utilities • Used by Debugging tools • Used by compiler itself • Used by WCF, Reflection, late binding, serialization etc • Type Metadata is the backbone of numerous .Net technologies
  • 17.
    The Role ofthe Assembly Manifest • Documents all external assemblies • Assembly version number • Copyright information, etc • Like Type metadata, it’s the job of the compiler to generate the assembly’s manifest
  • 18.
    Understanding the CTS •Type, is a general term used to refer to a member from the set {class, interface, structure, enumeration, delegate} • CTS is a formal specification that documents how types must be defined in order to be hosted by the CLR
  • 19.
    CTS Class Types •Every .Net-aware language supports at least the notion of a class type • Class is the cornerstone of OOP • A class may be composed of any number of members(ctor, properties, methods, events etc)
  • 20.
    CTS Interface Types •Interfaces are nothing more than a named collection of abstract definitions • Interfaces are usually declared as public, to allow types in other assemblies to implement their behavior
  • 21.
    CTS Structure Types •Structure can be thought of as a lightweight class type having value-based semantics • Typically are best suited for modeling geometric and mathematical data
  • 22.
    CTS Enumeration Types •Enumerations allow you to group name-value pairs • By default storage used to hold each item in enumeration is a 32 bit integer, however you can alter this • Enumerations derive from common base class called System.Enum which allows you to work better with enumerations
  • 23.
    CTS Delegate Types •Delegates are critical when you want to provide a way for one object to forward a call to another • Delegates provide the foundation for .Net event architecture
  • 24.
    CTS Type Members •Most types take any number of members • Type member is constrainted by the set {ctor, finalizer,static ctor, nested type, operator, method, property, indexer, field, readonly field, constant event} • CTS defines various concepts that may be associated with a given member(ex member’s visibility)
  • 25.
    Intrinsic CTS DataTypes • All .Net language keywords ultimately resolve to the same CTS type defined in as assembly named mscorlib.dll
  • 26.
    Understanding the CLS CTSis a set of rules that describe in vivid detail the minimal and complete set of features a given .Net- aware compiler must support to produce code that can be hosted by the CLR
  • 27.
    Understanding the CLS CTSis a set of rules that describe in vivid detail the minimal and complete set of features a given .Net- aware compiler must support to produce code that can be hosted by the CLR [assembly: CLSCompliant(true)] tells the compiler to check for CLS compliance
  • 28.
    Understanding the CLR •CLRis physically represented by a library named mscoree.dll •CLR might interact with the BCL Asm referenced for usemscoree loadedmscoree loads required assemblies
  • 29.
    Understanding the CLR Mscoreetasks: 1. Resolving the location for an assembly 2. Finding the requested type within assembly by reading metadata 3. Lays out the type in memory 4. Compiles the associated CIL into machine code 5. Perform necessary security checks 6. Execute the code
  • 30.
  • 31.
    The Assembly/Namespace/Type Distinction •Namespace is a grouping of semantically related types contained in an assembly or possibly spread across multiple assemblies • While defining a type using the fully qualified name provides greater readability • using keyword is simply a shorthand notation for specifying a type’s fully qualified name, and either approach results in the same underlying CIL (given that CIL code always uses fully qualified names) and has no effect on performance or the size of the assembly.
  • 32.
    Referencing External Assemblies •A vast majority of the .NET assemblies are located under a specific directory termed the global assembly cache (GAC) • GAC by default is in : C:WindowsAssemblyGAC • On a non-windows machine the location of the GAC depends on the .Net distribution
  • 33.
  • 34.