.NET console application exit code wrong in visual studio debugger output window when using return from within Main

273 Views Asked by At

I've read about different ways to set the exit code for a .NET console application here and here and at other places. And they all result in the correct value in the %ERRORLEVEL% after executing the console app from within a cmd.exe (or in $LastExitCode when executing from within powershell) on windows.

However, when i create a simple C# or VB.NET console application and run it from the debugger, the exit code listed in the debugger output window is always zero unless, instead of using return, Environment.Exit(Int32) or Environment.ExitCode are used or when the Visual Studio hosting process is disabled in the project options.

The debug output from both,

namespace TestDotNetConsoleAppExitCode
{
    class Program
    {
        static int Main(string[] args)
        {
            return -3;
        }
    }
}

and

Module Module1
    Function Main() As Integer
        Return -3
    End Function
End Module

results in

The program '[...] ....vshost.exe' has exited with code 0 (0x0).

This happens at least on Win10 and in VS2010 and VS2015 with all .NET frameworks from 2.0 to 4.7.2, but not with .NET Core (there's no vshost there).

Is there some project or visual studio option i'm missing?

0

There are 0 best solutions below