
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
How to use #error and #warning directives in C#?
#error directive
The #error directive allows generating an error from a specific location in your code.
Let us see an example −
Example
using System; namespace Demo { class Program { public static void Main(string[] args) { #if (!ONE) #error ONE is undefined #endif Console.WriteLine("Generating a user-defined error!"); } } }
After running the above program, a user-defined error generates −
Output
Compilation failed: 1 error(s), 0 warnings error CS1029: #error: 'ONE is undefined'
#warning directive
The #warning directive allows generating a level one warning from a specific location in your code.
Let us see an example −
Example
using System; namespace Demo { class Program { public static void Main(string[] args) { #if (!TWO) #warning TWO is undefined #endif Console.WriteLine("Generates a warning!"); } } }
After running the above program, warning generates and the output is visible −
Output
warning CS1030: #warning: `TWO is undefined' Generates a warning!
- Related Articles
- How to use directives in angular 8?
- How to use #if..#elif…#else…#endif directives in C#?
- How to create custom directives in Angular 8?
- What are JSP Directives?
- C/C++ Preprocessor Directives
- Explain the pre-processor directives in C language
- What are C# pre-processor directives?
- What is if/then directives for debug vs release in C#?
- How to use thread.getName() and thread.setName() in android?
- How to use *args and **kwargs in Python?
- How to use drag and drop in HTML5?
- How to use equals() and equalsIgnoreCase() in Java.
- How to declare and use Interfaces in C#?
- How to use drag and drop in Android?
- How to use getters and setters in TypeScript?

Advertisements