簡單介紹C#預處理
C#預處理
C#預處理階段是一個文本到文本的轉換階段,在預處理過程中,使能進行代碼的條件包含和排除。
- pp-un it:
- pp-gro up opt
- pp-gro up:
- pp-gro up-part
- pp-gro up pp-group-part
- pp-gro up-part:
- pp-tokensopt new-line
- pp-de claration
- pp-if -section
- pp-con trol-line
- pp-l ine-number
- pp-tokens:
- pp-token
- pp-tokens pp-token
- pp-token:
- identifi er
- keyword
- literal
- operator-or-punctuator
- new-line:
- The carriage return character (U+000D)
- The line feed character (U+000A)
- The carriage return character followed by a line feed character
- The line separator character (U+2028)
- The paragraph separator character (U+2029)
C#預處理聲明
在預處理過程中,為了使用名稱可以被定義和取消定義。#define 定義一個標識符。#undef “反定義”一個標識符,如果一個標識符在以前已經(jīng)被定義了,那么它就變成了不明確的。如果一個標識符已經(jīng)被定義了,它的語意就等同于true ;如果一個標識符沒有意義,那么它的語意等同于false.
- pp-de claration:
- #define pp-identifier
- #undef pp-identifier
來看看這個例子:
- #define A
- #undef B
- class C
- {
- #if A
- void F()
- #else
- void G()
- #endif
- #if B
- void H()
- #else
- void I()
- #endif
- }
- 變?yōu)?
- class C
- {
- void F()
- void I()
- }
如果有一個pp-unit, 聲明就必須用pp- token 元素進行。換句話說,#define 和#undef 必須在文件中任何 “真正代碼”前聲明,否則在編譯時會發(fā)生錯誤。因此,也許會像下面的例子一樣散布#if 和#define:
- define A
- #if A
- #define B
- #endif
- namespace N
- {
- #if B
- class Class1
- #endif
- }
因為#define 放在了真實代碼后面,所以下面的例子是非法的:
- #define A
- namespace N
- {
- #define B
- #if B
- class Class1
- #endif
- }
以上介紹C#預處理
【編輯推薦】