Skip to content

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