What are preprocessor directives in C#?

WBOY
Release: 2023-09-16 23:45:05
forward
834 people have browsed it

C# 中的预处理器指令是什么?

The C# compiler does not have a separate preprocessor; however, these directives are processed as if there were one. In C#, preprocessor directives are used to aid conditional compilation.

Preprocessor directives issue instructions to the compiler to preprocess information before actual compilation begins.

The following are the preprocessor directives in C#-

6
Sr.No. Preprocessor Directives & Description
1 #define

It defines a sequence of characters, called a symbol.

2 #undef

It allows you to undefine symbols.

3 #if

It allows testing one or more symbols to see them Whether the calculation result is true.

4 #else

It allows creating compound conditional directives together with #if.

5 #elif

It allows the creation of compound conditional instructions.

#endif

Specifies the end of the conditional directive.

7 #line

It allows you to modify the compiler's line numbers as well as ( Optional) Filename output for errors and warnings.

8 #error

It allows generating errors coming from specific locations in the code.

9 #warning

It allows to generate one level from a specific location in the code warn.

10 #region

It allows you to specify when using the Visual Studio Code Editor The outline feature allows you to expand or collapse blocks of code.

11 #endregion

It marks the end of the #region block.

Let’s see an example to understand the usage of preprocessor directives in C# -

Example

#define PI
using System;

namespace Demo {
   class Program {
      static void Main(string[] args) {
         #if (PI)
         Console.WriteLine("PI is defined");
         #else
         Console.WriteLine("PI is not defined");
         #endif
         Console.ReadKey();
      }
   }
}
Copy after login

The above is the detailed content of What are preprocessor directives in C#?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!