C# FAQ: What are the differences between CSharp and Java Main methods

Microsoft .NET Framework, ASP.NET, Visual C# (CSharp, C Sharp, C-Sharp) Developer Training, Visual Studio


Jump to: navigation, search
CSharp-Online.NET:FAQs
edit

What are the differences between C# and Java Main methods?

Both C# and Java classes are entered through a Main method. Superficially, whereas the C# Main method begins with an uppercase "M", the Java main method begins with a lowercase "m". The remainder of the Main method declaration is the same in both languages except that in C# Main() can sport a void parameter.

using System; 
 
class CSharpClass                         // C# example
{
   public static void Main(String[] args)
   {
   }
}
class JavaClass               // Java example
{
   public static void main(String[] args)
   {
   }
}

Professional developers recommend creating main methods for all classes for testing purposes. Thus, there can be many classes in an application containing main methods. C# conditional compilation makes multiple Main methods easy to use for testing.

The class is the unit of compilation in the Java language; so, to invoke a specific class via the command line, simply run its main method. To achieve the same result in C#, compile the application using the /main switch to specify the main to be used as the entry point for the executable application.

The Java language has the advantage that recompilation is unnecessary to change which main method will be used as the entry point of the application. A C# application must be recompiled to achieve this.

On the other hand, the Java system does not support conditional compilation causing any testing main methods to be included with released classes.

See also


Personal tools