2

In C#, you can use 4 different method signatures for your main method:

static void Main(string[] args) {}

static int Main(string[] args) { return 0; }

static void Main () {}

static int Main {return 0;}

While studying further about Main, the reference noted that value 0 is automatically returned even if you construct a Main() method prototyped to return void.

This being said, why would you use a Main method whose signature returned an int if it will be returned regardless? I understand that the return is kept in a variable named %ERRORLEVEL% and that you can capture this code with a batch file I was just curious about the return value always being provided regardless. Thanks.

Hermes Trismegistus
  • 515
  • 2
  • 5
  • 16
  • 5
    The return value can be used in another application. This is the standard behaviour for files that are running in batch files and need to report errors. – Julo Jan 11 '17 at 14:41
  • It is useful in case when you application is used as commend line utility and in case of application fail you can return -1 . One example automatic database deployment utility which reads some sql files and execute them to target server . The benefit is that you can add you utility in build / deployment tools and if it fails they will get error . – Yashveer Singh Jan 11 '17 at 14:45

3 Answers3

3

They're called Exit codes

It is a status or execution code that your program returns. It's somewhat legacy these days, but in general it is meant to let the caller know how the execution went.

Zero typically means successful execution.

Just as a function in your code returns a value to it's caller, so should your program return a value to its caller, like a script, or the operating system.

Return codes is a relic from the past, but is still used in some cases, like console applications, because it is simple and easy to handle.

Why should you use one over the other?

If you have no intention of returning an error code, C# has added the syntactic sugar of allowing void, so you don't need to think about returning anything. And in most cases, this is fine.

But if you are developing a console application, and you want to let the caller handle any results from your code, return codes are an easy and standardized way to do this.

static int Main(string[] args) { return 0; } // Everything went fine

static int Main(string[] args) { return -1; } // An error occured.

static int Main(string[] args) { return 2; } // Program completed, with warnings.

static void Main(string[] args) {  } // Just returns zero.

What the codes mean is up to each program, and would be documented in the documentation or similar.

See this answer for how it can be used in bat files:

Example .bat file

@echo off
myprogram.exe
if errorlevel 1 (
   echo Failure Reason Given is %errorlevel%
   exit /b %errorlevel%
)
Community
  • 1
  • 1
André Snede
  • 9,899
  • 7
  • 43
  • 67
1

If you rely on the void syntax returning a 0, then your program is only ever going to return a 0, which is by convention the status code for a successful execution. For many use cases, that may be fine, but the point of allowing your main method to explicitly return an int is that you can return different ints in different cases. For a non-trivial application, if you manually return different int values in meaningful contexts, you're going to get a lot more value out of that functionality than blindly returning a 0 in all termination scenarios.

Additionally, it seems like you're asking about why you should bother explicitly saying static int Main instead of static void Main if both will return an integer. Besides the above note about returning meaningful values, more broadly, if you want your method to return an integer, then it would make sense to utilize the explicit syntax rather than relying on fallback behavior, simply because doing the latter may be unknown or non-intuitive to someone else modifying or using your program. Look at your question the other way - if you wanted your main method to return an int, why wouldn't you use the syntax to do so explicitly? Does it make sense for a void method to return an int?

Sam Hanley
  • 4,707
  • 7
  • 35
  • 63
-2

The return value is the errorlevel, you can use the errorlevel in batchfiles.