Program Entry point in C#

Additional Examples

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.

Happy .NETing

You might also like...

Comments

About the author

Kamran Shakil Pakistan

I am 22 male. BS(Computer Science), MCSE, Brainbench certifed. Member of .NET Open source, Mono Project. E-author on various websites, including www.dotnetextreme.com, www.csharphelp.com and so...

Interested in writing for us? Find out more.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Nine people can't make a baby in a month.” - Fred Brooks