C#Mainメソッド戻り値-004
1509 ワード
概要
Mainメソッドの戻り値はプログラムの終了コード(exit code)であり,主にプログラムが正常に実行されたかどうかをシステムに伝えるために用いられる.
linux系では、exit codeの標準定義は次のとおりです. 0- success 1- general errors 126- permission issue 127-Illegal command 128-Invalid arguments and fatal errors 255-Out of range
戻り値はvoidタイプ
戻り値はintタイプ
linux/mac戻り値の表示
プログラムの実行:
上を実行したら、すぐに次のコマンドを実行します. $?保存されているのは、最近実行されたexit code です.注意:In Unix(Posix)、the exit code is an 8-bit value:0-255.
Windowsの戻り値の取得 windowsで実行されるプログラムでは、main()メソッドが返すステータスコードが環境変数ERRORLEVELに格納されます. 例Program.cs:
cscを使用してProgramをコンパイルする.cs生成Program.exe実行可能ファイル
バッチファイルtestを作成します.プログラムを実行するexeを取得し、その戻り値を取得します.
Mainメソッドの戻り値はプログラムの終了コード(exit code)であり,主にプログラムが正常に実行されたかどうかをシステムに伝えるために用いられる.
linux系では、exit codeの標準定義は次のとおりです.
戻り値はvoidタイプ
static void Main()
{
//...
}
戻り値はintタイプ
static int Main()
{
//...
return 0;
}
linux/mac戻り値の表示
プログラムの実行:
dotnet run
上を実行したら、すぐに次のコマンドを実行します.
echo $?
Windowsの戻り値の取得
using System;
namespace _0004MainReturnValue
{
class Program
{
static int Main(string[] args)
{
Console.WriteLine("Hello World!");
return 0;
}
}
}
cscを使用してProgramをコンパイルする.cs生成Program.exe実行可能ファイル
csc.exe Program.cs
バッチファイルtestを作成します.プログラムを実行するexeを取得し、その戻り値を取得します.
@echo off
Program.exe
@if "%ERRORLEVEL%" == "0" goto success
:failed
echo Execution Failed
echo return value = %ERRORLEVEL%
goto end
:success
echo Execution succeeded
echo Return value = %ERRORLEVEL%
goto end
:end