First C#.NET Program

Unknown | 3:05 AM |

/* 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.

Category:

About http://dotnetvisual.blogspot.in/:
DOT NET TO ASP.NET is a web application framework marketed by Microsoft that programmers can use to build dynamic web sites, web applications and web services. It is part of Microsoft's .NET platform and is the successor to Microsoft's Active Server Pages (ASP) technology. ASP.NET is built on the Common Language Runtime, allowing programmers to write ASP.NET code using any Microsoft .NET language. create an application very easily ....