C # preprocessing of instructions # line, # pragma warning (rpm)

C # preprocessing of instructions # line, # pragma warning (rpm)

Source: http: //www.cnblogs.com/jhxk/articles/2574556.html

#line 
#line lets you modify the compiler's line number (optional) errors and warnings output file name. The following example shows how to report two warnings associated with the line number. Forced #line instruction line number 200 200 (although the default is # 7). Another row (# 9) as a result of the default #line instruction sequences typically follow after.


class MainClass
{
     static void Main() 
     {
         #line 200
         int i; // CS0168 on line 200
         #line default
         char c; // CS0168 on line 9
      }
}


Note: 
#line instructions may use the intermediate step of generating an automatic process. For example, if the line is removed from the original source code file, but you still want the compiler to generate the output line of the original document based, you can remove the line, then simulate the original #line line number. #line hidden hidden several successive instruction debugger row, so that when the developer at the time of stepping through the code, will be skipped and the next #line #line hidden command (assuming it is not another command #line hidden) between all rows. This option can be also be used to distinguish user-defined ASP.NET code and the computer generated code. Although ASP.NET are the main users of this feature, it is likely that there will be more of a source generator uses it.
#line hidden directive does not affect the file name or line number in the error report. That is, if an error is encountered in a hidden block, the compiler will report the current file name and line number of the error.
#line filename directive specifies the file name you want to appear in the compiler output. By default, the actual name of the source code file. The file name must be enclosed in double quotation marks ( "") in. #Line source file can have any number of instructions.


Examples 
The following example shows how the debugger ignores the hidden lines of code. When you run this example, it will display three lines of text. However, when setting breakpoints as shown in the example and press the F10 key to step through the code, you will see the debugger ignores the hidden rows. Please also note that even if hidden set a breakpoint on the line, the debugger will ignore it.
Preprocessor_linehidden.cs //
the using the System;
class MainClass {
      static void the Main () {
           Console.WriteLine ( "Normal Line. 1 #."); // set a breakpoint here
           #line hidden
           Console.WriteLine ( "Hidden Line.");
          default #line
          Console.WriteLine ( "Normal Line # 2.");
       }
}

 

#pragma warning directive: allow us to close and reopen the warning message

In the process of development, we are always daily often compile the code, and in the process of compiling a lot of information will appear, many useless warnings will always be prompted to point out the process in order to interfere with some of the major warning, this can also be closed by a pre-processor instruction to stop the display, the common warning follows (CS0219, CS0681, etc.):

Look at an example:

C class public
{
    int I = 10; // WARNING: Ci valued, but its value is never used

    [Obsolete ( "expired", to false)]
    public static void AA ()
    { 
        
    }

  
    the Main void static () {
        int = T. 5; // WARNING: T valued, but its value is never used
        aa (); // WARNING: expired

    }
}

Compiling the generated message is as follows:

 

Note: I am using vs2010 if multiple compiler will automatically remove the warning, wishing to ensure that the warning is displayed in the output, you can do the next clean-up project:

Role is to clean up before the assembly file in the bin directory delete

 

If we do not wish to display the red box warning on the map, we can use #pragma warning directive to eliminate these warnings

Before addition of the pretreatment commands on the code:

#pragma warning disable 0618,0219,0414
public class C
{

    int i = 10; // Warning: Ci has been assigned, but never used it worth

    [Obsolete ( "expired", to false)]
    public static void AA ()
    { 
        
    }

  
    the Main void static ()
    {
        int = T. 5; // WARNING: T valued, but its value is never used
        aa (); // WARNING: expired

    }
}

It was found again generated the warning disappeared ...

#pragma warning disable/restore 0618

Which disable / restore representing the disabled and turns on the warning , and the latter number represents the number of warning, you do not need to "CS" at the beginning of the time of writing warning number here.

And this code just need to disable or write a file to open class, so these corresponding 0618 warning will not appear.

However, note that #pragma warning only have an effect on a single file, multiple files if you want to disable the warning can use / nowarn instruction compiler , specifically, see MSDN: point I entered

PS: #pragma is a single instruction, and wherein a warning only option is to effect # pragma: a special editor for instructions on how to compile a file containing pragma.

Published 17 original articles · won praise 224 · views 290 000 +

Guess you like

Origin blog.csdn.net/cxu123321/article/details/103754431