Entry Point in C# program


Following theory is valid on beta1, beta2 and RTM versions of visual studio.net !

The first function to be called in any C#program is Main. We can have as many as four different ways to declareMain in our program. They are as follows:

static void Main() {…}
static int Main() {…}
static void Main(string[] a) {…}
static int Main(string[] args) {…}

note :- String and string words are similar to use. They are alias!

The operating system calls Main and waits for it to return a value. This value denotes the success or failure of the program.

Main can either return a number or result inno return value, that is, void. If it returns an int, by convention, avalue of zero means success and any other value indicates an error. Nointernational authority can standardize the error numbers, it largelydepends on the programmer himself.

Copy as a command takes two parameters; thesource and destination files. Similarly, a C# program can also acceptcommand line parameters at runtime. The executable kamran.exe can beentered as

C:\>kamran one two three

kamran.cs
class zzz
{
public static void Main(string[] a)
{
int i;
for ( i=0; i < a.Length; i++)
System.Console.WriteLine(a[i]);
}
}

Output
one
two
three

one, two, three are called command lineparameters. The program accepts them in an array of strings. As thearray is a parameter to the function, we are free to decide on itsname. Every array has a member called Length, which tells us the sizeof the array. In our case, it is three. Thus, a[0] will contain thefirst word one and not the name of the program, a[1] will contain twoand a[3] – three. Main now behaves like any other function. What we donext with the command line arguments depends entirely upon us.


kamran.cs
class zzz
{
public static void Main(string[] a)
{
}
public static int Main()
{
}
}

Compiler Error
kamran.cs(3,20): error CS0017: Program 'kamran.exe' has
more than one entry point defined: 'zzz.Main(string[])'
kamran.cs(6,19): error CS0017: Program 'kamran.exe' has
more than one entry point defined: 'zzz.Main()'

You can have one and only onefunction called Main in any C# program. Even though you can call itwith different parameters, with the name changing, Main as a functionmust be given only once.

kamran.cs
class zzz
{
public static void Main(int i)
{
}
}

Compiler Warning
kamran.cs(3,21): warning CS0028: 'zzz.Main(int)' has the
wrong signature to be an entry point

Compiler Error
error CS5001: Program 'kamran.exe' does not have an entry point defined

Here, the compiler firstdisplays a warning that Main has not been created with the rightparameters. The error, following the warning, proclaims that we haveforgotten to create a function called Main. The signature includes thereturn type only in special cases as entry point.


kamran.cs
class zzz
{
public static long Main()
{
return 0;
}
}

Compiler Warning
kamran.cs(3,20): warning CS0028: 'zzz.Main()' has
the wrong signature to be an entry point

Compiler Error
error CS5001: Program 'kamran.exe' does not have an
entry point defined

The signature refers to theparameters given to the function plus the return value. Main in theabove program returns 'long', hence we see the error.


kamran.cs
class zzz
{
public void Main()
{
}
}

Compiler Error
error CS5001: Program 'kamran.exe' does not have an entry point defined

The signature also includesmodifiers like static etc. It just proves the importance of thefunction Main and the way it has been defined.


kamran.cs
class zzz {
public static void Main()
{
}
}
class yyy
{
public static void Main()
{
}
}

Compiler Error
kamran.cs(2,21): error CS0017: Program 'kamran.exe' has
more than one entry point defined: 'zzz.Main()'
kamran.cs(8,21): error CS0017: Program 'kamran.exe' has
more than one entry point defined: 'yyy.Main()'

The error proves the point that you cannothave two different classes, zzz and yyy, containing Main. Only oneoccurrence of Main is allowed. You always have only one chance of alifetime. Ditto with Main.

kamran.cs
class zzz
{
private static void Main()
{
System.Console.WriteLine("hell");
}
}

Output
hell

The access modifiers are not included withinthe signature for Main. Even though Main is made private, it isconsidered as an entry point function, hence hell gets displayed. Thisfunction is unique and works as a special case.

kamran.cs
public static void Main() {
System.Console.WriteLine("hell");
}
class zzz
{
}

Compiler Error
kamran.cs(1,15): error CS1518: Expected class, delegate, enum, interface, or struct

You cannot create a function outside a class or a structure. This rule has to be strictly adhered to even for Main.


kamran.cs
public class zzz
{
public static int Main()
{
return;
}
}

Compiler Error
kamran.cs(5,1): error CS0126: An object of a type
convertible to 'int' is required

In this example, the functionMain has to return an int and we are not returning any value with thekeyword return. The compiler tries to convert a nothing into an int andhence the error. This is a generic error that occurs whenever thecompiler tries to convert a data type into another and is notsuccessful.


kamran.cs
class zzz
{
public static int Main()
{
}
}

Compiler Error
kamran.cs(3,19): error CS0161: 'zzz.Main()': not all code paths
return a value

The compiler's error messagesneed the services of an editor. The function Main should return an int,but we forgot to do so. This results in an error. Thus whenever anentity should return a value, we should return such a data type or beprepared for an error. <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0" width="468" height="60"><param name="movie" value="/banners/Ad2.swf?clickTAG=http://www.red-gate.com/products/ants_profiler/index.htm?utm_source=chelp%26utm_medium=banner%26utm_content=vsmenu%26utm_campaign=antsprofiler" /><param name="quality" value="high" /> <embed src="http://www.csharphelp.c

om/banne
rs/Ad2.swf?clickTAG=http://www.red-gate.com/products/ants_profiler/index.htm?utm_source=chelp%26utm_medium=banner%26utm_content=vsmenu%26utm_campaign=antsprofiler" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="468" height="60"></embed> </object>

Twitter Digg Delicious Stumbleupon Technorati Facebook Email

No comments yet... Be the first to leave a reply!