Skip to main content

How to Return an Exit Code from a C# Program

· 5 min read
Jagdish Kumawat
Founder @ Dewiride

Every program ends with an exit code: 0 when it succeeded, anything else when it didn't. Shells, scripts and CI jobs read that number to decide what happens next. In a C# program written with top-level statements, return 1; is all it takes to report a failure.

Syntax​

Syntax (top-level statements)
return <code>;
PartWhat it does
returnEnds the program, here at the top level of the file.
<code>The exit code: 0 for success, a non-zero number such as 1 for failure.

Microsoft's Main() return values section: "When you return int or Task<int>, your program can send status information to other programs or scripts that run the executable. A return value of 0 usually means success, and a nonzero value means there's an error." When top-level statements contain return, the compiler generates a Main that returns int.

The Example​

The example checks whether all orders are paid. They aren't, so it writes an error and returns 1.

Prerequisites​

  • The .NET 10 SDK or newer, which runs a single .cs file with dotnet run. This guide uses .NET SDK 10.0.404.

Step 1: Write the Code​

Create a file named main.cs in an empty folder:

main.cs, which sets allPaid to false and, when it isn't true, writes Some orders are not paid. to standard error and returns 1; otherwise it prints All orders are paid. and returns 0.

main.cs
var allPaid = false;

if (!allPaid)
{
Console.Error.WriteLine("Some orders are not paid.");
return 1;
}

Console.WriteLine("All orders are paid.");
return 0;

Once one return has a value, every path through the file must return one, which is why the file ends with return 0;.

Step 2: Run It and Read the Exit Code​

Run the program, then ask the shell for the exit code of the last command:

Terminal
dotnet run main.cs
echo $?

A zsh terminal where dotnet run main.cs prints Some orders are not paid. and echo $? prints 1.

Output
Some orders are not paid.
1

dotnet run passed the program's exit code on, so the shell reported 1.

Two Other Ways​

  • Environment.Exit(2); ends the program at once with code 2, from anywhere, even deep inside a method.
  • Environment.ExitCode = 3; sets the code and lets the program finish normally. A program without a return value ended with code 3 in my check.

Prefer return at the top level: the exit is visible where the program ends.

Common Mistakes​

MistakeWhat happensDo this instead
Writing an error but ending normallyThe exit code is 0, so a CI job carries onReturn 1 after the error
Returning a value on one path onlyerror CS0161: '<top-level-statements-entry-point>': not all code paths return a valueEnd the file with return 0;
Reading $? after another commandIt now holds that command's codeRead it right after running the program

Troubleshooting​

SymptomLikely causeFix
echo $? prints 0 after an error messageThe program never returned a non-zero codeAdd return 1; on the failure path
$LASTEXITCODE is emptyNo program has run yet in this PowerShell windowRun the program first

FAQ​

What exit code does an unhandled exception give?​

A non-zero one. On macOS with .NET 10, a program that crashed with an unhandled exception ended with code 134.

Can I return an exit code from an async program?​

Yes. With both await and return in the top-level statements, the generated Main returns Task<int>, and the value you return becomes the exit code.

Conclusion​

return 1; at the top level of a C# file ends the program with a failure code that the shell reads as $? in zsh or bash and $LASTEXITCODE in PowerShell. Environment.Exit and Environment.ExitCode cover the cases where return doesn't fit.

Additional Resources​