The Common Language Runtime
The Common Language Runtime (CLR) is an Execution Environment . It works as a layer between Operating Systems and the applications written in .Net languages .
The core of the CLR is physically represented by a library named mscoree.dll ( Common Object Runtime Execution Engine). When an assembly is referenced for use, mscoree.dll is loaded automatically, which in turn loads the required assembly into memory. The runtime engine is responsible for a number of tasks, resolving the location of an assembly and finding the requested type within the binary by reading the contained metadata.
The CLR then lays out the type in memory, compiles the associated CIL into platformspecific instructions, performs any necessary security checks, and then executes the code.
To enable the runtime to provide services to managed code, language compilers must emit metadata that describes the types, members, and references in your code. Metadata is stored with the code; every loadable common language runtime portable executable (PE) file contains metadata. The runtime uses metadata to locate and load classes, lay out instances in memory, resolve method invocations, generate native code, enforce security, and set run-time context boundaries.
In addition to loading custom assemblies and creating custom types, the CLR will also interact with the types contained within the .NET base class libraries when required. Although the entire base class library has been broken into a number of discrete assemblies, the key assembly is mscorlib.dll. mscorlib.dll contains a large number of core types that encapsulate a wide variety of common programming tasks as well as the core data types used by all .NET languages.
Common Type System
In .NET, type is simply a general term used to refer to a class, interface, structure, enumeration, delegate. The CTS specification fully describes all possible data types and programming constructs supported by the runtime, specifies how these entities can interact with each other, and details how they are represented in the .NET metadata format.
CTS Class Types
a class type, which is the cornerstone of object-oriented programming(OOP). In C#, classes are declared using the class keyword
// A C# class type.
class Calc
{
public int Add(int x, int y)
{
return x + y;
}
}
CTS Class Characteristics
- Is the class “sealed” or not? Sealed classes cannot function as a base class to other classes.
- Does the class implement any interfaces? An interface is a collection of abstract members that provide a contract between the object and object user. The CTS allows a class or structure to implement any number of interfaces.
- Is the class abstract or concrete? Abstract classes cannot be directly created, but are intended to define common behaviors for derived types. Concrete classes can be created directly.
- What is the “visibility” of this class? Each class must be configured with a visibility attribute. Basically, this trait defines whether the class may be used by external assemblies or only from within the defining assembly
CTS Interface Types
Interface is a named collection of abstract member definitions.In C#, interface types are defined using the interface keyword.
// A C# interface type is usually declared as public, to allow types in other
// assemblies to implement their behavior.
public interface IDraw
{
void Draw();
}
CTS Structure Types
A structure can be thought of as a lightweight class type having value-based semantics. In C# structures are defined using struct keyword.
// A C# structure type.
struct Point
{
// Structures can contain fields.
public int xPos, yPos;
// Structures can contain constructors.
public Point(int x, int y)
{
xPos = x;
yPos = y;
}
// Structures may define methods.
public void Display()
{
Console.WriteLine("({0}, {1}", xPos, yPos);
}
}
CTS Enumeration Types
Enumerations are handy programming constructs that allow you to group name/value pairs.
// A C# enumeration type.
enum CharacterType
{
Wizard = 100,
Fighter = 200,
Thief = 300
}
CTS Delegate Types
Delegates are the .NET equivalent of a type-safe C-style function pointer. The key difference is that a .NET delegate is a class that derives from System.MulticastDelegate, rather than a simple pointer to a raw memory address. In C#, delegates are declared using the delegate keyword
// This C# delegate type can "point to" any method
// returning an integer and taking two integers as input.
delegate int BinaryOp(int x, int y);
CTS Type Members
A type member is constructor, finalizer, static constructor, nested type, operator, method, property, indexer, field, read-only field, constant, event.
Intrinsic CTS Data Types
All language keywords ultimately resolve to the same type defined in an assembly named mscorlib.dll.
Common Language Specification
The CLS 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. CLS can be viewed as a subset of the full functionality defined by the CTS. The CLS is ultimately a set of rules that compiler builders must conform to, if they intend their products to function seamlessly within the .NET universe.
Ensuring CLS Compliance
// Tell the C# compiler to check for CLS compliance.
[assembly: System.CLSCompliant(true)]
The
Intermediate Language Disassembler utility (ildasm.exe), allows us to load up any .NET assembly and investigate its contents, including the associated manifest, CIL code, and type metadata.