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)
4. 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
5. 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.
8. 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++
9. 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
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 of CIL
Ildasm.exe: C:Program Files (x86)Microsoft SDKsWindowsv10.0AbinNETFX XXX Tools
15. 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)
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 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
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 Data Types
• All .Net language keywords ultimately resolve to the
same CTS type defined in as assembly named
mscorlib.dll
26. 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
27. 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
28. Understanding the CLR
•CLR is physically represented by a library named
mscoree.dll
•CLR might interact with the BCL
Asm referenced for usemscoree
loadedmscoree loads required assemblies
29. 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
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