By Kamran Shakil
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 declare Main 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 in no return value, that is, void. If it returns an int, by convention, a value of zero means success and any other value indicates an error. No international authority can standardize the error numbers, it largely depends on the programmer himself.
Copy as a command takes two parameters; the source and destination files. Similarly, a C# program can also accept command line parameters at runtime. The executable kamran.exe can be entered 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 line parameters. The program accepts them in an array of strings. As the array is a parameter to the function, we are free to decide on its name. Every array has a member called Length, which tells us the size of the array. In our case, it is three. Thus, a[0] will contain the first word one and not the name of the program, a[1] will contain two and a[3] - three. Main now behaves like any other function. What we do next 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 one function called Main in any C# program. Even though you can call it with different parameters, with the name changing, Main as a function must 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 first displays a warning that Main has not been created with the right parameters. The error, following the warning, proclaims that we have forgotten to create a function called Main. The signature includes the return 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 the parameters given to the function plus the return value. Main in the above 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 includes modifiers like static etc. It just proves the importance of the function 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 cannot have two different classes, zzz and yyy, containing Main. Only one occurrence of Main is allowed. You always have only one chance of a lifetime. Ditto with Main.
kamran.cs
class zzz
{
private static void Main()
{
System.Console.WriteLine("hell");
}
}
Output
hell
The access modifiers are not included within the signature for Main. Even though Main is made private, it is considered as an entry point function, hence hell gets displayed. This function 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 function Main has to return an int and we are not returning any value with the keyword return. The compiler tries to convert a nothing into an int and hence the error. This is a generic error that occurs whenever the compiler tries to convert a data type into another and is not successful.
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 messages need 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 an entity should return a value, we should return such a data type or be prepared for an error.