できない.dev

C# の CS0246「型または名前空間名が見つかりませんでした」の原因と直し方

CS0246(型または名前空間名が見つかりませんでした)は、足りない using を足すか、参照を追加すれば直る。
エラー文が挙げる 2 択のどちらなのかを先に切り分け、つづり誤りも疑う。

公開: 更新:

症状できない · exit 1
dotnet build
…
/tmp/tmp.KP8aPlxhVr/app/Program.cs(1,1): error CS0246: The type or namespace name 'JsonSerializerOptions' could not be found (are you missing a using directive or an assembly reference?) [/tmp/tmp.KP8aPlxhVr/app/app.csproj]
/tmp/tmp.KP8aPlxhVr/app/Program.cs(2,19): error CS0103: The name 'JsonSerializer' does not exist in the current context [/tmp/tmp.KP8aPlxhVr/app/app.csproj]
Build FAILED.
解決後できた · exit 0
dotnet build
…
    0 Warning(s)
    0 Error(s)
Time Elapsed 00:00:00.81

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

要約

CS0246 のメッセージは「The type or namespace name could not be found (are you missing a using directive or an assembly reference?)」で、原因の候補を 2 つ挙げている。
つまり using が足りないか、参照そのものが無いかのどちらかだ。

まずどちらなのかを切り分ける。
判断は簡単で、パッケージやプロジェクトを追加した覚えがあるかで分かれる。

dotnet restore
dotnet build

実行例

.NET SDK 8.0.426 で上記の手順を動かすと、using System.Text.Json; が無い状態では JsonSerializerOptions が CS0246、JsonSerializer が CS0103 となり、ビルドは終了コード 1 で失敗した。
using を足すとビルドは通った。
using Newtonsoft.Json; だけを書いた場合は、名前空間 Newtonsoft が見つからず CS0246 で失敗した。
dotnet add package Newtonsoft.Json でバージョン 13.0.4 を追加すると、終了コード 0 でビルドできた。

$ dotnet build
  Determining projects to restore...
  All projects are up-to-date for restore.
/tmp/tmp.KP8aPlxhVr/app/Program.cs(1,1): error CS0246: The type or namespace name 'JsonSerializerOptions' could not be found (are you missing a using directive or an assembly reference?) [/tmp/tmp.KP8aPlxhVr/app/app.csproj]
/tmp/tmp.KP8aPlxhVr/app/Program.cs(2,19): error CS0103: The name 'JsonSerializer' does not exist in the current context [/tmp/tmp.KP8aPlxhVr/app/app.csproj]
 
Build FAILED.
 
/tmp/tmp.KP8aPlxhVr/app/Program.cs(1,1): error CS0246: The type or namespace name 'JsonSerializerOptions' could not be found (are you missing a using directive or an assembly reference?) [/tmp/tmp.KP8aPlxhVr/app/app.csproj]
/tmp/tmp.KP8aPlxhVr/app/Program.cs(2,19): error CS0103: The name 'JsonSerializer' does not exist in the current context [/tmp/tmp.KP8aPlxhVr/app/app.csproj]
    0 Warning(s)
    2 Error(s)
 
Time Elapsed 00:00:01.65
$ echo $?
1
$ dotnet build
  Determining projects to restore...
  All projects are up-to-date for restore.
  app -> /tmp/tmp.KP8aPlxhVr/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.
/tmp/tmp.KP8aPlxhVr/app/Program.cs(1,7): error CS0246: The type or namespace name 'Newtonsoft' could not be found (are you missing a using directive or an assembly reference?) [/tmp/tmp.KP8aPlxhVr/app/app.csproj]
 
Build FAILED.
 
/tmp/tmp.KP8aPlxhVr/app/Program.cs(1,7): error CS0246: The type or namespace name 'Newtonsoft' could not be found (are you missing a using directive or an assembly reference?) [/tmp/tmp.KP8aPlxhVr/app/app.csproj]
    0 Warning(s)
    1 Error(s)
 
Time Elapsed 00:00:00.67
$ echo $?
1
$ dotnet add package Newtonsoft.Json | grep -E "added to file|Restored"
info : PackageReference for package 'Newtonsoft.Json' version '13.0.4' added to file '/tmp/tmp.KP8aPlxhVr/app/app.csproj'.
log  : Restored /tmp/tmp.KP8aPlxhVr/app/app.csproj (in 1.42 sec).
$ dotnet build
  Determining projects to restore...
  All projects are up-to-date for restore.
  app -> /tmp/tmp.KP8aPlxhVr/app/bin/Debug/net8.0/app.dll
 
Build succeeded.
    0 Warning(s)
    0 Error(s)
 
Time Elapsed 00:00:00.64
$ echo $?
0

— 2026-10-21 時点の出力

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

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

解決策

1. 必要な using を足す

エラーが指す型の名前空間を調べて追加する。

using System.Text.Json;
 
var json = JsonSerializer.Serialize(new { name = "dekinai" });

ファイル単位で書く代わりに、プロジェクト全体へ効かせることもできる。

<ItemGroup>
  <Using Include="System.Text.Json" />
</ItemGroup>

2. 参照を追加する

パッケージの場合とプロジェクト参照の場合で、コマンドが変わる。

# NuGet パッケージ
dotnet add package Newtonsoft.Json
 
# 同じソリューション内の別プロジェクト
dotnet add reference ../Core/Core.csproj

3. 復元してからビルドする

参照は書いてあるのに解決されない場合は、復元を挟む。

dotnet restore
dotnet build

Microsoft のドキュメントは、パッケージ操作の後に解決できなくなったときの手順として、キャッシュを消してからの復元も挙げている。

dotnet nuget locals all --clear
dotnet restore

4. クリーンしてから再ビルドする

古い成果物が残って、直したはずの参照が反映されないことがある。

dotnet clean
dotnet build

これでも解決しない場合は、対象フレームワークを確認する。
.csproj の TargetFramework が古いと、新しい API を含む型は見つからない。

<PropertyGroup>
  <TargetFramework>net8.0</TargetFramework>
</PropertyGroup>

SDK 自体が想定と違うバージョンで動いているケースもある。
その切り分けは /csharp/sdk-not-found-global-json/ で説明している。

よくある原因

  1. using が足りない。
    型は参照先に存在するが、名前空間を開いていない。
    IDE を使わずに書いているときに起きやすい。
    ただし JsonSerializer.Serialize(...) のように型名から静的メンバーを呼ぶ箇所は、同じ原因でも CS0246 ではなく CS0103(The name 'JsonSerializer' does not exist in the current context)として報告される。
    型として宣言や new に使った箇所は CS0246 になるので、1 回のビルドで両方が並ぶこともある。
  2. 参照が無い。
    そもそもその型を含むパッケージやプロジェクトを参照していない。
    Microsoft のドキュメントは CS0012 / CS0234 / CS0246 をまとめて「アセンブリ参照が足りないことを示すエラー」として扱っている。
  3. パッケージが復元されていない。
    .csproj に PackageReference はあるのに、パッケージが取得されていない。
    CI やクローン直後によく起きる。
  4. つづりか対象フレームワークが違う。
    型名の大文字小文字が違う、または対象フレームワークに存在しない型を使っている。
    同じ名前で名前空間だけが違う型もある。