Monday, 9 January 2012

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
'This is the famous HelloWorld Program written using VB.NET
Namespace HelloWorldSample
'Definition of the Class
Public Class HelloWorld
'entry point method for the Class
Public Shared Sub Main()
System.Console.WriteLine("HelloWorld")
End Sub
'end of Class Declaration
End Class
'end of the Class Module
'end of namespace
End Namespace

This is how it goes in your favorite editor ‘Notepad’
   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
/* This is the famous helloworld program written using C#.NET */
/* Indicates that the code is referring System Namespace to access the
functionality’s of System.dll */
using System;
// Namespace name given for the class
namespace HelloWorldSample
{
//Definition of the class
public class HelloWorld
{
// Entry point method for the class
public static void Main()
{
//Displaying helloworld in the screen
System.Console.WriteLine("HelloWorld");
}
//end of the class declaration
}
//end of the namespace
}
Figure showing HelloWorld program written using C#.NET in Notepad
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)
   Figure shows compilation of the HelloWorld program using C# compiler
  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.