如何在C#中使用#error和#warning指令?
#error指令
#error指令允许从代码中的特定位置生成错误。
让我们看一个例子-
示例
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!");
}
}
}运行上面的程序后,用户定义的错误生成-
输出结果
Compilation failed: 1 error(s), 0 warnings error CS1029: #error: 'ONE is undefined'
#warning指令
#warning指令允许从代码中的特定位置生成一级警告。
让我们看一个例子-
示例
using System;
namespace Demo {
class Program {
public static void Main(string[] args) {
#if (!TWO)
#warning TWO is undefined
#endif
Console.WriteLine("产生警告!");
}
}
}运行上述程序后,将生成警告,并且输出可见-
输出结果
warning CS1030: #warning: `TWO is undefined' 产生警告!
热门推荐
1 狗狗下楼训练
3 狗狗训练头盔
6 狗狗训练机构
7 训练狗狗步骤
9 狗狗能训练
10 狗狗基本动作训练方法
11 怎么训练狗狗叼饭盆
12 狗狗一岁了还能训练吗
13 训练狗狗的
14 宁波狗狗训练
15 训练狗狗拉屎
16 教狗狗训练
17 狗狗要怎么训练上厕所
18 狗狗靠的训练