できない.dev

C#「CS5001: Program does not contain a static 'Main' method」の原因と直し方

CS5001: Program does not contain a static 'Main' method は、static な Main かトップレベルステートメントを置けば直る。
ライブラリなら OutputType を Library にする。
Exe のプロジェクトに入口が無いと出る。

公開: 更新:

症状できない · exit 1
dotnet build
…
CSC : error CS5001: Program does not contain a static 'Main' method suitable for an entry point [/tmp/tmp.Mh4vu8HhdX/App/App.csproj]
Build FAILED.
解決後できた · exit 0
dotnet build
…
    0 Warning(s)
    0 Error(s)
Time Elapsed 00:00:00.81

2026-11-03 に mcr.microsoft.com/dotnet/sdk:8.0(.NET SDK 8.0.426) で実際に打って取った出力。検証環境の詳細

要約

dotnet new console で作ったプロジェクトの Program.cs を空にして dotnet build すると、コンパイルの最後で止まった。
.NET SDK 8.0.426 では次のように出て、終了コードは 1 だった。

CSC : error CS5001: Program does not contain a static 'Main' method suitable for an entry point [App.csproj]

CS5001 は、実行ファイル(Exe)を作るプロジェクトなのに、プログラムの入口が見つからないという意味だ。
入口になるのは、static な Main メソッドか、トップレベルステートメント(Main を書かずにファイルの先頭から処理を書く形)のどちらかである。
どちらも無いと、プロジェクト全体に対してこのエラーが出る。
そのため行番号は付かない。
公式のエラー一覧(新しいタブで開く)によると、CS5001 はビルド(またはリビルド)のときにだけ報告される。

実行例

入口の無い Program.cs で dotnet build すると、CS5001 で失敗して終了コード 1 になった。
static async void Main では CS4009 も同時に出たが、Task を返す Main にすると終了コード 0 でビルドが通った。

$ dotnet build
  Determining projects to restore...
  All projects are up-to-date for restore.
CSC : error CS5001: Program does not contain a static 'Main' method suitable for an entry point [/tmp/tmp.Mh4vu8HhdX/App/App.csproj]
 
Build FAILED.
 
CSC : error CS5001: Program does not contain a static 'Main' method suitable for an entry point [/tmp/tmp.Mh4vu8HhdX/App/App.csproj]
    0 Warning(s)
    1 Error(s)
 
Time Elapsed 00:00:01.86
$ echo $?
1
$ dotnet build
  Determining projects to restore...
  All projects are up-to-date for restore.
CSC : error CS5001: Program does not contain a static 'Main' method suitable for an entry point [/tmp/tmp.Mh4vu8HhdX/App/App.csproj]
 
Build FAILED.
 
CSC : error CS5001: Program does not contain a static 'Main' method suitable for an entry point [/tmp/tmp.Mh4vu8HhdX/App/App.csproj]
    0 Warning(s)
    1 Error(s)
 
Time Elapsed 00:00:00.81
$ echo $?
1
$ dotnet build
  Determining projects to restore...
  All projects are up-to-date for restore.
CSC : error CS5001: Program does not contain a static 'Main' method suitable for an entry point [/tmp/tmp.Mh4vu8HhdX/App/App.csproj]
 
Build FAILED.
 
CSC : error CS5001: Program does not contain a static 'Main' method suitable for an entry point [/tmp/tmp.Mh4vu8HhdX/App/App.csproj]
    0 Warning(s)
    1 Error(s)
 
Time Elapsed 00:00:00.92
$ echo $?
1
$ dotnet build
  Determining projects to restore...
  All projects are up-to-date for restore.
CSC : error CS5001: Program does not contain a static 'Main' method suitable for an entry point [/tmp/tmp.Mh4vu8HhdX/App/App.csproj]
 
Build FAILED.
 
CSC : error CS5001: Program does not contain a static 'Main' method suitable for an entry point [/tmp/tmp.Mh4vu8HhdX/App/App.csproj]
    0 Warning(s)
    1 Error(s)
 
Time Elapsed 00:00:00.84
$ echo $?
1
$ dotnet build
  Determining projects to restore...
  All projects are up-to-date for restore.
/tmp/tmp.Mh4vu8HhdX/App/Program.cs(5,23): error CS4009: A void or int returning entry point cannot be async [/tmp/tmp.Mh4vu8HhdX/App/App.csproj]
CSC : error CS5001: Program does not contain a static 'Main' method suitable for an entry point [/tmp/tmp.Mh4vu8HhdX/App/App.csproj]
 
Build FAILED.
 
/tmp/tmp.Mh4vu8HhdX/App/Program.cs(5,23): error CS4009: A void or int returning entry point cannot be async [/tmp/tmp.Mh4vu8HhdX/App/App.csproj]
CSC : error CS5001: Program does not contain a static 'Main' method suitable for an entry point [/tmp/tmp.Mh4vu8HhdX/App/App.csproj]
    0 Warning(s)
    2 Error(s)
 
Time Elapsed 00:00:00.68
$ echo $?
1
$ dotnet build
  Determining projects to restore...
  All projects are up-to-date for restore.
  App -> /tmp/tmp.Mh4vu8HhdX/App/bin/Debug/net8.0/App.dll
 
Build succeeded.
    0 Warning(s)
    0 Error(s)
 
Time Elapsed 00:00:00.81
$ echo $?
0
$ dotnet build
  Determining projects to restore...
  All projects are up-to-date for restore.
  App -> /tmp/tmp.Mh4vu8HhdX/App/bin/Debug/net8.0/App.dll
 
Build succeeded.
    0 Warning(s)
    0 Error(s)
 
Time Elapsed 00:00:00.67
$ echo $?
0
$ dotnet build
  Determining projects to restore...
  All projects are up-to-date for restore.
  App -> /tmp/tmp.Mh4vu8HhdX/App/bin/Debug/net8.0/App.dll
 
Build succeeded.
    0 Warning(s)
    0 Error(s)
 
Time Elapsed 00:00:00.67
$ echo $?
0
$ dotnet run
Hello
$ grep OutputType App.csproj
    <OutputType>Library</OutputType>
$ dotnet build
  Determining projects to restore...
  All projects are up-to-date for restore.
  App -> /tmp/tmp.Mh4vu8HhdX/App/bin/Debug/net8.0/App.dll
 
Build succeeded.
    0 Warning(s)
    0 Error(s)
 
Time Elapsed 00:00:00.74
$ echo $?
0

— 2026-11-03 時点の出力

検証環境Git 2.39.5 / .NET SDK 8.0.426 / 2026-11-03 検証
検証日
実行環境
mcr.microsoft.com/dotnet/sdk:8.0 Debian GNU/Linux 12 (bookworm)
バージョン
  • Git 2.39.5
  • .NET SDK 8.0.426

この記事の「実行例」は、上記の環境で実際にコマンドを実行して得られた出力をそのまま掲載しています。 再現手順はリポジトリの検証スクリプトとして管理し、定期的に再実行して出力を更新しています。

解決策

1. 入口を置く

トップレベルステートメントなら、ファイルに処理を書くだけでよい。

Console.WriteLine("Hello");

Main を書くなら、static を付けて大文字で始める。

namespace App;
 
class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Hello");
    }
}

2. 非同期の Main は Task を返す

static async Task Main(string[] args)
{
    await Task.Delay(1);
    Console.WriteLine("Hello");
}

戻り値を Task にするとビルドが通り、dotnet run で Hello が出た。
許される Main の形はMain() and command-line arguments(新しいタブで開く)にまとまっている。

3. ライブラリなら OutputType を変える

<OutputType>Library</OutputType>

.csproj をこのように変えると、入口の無いクラスだけのプロジェクトでもビルドが通った。

よくある原因

  1. 入口を消した。
    Program.cs を空にしたとき、クラス定義だけにしたときのどちらでも CS5001 になった。
  2. 名前や修飾子を誤っている。
    static void main のような小文字の main、static の無い void Main は入口と見なされず、同じく CS5001 になった。
  3. 入口を async void Main にした。
    static async void Main では、次の 2 つが同時に出た。
Program.cs(4,23): error CS4009: A void or int returning entry point cannot be async
CSC : error CS5001: Program does not contain a static 'Main' method suitable for an entry point
  1. ライブラリのつもりで作っていた。
    コンソールアプリのテンプレートは .csproj に <OutputType>Exe</OutputType> を持つ。
    そこに入口の無いクラスだけを置くと CS5001 になる。