zoukankan      html  css  js  c++  java
  • 【C#】预处理指令的学习 条件编译

    预处理指令,我的理解是,编译器在编译过程中所做的指令,能根据用户定义的条件选择性的编译代码。
    比如在debug下要打印些信息,然而release之后,不能出现debug的信息,这个时候就能用预处理指令。
    还比如不同系统之间,代码要切换,也可以用预处理指令。
     
    常见的预处理指令有这几种:
    1. #define 和#undef我的理解是它们分别用来定义条件和关闭条件的。
     
    2. #if #elif #else # endif 我的理解是它跟if else 一样。
     
    3. #warning ”警告“ 它的作用是在编译器编译时,在输出栏目打印出警告信息。
     
    4. #pragma warning [disable/restore] 169 disable是让编译器忽略警告169
    restore是恢复警告169,这个是用在类上面的。
     
    5. #regin #endregin 这个一般都用来在visualstudio上折叠代码块的,方便我们管理代码。
     
    6. #error "错误" 如果在代码上加了这行预处理命令,
    那么编译代码就不会通过,并且在编译输出窗口打印出”错误“。
     
    7. #line
     
    注意:#define和#undef 一定是放在代码文件的第一行,
    不然会报错的。
    代码:
    #define RELEASE
    #undef DEBUG
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace YuChuLiQi
    {
    #pragma warning disable 169
        class Program
        {
            //因为这里加了#pragma warning  disable 169,所以字段未赋值的警告将看不见
            static int noField;
            static void Main(string[] args)
            {
                //要让这里编译else 不执行 这条语句,就要在开头加#undef DEBUG
                //否则就加#define DEBUG
    #if DEBUG
                Console.WriteLine("执行");
    #else
    
                Console.WriteLine("不执行");
    #endif
    #if !RELEASE
                Console.WriteLine("!Release");
                // ! == && || !=运算是可以的
    #elif !DEBUG  
                Console.WriteLine("!DEBUG");
    #elif DEBUG
                Console.WriteLine("DEBUG");
    #endif
    #if RELEASE
                Console.WriteLine("Release");
    #endif
    #warning "警告"
                Console.WriteLine("警告");
    //#error "异常,不要编译"
                Console.WriteLine("异常,不能编译通过");
    #line 36 "core.cs"
                Console.WriteLine("36");
                Console.ReadKey();
            }
        }
    
    }
    
     
  • 相关阅读:
    Django学习-9-ORM多对多操作
    Django学习-8-模板渲染的一些特性
    Django学习-7-ORM操作
    Django学习-6-路由系统
    Hadoop 知识
    最全的Spark基础知识解答
    windows server 2008 R2 远程连接用户数修改
    Windows 2008 R2 SP1部署WSUS 3.0 SP2
    Windows Server2008 R2 设置NAT 让Hyper-V连接Internet
    免费好用的Microsoft iSCSI Software Target 3.3
  • 原文地址:https://www.cnblogs.com/HelloQLQ/p/14998833.html
Copyright © 2011-2022 走看看