C # preprocessing command # line, # pragma warning

Source: Internet
Author: User

# Line

# Line allows you to modify the compiler's line number and (optional) Output of file names for errors and warnings. The following example shows how to report two warnings associated with the row number. # Line 200 Command forces the row number to be 200 (although the default value is #7 ). The result of another line (#9) as the default # line command follows the normal sequence.

Class MainClass

{

Static void Main ()

{

# Line 200

Inti; // CS0168 on line 200

# Line default

Char c; // CS0168 on line 9

}

}

Note:

# Line commands may be used by automatic intermediate steps during the generation process. For example, if the line 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 line and use # line to simulate the original line number. # Line hidden command hides several consecutive lines from the debugger, so that when a developer passes the code one by one, all lines between the # line hidden and the next # line command (assuming it is not another # line hidden command) will be skipped. This option can also be used to enable ASP. NET to differentiate user-defined code from computer-generated code. Although ASP. NET is the main user of this function, more source generators may use it.

# The line hidden command does not affect the file name or row number in the error report. That is, if an error occurs in the hidden block, the compiler reports the current file name and the wrong row number.

# Line filename command specifies the file name 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 quotation marks. The source code file can have any number of the # line command.

Example

The following example shows how the debugger ignores hidden rows in the code. When running this example, it displays three lines of text. However, when you set the breakpoint as shown in the example and press the F10 key to pass the code one by one, you will see that the debugger ignores hidden lines. Note that the debugger still ignores the breakpoint even if the breakpoint is set on the hidden row.

// Preprocessor_linehidden.cs

Using System;

Class MainClass

{

Static void Main ()

{

Console. WriteLine ("Normal line #1."); // set the breakpoint here

# Line hidden

Console. WriteLine ("Hidden line .");

# Line default

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

}

}

# Pragma warning command: allows us to disable and re-enable warning messages

In the process of daily development, we often compile the code, and a lot of information appears during the compilation process, A lot of useless warning information will always be prompted in a simple process to interfere with some major warnings. This can also be prevented by disabling the pre-processor command, common Warnings are as follows (CS0219, CS0681, etc ):

Let's look at an example:

Public class C

{

Int I = 10; // warning: C. I has been assigned a value, but has never used its value.

[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 and generate the following message:

Note: I use vs2010. If multiple compilations are performed, the warning will be automatically removed. To ensure that the warning is displayed in the output, clear the warning for the project:

The purpose of cleanup is to delete the Assembly files in the bin directory.

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

Add preprocessing commands to the previous Code:

# Pragma warning disable 0414

Public class C

{

Int I = 10; // warning: C. I has been assigned a value, but it is never worth it.

[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

}

}

The warning disappears when the alarm is generated again...

# Pragma warning disable/restore 0618

Disable/restore indicates the disabled and enable warnings respectively, and the following number indicates the Warning number. The warning number does not need to start with "CS.

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

However, note that # pragma warning only applies to a single file. If you want to disable warnings for multiple files, you can use the/nowarn command of the compiler. For details, see MSDN: Click here to go

PS: # pragma is a separate command, and warning is only one of the options. # pragma is used to provide special commands to the editor and describes how to compile a file containing a complex note.

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.