#line, #pragma warning of C # preprocessing directives

Source: Internet
Author: User

#line

#line allows you to modify the compiler's line number and (optionally) the file name output for errors and warnings. The following example shows how to report two warnings associated with a row number. The #line 200 instruction forces the line number to 200 (although the default value is #7). The other line (#9) as the result of the default #line instruction is followed by the usual sequence.

Class MainClass

{

static void Main ()

{

#line 200

int i; CS0168 on line 200

#line default

char c; CS0168 on line 9

}

}

Note:

#line directives may be used by automated intermediate steps in the build process. For example, if a row is removed from the original source code file, but you still want the compiler to generate output based on the original line number in the file, you can remove the row and then simulate the original line number with #line. #line the hidden directive hides several contiguous rows of the debugger so that when the developer steps through the code, all rows between the #line hidden and the next #line instruction (assuming it is not another #line hidden directive) are skipped. This option can also be used to enable ASP.net to differentiate between user-defined code and computer-generated code. Although ASP.net is the primary consumer of this feature, it is likely that more source generators will use it.

#line the 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 reports the current file name and the wrong line number.

#line filename directive specifies the name of the file that you want to appear in the compiler output. By default, the actual name of the source code file is used. The file name must be enclosed in double quotes (""). The source code file can have any number of #line directives.

Example

The following example shows how the debugger ignores hidden rows in code. When you run this example, it will display three lines of text. However, when you set the breakpoint as shown in the example and press F10 to step through the code, you will see that the debugger ignores the hidden rows. Also note that even if you set a breakpoint on a hidden line, the debugger ignores it.

Preprocessor_linehidden.cs

Using System;

Class MainClass

{

static void Main ()

{

Console.WriteLine ("Normal line #1."); Set breakpoints Here

#line hidden

Console.WriteLine ("Hidden line.");

#line default

Console.WriteLine ("Normal line #2.");

}

}

#pragma warning directive: allow us to turn off and reopen warning messages

In the day-to-day development process, we always compile the code, and in the process of compiling a lot of information, a lot of useless warning information will always be prompted in a little bit of the process to interfere with some of the main warning, this is also a preprocessor directive can be closed to prevent its display, the common warning is as follows ( CS0219, CS0681, etc.):

Look at an example:

public class C

{

int i = 10;//Warning: C.I has been assigned, but its value has never been used

[Obsolete ("Expired", false)]

public static void AA ()

{

}

static void Main ()

{

int t=5; Warning: T has been assigned, but its value has never been used

AA (); Warning: Expired

}

}

Compile the build message as follows:

Note: What I'm using is vs2010 if multiple compilations automatically remove warnings, and if you want to make sure that warnings are displayed in the output, you can clean up the project:

The role of cleanup is to delete the assembly files that were previously in the bin directory

If we do not want to display the warning in the red box above, we can use the #pragma warning directive to eliminate these warnings

Add preprocessing directives on the previous code:

#pragma warning Disable 0618,0219,0414

public class C

{

int i = 10;//Warning: C.I has been assigned, but never used it's worth

[Obsolete ("Expired", false)]

public static void AA ()

{

}

static void Main ()

{

int t=5; Warning: T has been assigned, but its value has never been used

AA (); Warning: Expired

}

}

When it is generated again, the warning disappears ...

#pragma warning Disable/restore 0618

Where Disable/restore represents the disable and turn off warning, and the following number represents the warning number, where the warning number is written without the "CS" start.

This code can only be written in a class file that needs to be disabled or opened, so that the corresponding 0618 warning will not appear.

However, it is important to note that #pragma warning only works on a single file, and if you want to disable warnings for multiple files, you can use the compiler's/nowarn directive, as you can see in MSDN: Point I entered

PS: #pragma is a separate instruction, and warning is just one of the options, #pragma的作用是: Used to give editors special instructions on how to compile files that contain pragma.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.