EN
C# / .NET - return status code from Main()
0 points
In this short article, we would like to show how to return the status code from the Main function in C#.
Quick solution:
xxxxxxxxxx
1
// put at the end of the Main()
2
3
Environment.Exit(code);
4
5
// e.g.
6
Environment.Exit(2);
xxxxxxxxxx
1
using System;
2
3
public class TestClass
4
{
5
public static void Main()
6
{
7
Environment.Exit(2); // 0 usually means the program ended successfully
8
// other numbers describe error code
9
// e.g.
10
// here we returned error with code 2
11
}
12
}
Example output:
xxxxxxxxxx
1
The program '[4476] ConsoleApp1.exe' has exited with code 2 (0x2).
Above code is equivalent to the following c++ / cpp code version:
xxxxxxxxxx
1
using namespace std;
2
3
int main()
4
{
5
return 2; // 0 usually means the program ended successfully
6
// other numbers describe error code
7
// e.g.
8
// here we returned error with code 2
9
}