Ads 468x60px

.

First VB.NET / C# program

To start of with any language it’s always worth writing a simple program, which actually does nothing but displays a “HelloWorld” string in the screen. Taking this simple program we will try to figure out how .NET framework delivers a successful HelloWorld.exe. Well to write such a complex program we will go for our favorite editor, the choice is unanimous, it’s “Notepad”.

First VB.NET Program
Figure showing HelloWorld program written using VB.NET
CODES:


Now let us spend sometime in examining the HelloWorld program to find out what’s new in writing code through any .NET language. The lines in the program that starts with a ‘(single quote) are comment entries like in other programming languages which are excluded in the compilation process. Like VB the way in which comment entries are represented remains the same in VB.NET.
Namespace HelloWorldSample - The keyword “Namespace” is new to some programmers who are not familiar with C++.
‘Namespace’ – a keyword in .NET is used to avoid name collisions i.e. For example, you develop a library which has a class named “File” and you use some other library which also has a class named “File”, in those cases there are chances of name collision. To avoid this you can give a namespace name for your class, which should be meaningful. It is always better to follow the syntax (MS Recommended) given below while giving names for your namespaces
CompanyName.TechnologyName
However the hierarchy can be extended based on the implementation of the classes in the library.
Public Class HelloWorld - This is the class declaration in VB.NET; the interesting thing for VB developers is that VB.NET is a fully object-oriented language (so everything is a Class here) . The class always ends with an “End Class”.
‘Public’ - is the modifier to determine the scope of the class (for other modifiers refer .NET framework SDK documentation or later parts of this tutorial). HelloWorld is the class name given for the class. Consumers of the class will be accessing through this name only
Public Shared Sub Main () - This is called as the entry point function because the runtime after loading your applications searches for an entry point from which the actual execution starts. C/C++ programmers will find this method very familiar (VB Programmers remember Sub Main). All Applications (exe) must have a definition for the Main Method. Try removing the Main method from your application and the compiler will complain that "No Start Point Defined". This means that the Main Method is the starting point of any application, in other words When you execute your Application "Main" method is called first automatically.
'Public' - This is the Access modifier for the Method. Since the Main method should be accessible to everyone in order for the .NET Runtime to be able to call it automatically it is always defined as public.
'Shared' - indicates that the method is a Class Method. Hence it can be called without making an instance of the class first. 

Now its time to compile and execute this complex program. To compile the above piece of code you can use VB.NET compiler. To run the VB.NET compiler make sure you set your path variable to point to the place where your VB.NET compiler is available. (To set a new value in the path variable, go to control panel and double click System icon, then choose advanced tab and click Environment Variables button to add or edit the environmental variables)
Figure shows compilation of the HelloWorld program for VB.NET
The compiler used here is “vbc”, which is a visual basic .net compiler accepts the source file “HelloWorld.vb” compiles the same to produce a program that’s not true executable, instead it generates something called assembly. Here the VB.NET compiler produces a Managed Code/Intermediate Language (MSIL) format that uses instructions which are CPU-independent.

First C#.NET Program
CODES:


The lines in the program that starts with a // and /*….*/ (comment blocks) are comment entries like in other programming languages which are excluded in the compilation process. For C or C++ programmers the C# style of coding sounds great because it almost follows the same style.
namespace HelloWorldSample - The keyword “namespace” is new to some programmers who are not familiar with C++.
‘namespace’ – a keyword in .NET is used to avoid name collisions i.e. For example, you develop a library which has a class named “File” and you use some other library which also has a class named “File”, in those cases there are chances of name collision. To avoid this you can give a namespace name for your class, which should be meaningful. It is always better to follow the syntax (MS Recommended) given below while giving names for your namespaces
CompanyName.TechnologyName
However the hierarchy can be extended based on the implementation of the classes in the library.
public class HelloWorld - This is the class declaration in C#.NET; the interesting thing for C++ or Java developers is that they can apply the OOPS concepts that are supported by C#.NET . The class always ends with an “End Class”.
‘public’ - is the modifier to determine the scope of the class (for other modifiers refer .NET framework SDK documentation or later parts of this tutorial). HelloWorld is the class name given for the class. Consumers of the class will be accessing through this name only.
public static void Main () - This is called as the entry point function because the runtime after loading your applications searches for an entry point from which the actual execution starts. C/C++ programmers will find this method very familiar (VB Programmers remember Sub Main). All Applications (exe) must have a definition for the Main Method. Try removing the Main method from your application and the compiler will complain that "No Start Point Defined". This means that the Main Method is the starting point of any application, in other words When you execute your Application "Main" method is called first automatically.
'public' - This is the Access modifier for the Method. Since the Main method should be accessible to everyone in order for the .NET Runtime to be able to call it automatically it is always defined as public.
'static' - indicates that the method is a Class Method. Hence it can be called without making an instance of the class first.
‘void’ – indicates the return type of the Main function, here in this case the Main function returns nothing so it is mentioned as void, for functions that returns value should have appropriate type such as long, string etc., 

Now its time to compile and execute this complex program. To compile the above piece of code you can use C# compiler. To run the C# compiler make sure you set your path variable to point to the place where your C# compiler is available. (To set a new value in the path variable, go to control panel and double click System icon, then choose advanced tab and click Environment Variables button to add or edit the environmental variables)

The compiler used here is “csc”, which is a visual basic .net compiler accepts the source file “HelloWorld.cs” compiles the same to produce a program that’s not true executable, instead it generates something called assembly.

Assembly
An assembly is a grouping of files deployed as a single file. An assembly almost always consists of at least two files: the executable and the manifest. The manifest is a list of all the files that exist inside the assembly. The executable content inside the assembly is referred to individually as a module. Conceptually, modules correspond to DLLs or EXEs; each module contains metadata, in addition to the metadata of its parent assembly. The assembly format is an enhanced version of the current Portable Executable (PE) format (your normal Windows .EXE file format).

Manifest
Manifest is considered as the integral part of every assembly that renders the assembly self-describing. The assembly manifest contains the assembly's metadata and it also establishes the assembly identity, specifies the files that make up the assembly implementation, specifies the types and resources that make up the assembly, itemizes the compile-time dependencies on other assemblies, and specifies the set of permissions required for the assembly to run properly.

Metadata
The standard PE header comes at the beginning of the file. Inside the file is the CLR header, followed by the data required to load the code into its process space—referred to as metadata. It describes to the execution engine how the module should be loaded, what additional files it needs, how to load those additional files, and how to interact with COM and the .NET runtime.

Metadata also describes the methods, interfaces, and classes contained in the module or assembly. The information the metadata provides allows the JIT compiler to compile and run the module. The metadata section exposes much of your application's internals and eases the transition from disassembled IL to useful code.

Introduction

We all know that there have been disparities between different languages such as VB, VC++ and developers, who code program through these languages. The disparity lies in terms of language features, performance, and flexibility in developing any piece of program. Well it’s a known fact that, at the end, what matters is how efficiently your programs run on the client machine, no matter what language you use. Earlier this was driven by compilers, which were used to compile the code written using these languages to make it native code (processor specific).

With the release .NET framework Microsoft has driven out the disparities in such a way that no matter whatever .NET language you use to develop .NET applications, still the end result will be determined by .NET framework runtime and not by the language  compilers as it was happening earlier. In this tutorial we will identify some of the key elements of the .NET framework through a simple program and concentrate on how .NET framework Runtime addresses platform or processor specific code issues to produce optimized code, which is native to the processor and to know how the framework helps in managing code effectively. 

Common Language Runtime (CLR) 
The primary function of a runtime is to support and manage the execution of code targeted for a language or a platform. For example, the Microsoft VC++ requires the msvcrt60.dll that contains its core support functionality. Even languages like Java have a run time, in the form of Java Virtual Machine. 

The .Net platform also comes with a runtime that is officially called as the Common Language Runtime or simply the CLR. The CLR is designed to support a variety of different types of applications, from Web server applications to applications with traditional rich Windows user interface. Though the role of the CLR is similar to its counterparts in other languages or platforms, there are some key differences that make it one of the major features of the .NET platform. Here are the key differences between the .NET CLR and runtime of other languages:
  • It is a common runtime for all languages targeting the .NET platform.
  • It acts as an agent that manages code at execution time and also provides core services such as memory management, thread management and remoting.
  • It enforces strict type safety and other forms of code accuracy that ensure security and robustness.
  • It is responsible for enabling and facilitating the Common Type System. The Common Type System allows classes that are written in any .NET language to interoperate with—even inherit from, with overrides—classes written in any language. So your COBOL.NET program can interoperate with your C#, VB.NET, Eiffel.NET and with any other .NET language programs.
  • It offers a mechanism for cross-language exception handling.
  • It provides a more elegant way for resolving the versioning issues (also referred to as the Dll Hell in our classic COM).
  • It provides a simplified model for component interaction. 
Code that targets the runtime is known as managed code, while code that does not target the runtime is known as unmanaged code. Managed code requires a runtime host to start it. The responsibility of the runtime host is to load the runtime into a process, create the application domains (we’ll look at this in detail later) within the process, and loads the user code into the application domains. While we can write our own runtime hosts using the set of APIs provided by Microsoft, the .NET platform by default ships with runtime hosts that include the following.
ASP.NET – Loads the runtime into the process that is to handle the Web request. ASP.NET also creates an application domain for each Web application that will run on a Web server.
Microsoft Internet Explorer – Creates application domains in which to run managed controls. The .NET Framework supports the download and execution of browser-based controls. The runtime interfaces with the extensibility mechanism of Microsoft Internet Explorer through a mime filter to create application domains in which to run the managed controls. By default, one application domain is created for each Web site.
Shell executables – Invokes runtime hosting code to transfer control to the runtime each time an executable is launched from the shell. 
Now that you have understood conceptually the key features of the CLR in .NET framework, you can begin to look into the physical implementation and execution of code in the CLR. 

The following figure illustrates the flow of activities from the source code to its execution.