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
to be done

If line 6 is replaced by:

6: #error Main not yet completed

a compilation error is produced, and no EXE file is created:

c:expt>csc test.cs
test.cs(6,8): error CS1029: #error: 'Main not yet
completed'

#warnings and #errors are useful for absent minded developers dealing with lots of code simultaneously, and who need some helpful self-imposed reminders.

Note that you can place #warnings and #errors between #if and #endif 'blocks' so that a compilation warning or error appears only when certain symbols are defined.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.139.70.101