24.4. #warning and #error

The directives #warning and #error are used to make the compiler display a compiler warning or error message. For warnings, the compiler displays the warning message and then carries on with the compilation. Compilation warnings do not cause compilation to terminate. On the other hand, with errors the compiler displays an error message and terminates the compilation process. Here is an example:

 1: using System;
 2:
 3: public class TestClass{
 4:
 5:   public static void Main(){
 6: #warning Main not yet completed
 7:     Console.WriteLine("to be done");
 8:     // insert code here after New Year holidays
 9:     //...
10:   }
11: }

Compilation warning:

test.cs(6,11): warning CS1030: #warning: 'Main not yet
completed'

Output:

 c:\expt>test ...

Get From Java to C#: A Developer's Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.