Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use precompiled instructions in C #

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail how to use the precompiled instructions in C#. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

1.#define and # undef

Usage:

# define DEBUG

# undef DEBUG

# define tells the compiler that I have defined a symbol of a DEBUG, which is similar to a variable, but it has no specific value, so you can think of it as a symbol. # undef is to delete the definition of this symbol. If the symbol DEBUG is not defined, # undef does not work, otherwise # define does not work. Both must be placed before the source code. The order of the two depends on the order of the code:

# define DEBUG

# undef DEBUG

In this case, DEBUG is not defined, and if the two are in different order, the compiler thinks that DEBUG is defined.

2.#if 、 # elif 、 # else 、 # endif

This tells the compiler to control the process of compiling the code. Consider the following code:

# if DEBUG Console.Write ("debug"); # elif RELEASE Console.Write ("realse"); # else Console.Write ("other"); # endif

The above code means that if DEBUG is defined, debug is output, RELEASE is defined, realse is output, otherwise other is output. What happens if DEBUG and REALSE are defined? You can try it for yourself.

3.#warning 、 # error

With these two assignments, you can tell the compiler whether to give a warning or an error message. Except for the error message, the compilation stops.

Refer to the following code:

# if DEBUG-> this DEBUG defaults to # define # warning "now Ddbug status" # elif RELEASE-> if release status is selected, there is no default. I don't know whether I am experimenting with # warning "now Realse status" # else # error "and know what state" # endif "

4.#region and # endregion

These two are used to form code blocks.

5.#line (this specified use is not clear, refer to MSDN)

This directive changes the file name and line number information displayed by the jian compiler in warning and error messages, and uses # line default to restore the line number to the default line number.

The following example shows how to report two warnings associated with a line number. The # line 200 directive forces the line number to be 200 (although the default is # 7). The other line (# 9) follows the usual sequence as the result of the default # line instruction.

Example 1:

/ / preprocessor_line.cspublic class MyClass2 {public static void Main () {# line 200 int I; / / CS0168 on line 200 # line default char c; / / CS0168 on line 9}}

Example 2:

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

/ / preprocessor_linehidden.csusing System;class MyClass {public static void Main () {Console.WriteLine ("Normal line # 1.") / / Set a break point here. # line hidden Console.WriteLine ("Hidden line."); # line default Console.WriteLine ("Normal line # 2.");}} this is the end of the article on "how to use precompilation instructions in C#". I hope the above content can be helpful to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report