It seems that in the future there will be at least two major [[virtual machine]] technologies competing (see [[Java virtual machine]])
The Common Language Runtime: The .NET Framework provides a run-time environment called the Common Language Runtime, which manages the execution of code and provides services that make the development process easier. Compilers and tools expose the runtime's functionality and enable you to write code that benefits from this managed execution environment. Code that you develop with a language compiler that targets the runtime is called managed code; it benefits from features such as cross-language integration, cross-language exception handling, enhanced security, versioning and deployment support, a simplified model for component interaction, and debugging and profiling services.
The runtime automatically handles object layout and manages references to objects, releasing them when they are no longer being used. Objects whose lifetimes are managed in this way by the runtime are called managed data. Automatic memory management eliminates memory leaks as well as some other common programming errors.
The Common Language Runtime makes it easy to design components and applications whose objects interact across languages. Objects written in different languages can communicate with each other, and their behaviors can be tightly integrated. For example, you can define a class, then, using a different language, derive a class from your original class or call a method on it. You can also pass an instance of a class to a method on a class written in a different language. This cross-language integration is possible because language compilers and tools that target the runtime use a common type system defined by the runtime, and they follow the runtime's rules for defining new types, as well as creating, using, persisting, and binding to types.
Registration information and state data are no longer stored in the registry where it can be difficult to establish and maintain; instead, information about the types you define (and their dependencies) is stored with the code as metadata, making the tasks of component replication and removal much less complicated. The Common Language Runtime can only execute code in assemblies. An assembly consists of code modules and resources that are loaded from disk by the runtime. The assembly may be an executable (exe) or a library (dll).
The benefits of the runtime are as follows:
Performance improvements.
The ability to easily use components developed in other languages.
Extensible types provided by a class library.
A broad set of language features.
§1.2 The Intermediate Language: Currently Microsoft provides compilers for C# (Pronounced as C Sharp), VisualBasic.NET, Managed C++ and JScript in their .NET Framework SDK. These compilers generate the so called Intermediate Language(IL) code which is then assembled to a Portable Executable(PE) code. The PE code is interpreted at runtime by CLR for execution. Our first example is the traditional ‘Hello World’ program written in IL. (ilhello.il)
.assembly Hello{}
.method public static void run() il managed{
.entrypoint
ldstr “Hello World.NET”
call void [mscorlib]System.Console::WriteLine(class System.String)
ret
}
The above code declares Hello as an assembly(unit of code) and defines an il managed function called run. This function is marked as an entrypoint (the function which the CLR must invoke when this code is executed). Next it stores a string on the stack and invokes WriteLine method of Console class (this method displays a line on screen). For resolving name collisions all classes in .NET library are packaged in namespaces. The Console class for instance belongs to namespace called System (the root of all the namespaces in the .NET framework class library). The code for System.Console class is stored in mscorlib.dll. To execute this code, it must be first assembled to PE code using ilasm.exe utility available with .NET Framework SDK.
ilasm ilhello.il
The above command will create ilhello.exe which when executed will display
Hello World.NET
on the screen.
Needless to say that coding in raw IL is pretty difficult. For actual programming any .NET compliant high level language can be used. Given below (mcpphello.cpp) is the Hello World program coded ni Managed C++
#using <mscorlib.dll>
using namespace System;
void main()
{
Console::WriteLine(S"Hello World.NET");
}
Compile it using command cl /clr mcpphello.cpp
The listing below shows same program coded in VisualBasic.NET(vbhello.vb).
Imports System
Module Hello
Sub Main()
Console.WriteLine(“Hello World.NET”)
End Sub
End Module
Compile vbhello.vb using command:vbc vbhello.vb
The code below is hello program in JScript (jshello.js)
import System;
Console.WriteLine(“Hello World.NET”);
Compile jshello.js using command jsc jshello.js
Finally the Hello World program in C# would be (cshello.cs)
using System;
class Hello{
public static void Main(){
Console.WriteLine(“Hello World.NET”);
}
}
And it can be compiled using CSharp compiler as: csc cshello.cs
§1.3 Mixed Language Programming: The .NET framework not only allows you to write your program in any language but also allows you to use components coded in one language in a program written in another. Listed below is a component implemented in C#(bizcalc.cs) for calculating the price of a depreciating asset at an end of a given period:
namespace BizCalc{
public class Asset{
public double OriginalCost;
public float AnnualDepreciationRate;
public double GetPriceAfter(int years){
double price = OriginalCost;
for(int n = 1; n <= years; n++) price = price * (1 - AnnualDepreciationRate/100);
return price;
}
}
}
Compile asset.cs to create a dll using command: csc /t:library bizcalc.cs
A VB program below (assettest.vb) uses the above component:
Imports BizCalc
Imports System
Module AssetTest
Sub Main()
Dim ast As Asset = new Asset()
ast.OriginalCost = 10000
ast.AnnualDepreciationRate = 9
Console.WriteLine("Price of asset worth 10000 after 5 years: {0}", _
ast.GetPriceAfter(5))
End Sub
End Module
Compile the above program using: vbc /r:bizcalc.dll assettest.vb
|