Книга: C# 2008 Programmer
#pragma warning
#pragma warning
The #pragma warning
directive enables or disables compiler warning messages. For example, consider the following program:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TestDefine {
class Program {
int num = 5;
static void Main(string[] args) {}
}
}
In this program, the variable num
is defined but never used. When you compile the application, the C# compiler will show a warning message (see Figure 3-20).
Figure 3-20
To suppress the warning message, you can use the #pragma warning
directive together with the warning number of the message that you want to suppress:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
#pragma warning disable 414
namespace TestDefine {
class Program {
int num = 5;
static void Main(string[] args) {}
}
}
This example suppresses warning message number 414 ("The private field 'field' is assigned but its value is never used"). With the #pragma warning
directive, the compiler will now suppress the warning message (see Figure 3-21).
Figure 3-21
You can suppress multiple warning messages by separating the message numbers with a comma (,) like this:
#pragma warning disable 414, 3021, 1959