Skip to content

JA CSharp Directive Error

Codex edited this page Oct 11, 2026 · 2 revisions

Українська · Русский · English · Deutsch · Français · Italiano · Español · 繁體中文 · 日本語 · 한국어

明らかなコンパイルエラー

ディレクトリ C# · 実践的な指導

ディレクティブがアクティブなブランチに属している場合、作成者エラーでコンパイルを停止します。例は、意図的な拒否の条件を示しています。

簡単な例 (Example.cs)

この例を Example.cs として別に保存し、このページの他の例とは独立して実行してください。

Example.cs:

// PROJECT-EXAMPLE
#define CONFIGURED
#if !CONFIGURED
#error Missing configuration
#endif
System.Console.WriteLine("Configured");

この例はコンパイルできます。CONFIGURED が定義されているため、#error の分岐は無効です。下にある意図的に失敗する例には引き続き表示を付けています。

結果:

Configured

構文

#error message

これらの例は意図的にコンパイルしません。目的の構成を設定した後、バグを削除するか、非アクティブなブランチに移動します。

ステータス: 意図的なコンパイル エラー。

例 1

#error Missing required configuration
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.IO;
using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;

public static class Program
{
    public static async Task Main(string[] args)
    {
        Console.WriteLine("Never reached");
    }
}

例 2

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.IO;
using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;

public static class Program
{
    public static async Task Main(string[] args)
    {
        #if !CONFIGURED
        #error Define CONFIGURED before compiling
        #endif
        Console.WriteLine("Configured");
    }
}

公式ソース: Microsoft Learn。

Clone this wiki locally